I want to create same sized smaller arrays from a big array. But items should distribte randomly. I can distribute by order like following:
int[] source = new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
int i = 0;
int chunkSize = 3;
var result = source.GroupBy(s => i++ / chunkSize).Select(g => g.ToArray()).ToArray();
// [10,20,30][40,50,60][70,80,90]
But result should be random like: // [90,20,50][70,30,60][40,80,10]
Can I do it using linq?
The following implements the suggestions in the comments to add an OrderBy clause. There are, of course, other ways to achieve the result, but this is likely the simplest way using LINQ, as requested.
int[] source = new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
int i = 0;
int chunkSize = 3;
Random r = new Random();
var result = source.OrderBy(x => r.Next()).GroupBy(s => i++ / chunkSize).Select(g => g.ToArray()).ToArray();
You can shuffle any 1D array with this method.
If you modify it to return IList<T> instead of void, you can append it into your query:
var result = source.Shuffle().GroupBy(...)
You could do the following.
var arr = new int[] { 1, 1, 2, 6, 6, 7, 1, 1, 0 };
var sizeOfResultArray = 3;
var result = arr.ChunkBy(sizeOfResultArray);
The extension methods are defined as
public static class Extensions
{
private static Random rng = new Random();
public static IEnumerable<IEnumerable<T>> ChunkBy<T>(this IEnumerable<T> source, int chunkSize)
{
return source.Shuffle()
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
{
var collection = list.ToList();
int n = collection.Count();
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = collection[k];
collection[k] = collection[n];
collection[n] = value;
}
return collection;
}
}
The Shuffle is a Fisher-Yates shuffle implementation by Grenade here
You need to randomize and then group. Code below should work
int[] source = new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
Random rand = new Random();
int[] random = source.Select(x => new { num = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.num).ToArray();
int groupsize = 3;
int[][] groups = random.Select((x, i) => new { num = x, index = i })
.GroupBy(x => x.index / groupsize)
.Select(x => x.Select(y => y.num).ToArray())
.ToArray();
Related
I have int array representing elements of square matrix. I need to get rows,columns, 2 main diagonals of it. I have trouble with getting second diagonal (for 3*3 matrix it is elements with indexes 2,4,6 for 6*6 - 5,10,15,20,25,30). I'm wondering is there a neat way to do it without for loops.
var matrix = new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 };
int size = 3;
int i = 0;
var rows = matrix.GroupBy(x => i++ / size);
i = 0;
var columns = matrix.GroupBy(x => i++ % size);
var diag1 = matrix.Where((x, index)=>index%(size+1)==0);
var diag2 = matrix.Where((x, index) => index % (size - 1) == 0);//PROBLEM - takes 0,8 indexes also.
Try this
var matrix = new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 };
int size = 3;
var rows = matrix.Select((x,i) => new {x = x, i = i}).GroupBy(x => x.i / size).Select(x => x.Select(y => y.x).ToArray()).ToArray();
var columns = matrix.Select((x, i) => new { x = x, i = i }).GroupBy(x => x.i % size).Select(x => x.Select(y => y.x).ToArray()).ToArray();
var diag1 = matrix.Where((x, index) => (index /size) == (index % size)).Select(x => x).ToArray();
var diag2 = matrix.Where((x, index) => (index / size) == (size - 1) - (index % size)).Select(x => x).ToArray();
Here's what I mean. Suppose S = {1, 4} and N = 5. The ordered arrangements of elements in the set S would be like
{1}, {4}, {1,1}, {1,4}, {4,1}, {4,4}, {1,1,1}, ....
and the ones that sum up to N are
{1,4}, {4, 1}, {1,1,1,1,1}
I want an algorithm that finds those efficiently.
My "brute force" way would be like
static IEnumerable<IEnumerable<int>> OrderedArrangements(IEnumerable<int> nums, int k)
{
var singles = nums.Select(i => new int[] {i} );
var cumulative = singles;
for(int j = 2; j <= k; ++j)
{
var last = cumulative.Where(list => list.Count() == (j - 1));
var next = from x in singles
from y in last
select x.Concat(y);
cumulative = cumulative.Concat(next);
}
return cumulative;
}
and then
int sumToN = OrderedArrangements(new int[] {1, 4}, N)
.Where(x => x.Sum() == N);
but I'm wondering if there's an obvious and more efficient way to do this.
Just in case the above answer isn't clear enough, you could try straight forward recursion e.g.
...
/ \
(1) (4)
/ \ / \
(1)(4) (1)(4)
static void f(int sum, int n, String str, int[] arr){
if (n == sum){
Console.WriteLine(str);
return;
}
if (n > sum) return;
for (int i = 0; i < arr.Length; i++){
f(sum, n + arr[i], str + arr[i].ToString(), arr);
}
}
static void Main(string[] args){
int[] arr = { 1, 4 };
f(5, 0, "", arr);
}
Where sum is N in your question, n is initialized to 0, str is initialized to "" and arr is S in your question.
output:
11111
14
41
This works for me:
static IEnumerable<IEnumerable<int>> OrderedArrangements(IEnumerable<int> nums, int k)
{
return
k <= 0
? new [] { Enumerable.Empty<int>() }
: nums
.SelectMany(
n => OrderedArrangements(nums, k - n),
(n, ns) => new [] { n }.Concat(ns))
.Where(ns => ns.Sum() == k);
}
The result of OrderedArrangements(new [] { 1, 4 }, 5) is:
I ran this performance testing code:
Func<Func<IEnumerable<IEnumerable<int>>>, double> measure = f =>
{
var sw = Stopwatch.StartNew();
var result = f();
sw.Stop();
return sw.Elapsed.TotalMilliseconds;
};
var n = 200;
var a = 0.0;
var b = 0.0;
for (var i = 0; i < n; i++)
{
a += measure(() => OrderedArrangements_A(new [] { 1, 4, 9, 13 }, 50000));
b += measure(() => OrderedArrangements_B(new [] { 1, 4, 9, 13 }, 50000));
}
OrderedArrangements_A is the OP's code and OrderedArrangements_B is mine.
I got an average of 15.6ms for "A" and 0.004ms for "B". My code run about 3,895 times faster for this test.
i want to generate random number from two list. i want to create a function where i pass how much random number from two list.
List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
i want my result = List<int> result = {54,40,123,17,3,1,3489,76...etc}
When i run again the set of result will be change.
Presently i am using this function that return List
public static List<int> GenerateRandom(int count)
{
// generate count random values.
HashSet<int> candidates = new HashSet<int>();
while (candidates.Count < count)
{
// May strike a duplicate.
candidates.Add(random.Next(1,30));
}
// load them in to a list.
List<int> result = new List<int>();
result.AddRange(candidates);
// shuffle the results:
int i = result.Count;
while (i > 1)
{
i--;
int k = random.Next(i + 1);
int value = result[k];
result[k] = result[i];
result[i] = value;
}
return result;
}
i am calling the function
List<int> vals = GenerateRandom(20);
But i want that the random number from two List<> List<int> integers and List<int> value2 . so how can i do this .
You can do something like this:
var result =
integers.Concat(value2)
.OrderBy(x => random.Next())
.Take(count)
.ToList();
You could write a general-purpose function to give you a random ordering of any number of sequences, like so:
public static IReadOnlyCollection<T> InRandomOrder<T>(Random rng, params IEnumerable<T>[] lists)
{
return lists
.SelectMany(x => x)
.OrderBy(y => rng.Next())
.ToList();
}
You can then pass as many lists as you like and get the contents back in a fully randomised order:
var list1 = new[] {1, 2, 3, 4, 5};
var list2 = new[] {6, 7, 8};
var list3 = new[] {9, 0};
Random rng = new Random();
for (int i = 0; i < 10; ++i)
{
var randomisedFirst5 = InRandomOrder(rng, list1, list2, list3).Take(5);
Console.WriteLine(string.Join(", ", randomisedFirst5));
}
There's a less efficient approach you can use that avoids the need for an instance of Random, but you should only use this for short lists or where you really don't care about performance. It uses Guid.NewGuid() to generate random numbers:
public static IReadOnlyCollection<T> InRandomOrder<T>(params IEnumerable<T>[] lists)
{
return lists
.SelectMany(x => x)
.OrderBy(y => Guid.NewGuid())
.ToList();
}
Even the more efficient approach isn't the fastest. A faster way would be to use reservoir sampling to take the first N items that you want, and put them into an array which you shuffle using Knuth. That would make it a lot faster, at the expense of more complicated code - meaning you should only do it the fast way if it's really needed.
If what you want is to select a number that exists either in list A or B, randomly, you can do:
List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
List<int> allInOne = new List<int>(integers.Concat(value2));
Random r = new Random(DateTime.Now.Millisecond);
/********************************
For demonstration purposes
********************************/
for(int i = 0; i < 5; i++)
{
var randomListIndex = r.Next(0, allInOne.Count - 1);
Console.WriteLine(allInOne[randomListIndex]);
}
Use KeyValuePair
static void Main(string[] args)
{
List<KeyValuePair<int, int>> results = GenerateRandom(100);
}
static List<int> integers = new List<int>() { 54, 23, 76, 123, 93, 7, 3489 };
static List<int> value2 = new List<int>() { 1, 3, 4, 6, 8, 17, 40 };
static Random random = new Random();
public static List<KeyValuePair<int,int>> GenerateRandom(int count)
{
List<KeyValuePair<int,int>> result = new List<KeyValuePair<int,int>>();
for(int i = 0; i < count; i++)
{
int firstValue = integers[random.Next(0, integers.Count - 1)];
int seconValue = value2[random.Next(0, value2.Count - 1)];
result.Add(new KeyValuePair<int,int>(firstValue,seconValue));
}
return result;
}
I actually made a library a while back that handles some of this stuff : Underscore.cs
It's a nuget package so easy to install, the code to shuffle or take a sample randomly of two lists is :
var ls1 = GenerateRandom(10);
var ls2 = GenerateRandom(20);
var mixer = ls1.Concat(ls2).ToList();
//if you want all of the items shuffled use shuffle
var result = _.List.Shuffle(mixer);
//or if you want a subset randomly sorted use sample
result = _.List.Sample(mixer);
I have two arrays say one is string array and the other is int array
string array has---> "11","11","11","11","12","12" elements and the int array has 1,2,3,4,5,6 respectively.
I want result two arrays containing string array--->"11","12"
and int array---->10,11
If the string array has duplicate elements, the other array containing that respective index value must be added .For example "11" is in 1st,2nd,3rd,4th index So its corresponding value must sum of all those elements in other array.Can it be done?
I have written some code but unable to do it..
static void Main(string[] args)
{
//var newchartValues = ["","","","","","",""];
//var newdates = dates.Split(',');
//string[] newchartarray = newchartValues;
//string[] newdatearray = newdates;
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
string[] newdates = new string[] { "11", "11","11","12","12","12" };
int[] intarray = new int[newchartValues.Length];
List<int> resultsumarray = new List<int>();
for (int i = 0; i < newchartValues.Length - 1; i++)
{
intarray[i] = Convert.ToInt32(newchartValues[i]);
}
for (int i = 0; i < newdates.Length; i++)
{
for (int j = 0; j < intarray.Length; j++)
{
if (newdates[i] == newdates[i + 1])
{
intarray[j] += intarray[j + 1];
resultsumarray.Add(intarray[j]);
}
}
resultsumarray.ToArray();
}
}
I don't quite get what you need, but I think I fixed your code, result will contain 10 and 11 in this example:
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
string[] newdates = new string[] { "11", "11", "11", "11", "12", "12" };
List<int> result = new List<int>();
if (newdates.Length == 0)
return;
string last = newdates[0];
int cursum = newchartValues[0];
for (var i = 1; i <= newdates.Length; i++)
{
if (i == newdates.Length || newdates[i] != last)
{
result.Add(cursum);
if (i == newdates.Length)
break;
last = newdates[i];
cursum = 0;
}
cursum += newchartValues[i];
}
Here is an approach that should do what you want:
List<int> resultsumarray = newdates
.Select((str, index) => new{ str, index })
.GroupBy(x => x.str)
.Select(xg => xg.Sum(x => newchartValues[x.index]))
.ToList();
Result is a List<int> with two number: 6, 15
Something like this?
int[] newchartValues = new int[] { 1, 2, 3, 4, 5, 6 };
int[] newdates = new int[] { 11, 11,11,12,12,12 };
var pairs = Enumerable.Zip(newdates, newchartValues, (x, y) => new { x, y })
.GroupBy(z => z.x)
.Select(g => new { k = g.Key, s = g.Sum(z => z.y) })
.ToList();
var distinctDates = pairs.Select(p => p.k).ToArray();
var sums = pairs.Select(p => p.s).ToArray();
I have an array:
int[] months = new int[4] {1, 4, 7, 10};
I would like to sort the array starting by the given value and sort the rest of the array in the original order.
Let's say I want to start sorting the array by a value of 7. The sorted array would be then in order of:
7, 10, 1, 4
Or starting with a value 4 the sorted array would be an order of
4, 7, 10, 1
How about:
var orderedMonths = months.Where(x => x >= 7)
.OrderBy(x => x)
.Concat(months.Where(x => x < 7));
Note that this will mean that the elements of the "rest of the array" will be in order of appearance rather than increasing numeric order. If you meant the latter (i.e. sort both 'segments' numerically) , I would do:
var orderedMonths = months.OrderBy(x => x < 7) // false comes before true
.ThenBy(x => x);
On the other hand, if you want to sort both segments by order of appearance, I would do:
var orderedMonths = months.GroupBy(x => x < 7)
.OrderBy(group => group)
.SelectMany(x => x);
(or)
var orderedMonths = months.Where(x => x >= 7)
.Concat(months.Where(x => x < 7));
Assuming this is your sorted int array you could
int[] months = new int[4] { 1, 4, 7, 10 };
int value = 10;
int[] chk1 = new int[4];
chk1 = months.SkipWhile(a => a != value).
Concat(months.TakeWhile(a => a != value)).ToArray();
This should get you the required order
Can you use a list?
int NumberToBeFound = 7;
int IndexOfNumber = -1;
for(int i=0;i<months.count;i++){
if(months[i] == NumberToBeFound){
IndexOfNumber = i;
break;
}
}
List<int> Sorted = new List<int>();
for(int i = IndexOfNumber; i < months.count;i++){
Sorted.Add(months[i]);
}
for(int i = 0; i < IndexOfNumber; i++){
Sorted.Add(months[i]);
}
months = Sorted.ToArray();