I am trying to test more than one variable in an if statement.
double firstPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];
In fact their will be more than one variable:
thirdPlot
fourthPlot
…
if(firstPlot < highPrice && firstPlot > lowPrice)
Is it possible to test each variable in one shot inside the if statement without having to repeat the if statement the same number of times as the number of variables? Like creating one variable that will gather all Plot and be tested all at the same time separately inside the if statement?
Thank you
Looks like Values is a jagged array from which you want to extract the first element of each underlying array, so the following should work:
using System.Collections.Generic;
using System.Linq;
...
double[][] plots = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;
IEnumerable<double> firstValues = plots.Select(a => a[0]);
bool allValuesInRange = firstValues.All(v => v < highPrice && v > lowPrice);
EDIT after your comment:
NinjaTrader.NinjaScript.Series<double>[] series =
RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;
IEnumerable<double> firstValues = series.Select(s => s[0]);
foreach (double firstValue in firstValues)
{
Console.WriteLine(firstValue);
}
Can you iterate through your array instead? E.g., something like this:
private bool CheckValues(/*params here*/)
{
var RMMAVals = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5);
for (int k = 0; k<RMMAVals.Length; k++)
{
if (RMMAVals[k][0] >= highPrice || RMMAVals[k][0] <= lowPrice)
return false;
}
return true;
}
Related
I am trying to create an array based on the ones that I have. I have two arrays which look like this
sample array A: {AB01, AB01, AB01, AB02, AB02, AB02, AB03, AB01}
sample array B: {10, 10, 20, 10, 20, 20, 40, 10}
Both arrays are dynamic so provided data is just a sample data. Same indexes in both arrays creates a pair: so for example: pair 1: AB01 10 , pair 2: AB01 10 and so on. Based on those two arrays I want to create third array which will represent indexes but including duplicates. So I want my third array to look like this:
array (3) C: {10, 10, 20, 30, 40, 40, 50, 10}
FYI I cannot use LINQ 4+
I tried nested loop over the arrays but I do not get the expected result.
for(int i = 0; i < arrayA.Length; i++){
for(int j = i + 1; j < arrayA.Length; j++){
if(arrayA[i] == arrayA[j] && arrayB[i] == arrayB[j]){
arrayC.Insert(i, 10 * (i+1));
}
else
arrayC.Insert(i, 10 * (i+2));
}
}
As I mentioned I expext my array C to be indexes of each element but I want to include duplicates:
array (3) C:
{10, 10, 20, 30, 40, 40, 50, 10}
EDIT:
As the result is not clear.
If there was no duplicates in the arrays I would want my array C to look like this:
{10, 20, 30, 40, 50, 60, 70, 80}
It means that each pair eg(AB01, 10) will be given index 10 in array C.
But when it comes to duplicates array C will look different.
Pair AB01 - 10 appears as first so I want to give it index 10. And because it appears three times at position: 0, 1 and 7 I want number 10 to be at those positions in array C. Next on I have pair AB01 - 20 it appears only one time at position 2 and values are different than in pair to so I want to give it index 20. And so on.
As written in the comments, a re-formulation of the task is:
for each pair, if it has been encountered before, use the already
allocated "index", otherwise create a new one as (previous index) +
10.
You need to remember the already assigned indexes, e.g. in a Dictionary<(string, int), int>, mapping a pair to the index. In addition. you need to keep track of the highest index assigned so far.
var arrayA = new string[] { "AB01", "AB01", "AB01", "AB02", "AB02", "AB02", "AB03", "AB01"};
var arrayB = new int[] {10, 10, 20, 10, 20, 20, 40, 10};
var assignedIndexes = new Dictionary<(string, int), int>();
var highestIndex = 0;
var result = new int[arrayA.Length];
for (var i = 0; i < arrayA.Length; i++)
{
var tuple = (arrayA[i], arrayB[i]);
if (!assignedIndexes.TryGetValue(tuple, out var index))
{
index = highestIndex + 10;
assignedIndexes.Add(tuple, index);
highestIndex = index;
}
result[i] = index;
}
Console.WriteLine(string.Join(", ", result));
Prints: 10, 10, 20, 30, 40, 40, 50, 10
If you cannot use ValueTuples, too, replace them by Tuples:
var assignedIndexes = new Dictionary<Tuple<string, int>, int>();
var tuple = Tuple.Create(arrayA[i], arrayB[i]);
I am using this library for combinatorics:
https://github.com/eoincampbell/combinatorics/
What I need is to find n-th permutation and count elements of fairly large sets (up to about 30 elements), but I get stopped in my tracks before even starting, check out this code:
int[] testSet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
var permutation = new Permutations<int>(testSet);
var test = permutation.Count;
Everything works peachy just until 20 element large set, once I add 21st, permutations stop working right, eg.
here is what permutation.Count returns:
-4249290049419214848
which is far from being the right number.
I am assuming that it all boils down to how huge numbers I use - overflowing ints/longs that library uses. That is why, I am asking for an advice - is there a library? approach? or a fairly quick to implement way to have combinatorics work on bigintegers?
Thanks!
Get the number of possible permuations.
The number of permutations is defined by nPr or n over r
n!
P(n,r) = --------
(n - r)!
Where:
n = Number of objects
r = the size of the result set
In your example, you want to get all permutations of a given list. In this case n = r.
public static BigInteger CalcCount(BigInteger n, BigInteger r)
{
BigInteger result = n.Factorial() / (n - r).Factorial();
return result;
}
public static class BigIntExtensions
{
public static BigInteger Factorial(this BigInteger integer)
{
if(integer < 1) return new BigInteger(1);
BigInteger result = integer;
for (BigInteger i = 1; i < integer; i++)
{
result = result * i;
}
return result;
}
}
Get the nTh permutation
This one depends on how you create/enumerate the permutations. Usually to generate any permutation you do not need to know all previous permutations. In other words, creating a permutation could be a pure function, allowing you to directly create the nTh permutation, without creating all possible ones.
This, however, depends on the algorithms used. But will potentially be a lot faster to create the permutation only when needed (in contrast to creating all possible permutations up front -> performance and very memory heavy).
Here is a great discussion on how to create permutations without needing to calculate the previous ones: https://stackoverflow.com/a/24257996/1681616.
This is too long for a comment, but wanted to follow up on #Iqon's solution above. Below is an algorithm that retrieves the nth lexicographical permutation:
public static int[] nthPerm(BigInteger myIndex, int n, int r, BigInteger total)
{
int j = 0, n1 = n;
BigInteger temp, index1 = myIndex;
temp = total ;
List<int> indexList = new List<int>();
for (int k = 0; k < n; k++) {
indexList.Add(k);
}
int[] res = new int[r];
for (int k = 0; k < r; k++, n1--) {
temp /= n1;
j = (int) (index1 / temp);
res[k] = indexList[j];
index1 -= (temp * j);
indexList.RemoveAt(j);
}
return res;
}
Here is a test case and the result of calling nthPerm using the code provided by #Iqon.
public static void Main()
{
int[] testSet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
BigInteger numPerms, n, r;
n = testSet.Length;
r = testSet.Length;
numPerms = CalcCount(n, r);
Console.WriteLine(numPerms);
BigInteger testIndex = new BigInteger(1234567890987654321);
int[] myNthIndex = nthPerm(testIndex, (int) n, (int) r, numPerms);
int[] myNthPerm = new int[(int) r];
for (int i = 0; i < (int) r; i++) {
myNthPerm[i] = testSet[myNthIndex[i]];
}
Console.WriteLine(string.Join(",", myNthPerm));
}
// Returns 1,12,4,18,20,19,7,5,16,11,6,8,21,15,13,2,14,9,10,17,3
Here is a link to ideone with working code.
You can useJNumberTools
List<String> list = new ArrayList<>();
//add elements to list;
JNumberTools.permutationsOf(list)
.uniqueNth(1000_000_000) //next 1 billionth permutation
.forEach(System.out::println);
This API will generate the next nth permutation directly in lexicographic order. So you can even generate next billionth permutation of 100 items.
for generating next nth permutation of given size use:
maven dependency for JNumberTools is:
<dependency>
<groupId>io.github.deepeshpatel</groupId>
<artifactId>jnumbertools</artifactId>
<version>1.0.0</version>
</dependency>
I have an array like this
int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};
I want to find the greatest number and make it In order from largest to smallest
like this
100,75,68,63,54,36,25,24,12,10,9,6,5,1
int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
int maxValue = intnumber.Max();
You can sort the array for viewing elements in ascending order
Array.Sort(intnumber);
Array.Reverse(intnumber);
foreach (var str in intnumber )
{
MessageBox.Show(str.ToString());
}
Try this,
int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
//Maximum Value
int maxValue = intnumber.Max();
//Maximum Index
int maxIndex = intnumber.ToList().IndexOf(maxValue);
You can use :
int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};
Array.Sort(intnumber );
Array.Reverse(intnumber );
int max = intnumber[0];
exactly output that you want.
int[] intnumber = new int[] { 10,25,12,36,100,54,68,75,63,24,1,6,9,5 };
Array.Sort<int>(intnumber ,
new Comparison<int>(
(i1, i2) => i2.CompareTo(i1)
));
intnumber .Dump();
P.S. To run this demo you need to follow these steps:
1.Download LINQPad.
2.Download the demo file, open it with LINQPad and hit F5.
I found my answer with your helps
Console.WriteLine("How many Numbers Do you want? ");
int counter = int.Parse(Console.ReadLine());
double[] numbers = new double[counter];
for (int i = 0; i < numbers.Length; i++)
{
Console.Write((i + 1) + " : ");
numbers[i] = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("_______________________________________________");
Array.Sort(numbers);
Array.Reverse(numbers);
foreach (double item in numbers)
{
Console.WriteLine(item);
}
Console.WriteLine("_______________________________________________");
Console.WriteLine("The Greatest Number is " + numbers[0]);
Console.ReadKey();
Let intNumbers be the array that you are using, Then you can use the .Max() method of the Array Class to get the maximum value, that is the greatest number. If you want to Sort the Current array means You have to use the .Sort() method. The requirement is simply Printing the Array in descending order means you have to use the .OrderBy()
int[] inputNumbers = new int[] { 15, 12, 11, 23, 45, 21, 2, 6, 85, 1 };
Console.WriteLine("Input Array is : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));
Console.WriteLine("Max value in the array is : {0}\n",inputNumbers.Max());
Console.WriteLine("Array in descending order : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));
Here is a working Example
int max = Integer.MIN_VALUE;
for (int i =0; i < intnumber.length; i++)
{
int num = intnumber[i];
//Check to see if num > max. If yes, then max = num.
}
System.out.println(max);
I was trying to find (using a code listed below) a simple solution for copying all the objects which are stored in first array, to second array, with changing paralelly the index of objects in second array + 1, so the object[0] in first array would be equal to object[1] in second one, and the last object[9] in the first one would be equal to the object[0] in second.
While trying to start code which I've written I've received message, which stated that "Destination array was not long enough Check destIndex and length, and the array's lower bounds", I'm just starting with arrays part at c#, so I'd be very gratefull for any quidance.
static void Main(string[] args)
{
int[] first = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int[] second = new int[first.Length];
Array.Copy(first, 0, second, 1, 10);
foreach (int x in second)
{
Console.WriteLine("{0}", x);
}
Console.ReadKey();
}
}
Instead of Array.Copy and foreach you can do this:
for (int i = 0; i < first.Length; i++)
second[i == first.Length - 1 ? 0 : i + 1] = first[i];
It just goes from i = 0 to i = first.Length-1, copying the element at that index in first to that index plus 1 in second.
If the idea is to rotate the array one position forward, you can use a simple for:
static void Main(string[] args)
{
int[] first = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int[] second = new int[first.Length];
for (int k = 0; k < first.Length; k++)
second[(k == (first.Length - 1))? 0: k + 1] = first[k];
foreach (int x in second)
{
Console.WriteLine("{0}", x);
}
Console.ReadKey();
}
Is there a good way to emulate yield in Ruby? I'm interested in writing similar 'infinite fib sequence' in Ruby.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace cs2 {
class Program {
static void Main(string[] args) {
var i=Fibs().TakeWhile(x=>x < 1000).Where(x=>x % 2==0).Sum();
}
static IEnumerable<long> Fibs() {
long a = 0, b = 1;
while (true) {
yield return b;
b += a;
a = b - a;
}
}
}
}
If it is possible, please give an example.
The common idiom in ruby to implement such sequences, is to define a method that executes a block for each element in the sequence if one is given or return an enumerator if it is not. That would look like this:
def fibs
return enum_for(:fibs) unless block_given?
a = 0
b = 1
while true
yield b
b += a
a = b - a
end
end
fibs
#=> #<Enumerable::Enumerator:0x7f030eb37988>
fibs.first(20)
#=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
fibs.take_while {|x| x < 1000}.select {|x| x%2 == 0}
#=> [2, 8, 34, 144, 610]
fibs.take_while {|x| x < 1000}.select {|x| x%2 == 0}.inject(:+)
=> 798
Fibonacci numbers with Ruby 1.9 Fibers:
fib = Fiber.new do
x, y = 0, 1
loop do
Fiber.yield y
x,y = y,x+y
end
end
20.times { puts fib.resume }