If I have an integer array of variable length [n] is it possible to iterate it so that each value increments only when the next value has reached n-1.
for example if n=3:
int[] intArray = {0, 0, 0};
increment intArray[] such that:
intArray = {0, 0, 1}
intArray = {0, 0, 2}
intArray = {0, 1, 0}
intArray = {0, 1, 1}
intArray = {0, 1, 2}
intArray = {0, 2, 0}
intArray = {0, 2, 1}
intArray = {0, 2, 2}
intArray = {1, 0, 0}
intArray = {1, 0, 1}
...
intArray = {2, 2, 2}
It helps to think of patterns in your problems - sometimes they suggest general solutions that may be simple to break down and implement.
In this case, the array contents are acting like base-n numbers (base-3 in the specific example). So you could consider an algorithm for incrementing an arbitrary numeric base, and once you have that move on to coding it.
A function that would "increment" a base-n number would start by incrementing the least-significant digit and checking it for overflow. In base-10, we would retain the 0, and carry the 1 to the next most significant digit. In an arbitrary numeric base, this is the same as resetting the digit to zero and incrementing the next most significant digit.
There are special cases to consider. What if you're asked to increment the "maxint" in your numeric base, e.g. {2, 2, 2}? Will you add another significant digit? Will you peg the value at "maxint", or wrap to 0? Will you throw an exception? There's no "right" answer, unless you already have a specification.
In pseudo-code, here's the idea:
increment( digits , base ) {
for (place=digits.length - 1; place >= 0; place-- ) {
digit = digits[place]++; // increment digit in this place
if (digit < base)
break;
else
digit = 0; // overflowed, so "wrap" to zero, increment next place
}
return digits;
}
Related
I was wondering whether there's an easy way to implement the method I've described in the comments below
// Yields all the longs in the range [first, last] that
// are composed of the prime digits.
// For example, if the range is [4, 50] then the numbers
// yielded are 5, 7, 22, 23, 25, 27, 32, 33, 35, 35,
// although they don't necessarily need to be yieled in order.
static IEnumerable<long> PossiblesInRange(long first, long last)
{
throw new NotImplementedException();
}
The prime digits, in order, are
static long[] PrimeDigits = { 2, 3, 5, 7 };
I have list that is guaranteed to contain sequential pairs of identical elements. Is there a way to remove half of repeating values in a list (any one element of each pair)?
Example #1:
Take:
{2, 2, 2, 2, 5, 5}
And return:
{2, 2 , 5}
Example #2:
Take:
{8, 8, 1, 1, 5, 5, 1, 1}
And return:
{8, 1, 5, 1}
There is no need to verify if elements actually comes in pairs.
If the sequence is guaranteed to be made of consecutive pairs, then this works:
values.Where((x, n) => n % 2 == 0)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have array which contains unique random numbers (where numbers have small possible range of 0 to 20). For example:
[6, 3, 11, 9, 4, 5]
How can I convert following array to something like this:
[3, 0, 5, 4, 1, 2]
The second array starts from 0 and ends with (array.Length-1), but placement is relative to magnitude in the first array.
How can I implement this in an efficient way in C/C++/C#? (more interested in the method)
I gave just one example. It can be really anything:
[7, 10, 0, 19, 50, 33, 45, 100]
[1, 2, 0, 3, 6, 4, 5, 7]
Smallest number from array A is 0 in array B. Biggest number in array A is (array.Length-1) in array B. Array A can be completely random (just it will never contain two or more identical numbers), but array A have to contain all numbers from 0 to array.Length-1) in same order as in array A.
int[] list1 = new[] { 7, 10, 0, 19, 50, 33, 45, 100 };
var orderedList = list1.OrderBy(x => x).ToList();
int[] list2 = list1.Select(x => orderedList.IndexOf(x)).ToArray();
EDIT
Per #Blorgbeard's request
int[] list1 = new[] { 6, 3, 11, 9, 4, 5 };
var dict = list1.OrderBy(x => x)
.Select((i, inx) => new { i, inx })
.ToDictionary(x => x.i, x => x.inx);
int[] list2 = list1.Select(x => dict[x]).ToArray();
I've tried copying arrays in such a way I can crunch data in an array with threads but obviously without splitting the array into smaller chunks (lets say 1 array -> 4 quarters (4 arrays)).
The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.
Here is pseudo code to show what I wish to do.
int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15 }
or lets say the length of the array cant be split up evenly
int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15, 16}
The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.
It's a shame you didn't show which method that was. Array.Copy has various overloads for copying part of an array to another array. This one is probably the most helpful:
public static void Copy(
Array sourceArray,
int sourceIndex,
Array destinationArray,
int destinationIndex,
int length
)
Alternatively, look at Buffer.BlockCopy, which has basically the same signature - but the values are all in terms of bytes rather than array indexes. It also only works with arrays of primitives.
Another alternative would be not to create copies of the array at all - if each thread knows which segment of the array it should work with, it can access that directly. You should also look into Parallel.ForEach (and similar methods) as a way of parallelizing operations easily at a higher level.
For example, I have an array of floating point numbers:
float[] numbers = new float[] { 1, 34, 65, 23, 56, 8, 5, 3, 234 };
If I use:
Array.Sort(numbers);
Then the array is sorted by the size of the number.
I want to sort the numbers by another criteria, so element A should go before element B if f(A) < f(B), rather than the usual of A < B.
So, for example, If I want to sort them according to there value modulo 5. The array would become:
5, 65, 1, 56, 3, 8, 23, 34, 234
I think it can be done through LINQ, but I'm not sure how.
I want to sort the numbers by another criteria, so element A should go before element B if f(A) < f(B)
numbers.OrderBy(f);
You can use the Comparison<T> overload of Array.Sort:
Array.Sort(numbers, (a,b) => (a % 5).CompareTo(b % 5));
Comparison<T> is just a delegate, so you can use lambdas / anonymous methods. It's not LINQ, but I think it's what you meant.
Using LINQ:
var result = from n in numbers orderby n % 5, n select n;
var sortedNumbers = result.ToArray();
Alternately:
var result = numbers.OrderBy(n => n % 5).ThenBy(n => n);
Ordering by mod 5, then by the number yields the results in the order you specified.
Have a look at IComparer ;-) and let an List sort the elements for you with your custom comparer.
More Brute even! I suggest you to make an Array of f(x) and then sort on that!