dividing a string array in groups - c#

I have a string array of 3000 words. How can I divide the array into groups of ten using LINQ. Each ten items should be stored in one variable. The result should be a new array containing the groups.

Assuming that the words are separated by single space, you can split and re-group like this:
var res = longWord
.Split(' ').
.Select((s, i) => new { Str = s, Index = i })
.GroupBy(p => p.Index / 10)
.Select(g => string.Join(" ", g.Select(v => v.Str)));

Related

A better way to count and sort by frequency?

I have a string liste like this
title1;duration1
title2;duration2
title1;duration3
Which means that the title was shown for duration milliseconds to be replaced by the next title for the next duration.
title can repeat itself.
The goal is to look for each title that is the same, to then add its duration to then create a list of all distinct titles sorted descendingly by their sum of durations.
My approach:
string[] units = liste.split('\n');
Dictionary<string, long> d = new Dictionary<string, long>();
foreach(var row in units)
{
string[] e = row.split(';');
//if e[0] in d => add e[1] to d[e[0]] else set d[e[0]] to e[1]
}
//Convert d to list and sort descendingly by long.
Is there a better way?
I'm not necessarily suggesting this is the best way because it is kind of incomprehensible and maintainable code is important, but you can obtain your result in a single statement with LINQ. This solution assumes you have confidence in your data being clean - meaning no blank values or values that don't convert to double, etc.
split the string on newline
project an object for each line and substring at ";"
Group by title
project again into a new list that sums the groupings
Finally sort the list.
string liste = #"title1;8.91
title2; 3
title1; 4.5";
var result = liste.Split('\n')
.Select(l => new {
title = l.Substring(0, l.IndexOf(';')).Trim(),
duration = l.Substring(l.IndexOf(';')+1, l.Length - (l.IndexOf(';')+1)).Trim()
})
.GroupBy(l => l.title)
.Select(l => new { title = l.Key, durations = l.Sum(m => double.Parse(m.duration))})
.OrderByDescending(l => l.durations);
Use linq :
string input = "title1;10\n" +
"title2;20\n" +
"title1;30";
var rows = input.Split(new char[] {'\n'}).Select(x => x.Split(new char[] {';'})).Select(y => new {title = y.First(), duration = int.Parse(y.Last())}).ToList();
var sums = rows.GroupBy(x=> x.title).Select(x => new {title = x.Key, duration = x.Sum(y => y.duration)}).ToList();

How to get the element of array with Max Value?

I'm trying to get the letter of an array that as a max value of a repeated letters on a string.
I have is this:
var AsciiCode = new int[255];
string word= "Hello everybody";
foreach (char c in word)
{
AsciiCode[c]++;
}
MessageBox.Show(string.Format("The max count is:
{0}\nLetter: {1}", AsciiCode.Max(), AsciiCode.ElementAt(//MAX_VALUE_HERE//) ));
A solution with using Linq can be this:
var res =
word.GroupBy(g => g)
.Select(c => new { c.Key, Count = c.Count() })
.OrderByDescending(o => o.Count)
.FirstOrDefault();
C# Demo

How to convert IEnumerable<IEnumerable<IGrouping<int,string>>> to IEnumerable<IEnumerable<string>>

I'm trying to partition some comma separated lines into groups of size 2 at max.
How can i convert the collection of groups to list of lists as below?
I expect the partitions to be 3 first and then 4 after grouping.
List<string> chunk = new List<string>()
{
"a,b,c",
"a,d,e",
"b,c,d",
"b,e,d",
"b,f,g",
"e"
};
var partitons = chunk.GroupBy(c => c.Split(',')[0], (key, g) => g);
var groups = partitons.Select(x => x.Select((i, index) => new { i, index }).GroupBy(g => g.index / 2, e => e.i));
IEnumerable<IEnumerable<string>> parts = groups.Select(???)
This is what I wanted
var parts = groups.SelectMany(x => x).Select(y => y.Select(z => z));
Try this:
partitons = groups.Select(x => x.SelectMany(y => y));
I get this:

How i can split comma separated string in a group using LINQ

I have the string of comma separated ids like 1,2,3,4,5,6,7,8,9...... etc.
Please suggest how i can split them in group of "Quantity" means if Quantity=3 then group are (List) ["1,2,3"], ["4,5,6"], ["7,8,9"] etc.
Range of Quantity is from 1-75.
Try this:
var quantity = 3;
yourList.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / quantity )
.Select(x => x.Select(v => v.Value).ToList())
.ToList();

How to compare 2 list by characters content and its correspondents double values?

I have 2 lists: a string list and a double list with same length and with same index of correspondence. I need to compare all the strings, find the indexes of the list that has the same characters, independent of its order, and delete the highest double value that corresponds to both,
Example:
List<string> str= new List<string>();
str.add("efc");
str.add("abc");
str.add("cde");
str.add("cab");
str.add("fbc");
List<double> vlr= new List<double>();
vlr.add(0.1);
vlr.add(0.5);
vlr.add(0.4);
vlr.add(0.2);
vlr.add(0.3);
and this case, "abc" => (0.5) must be deleted because "cab" has the same characters AND lower correspondent value =>(0.2).
There is a lambda expression for this 2 arrays??
What I've tried:
var distinct = list .Select((str, idx) => new { Str = str, Idx = idx })
.GroupBy(pair => new HashSet<char>(pair.Str), HashSet<char>.CreateSetComparer())
.Select(grp => grp.OrderBy(p => p.Idx).First())
.ToList();
Here's one way to solve it:
// Pair the strings with their correspondence values
var pairs = str.Zip(vlr, (s, d) => new {s, d});
// Group using a sorted string, eliminating differences due to character order
var groups = pairs.GroupBy(x => new string(x.s.ToCharArray().OrderBy(c => c).ToArray()));
// For each group, retain the item with the lowest correspondence value
var filtered = groups.Select(x => x.OrderBy(y => y.d).First().s);
var newDict = str.Zip(vlr, (s, d) => new { s, d })
.GroupBy(x => String.Join("", x.s.OrderBy(y => y)))
.Select(g => g.OrderBy(x => x.d).First())
.ToDictionary(x => x.s, x => x.d);
here is the code:
var group = str.GroupBy(s => string.Join("", s.ToCharArray().OrderBy(c => c)));
var _vlr = group.Select(g => g.Min(s => vlr[str.IndexOf(s)]));
var _str = group.Select(g => g.OrderBy(s => vlr[str.IndexOf(s)]).First());
and the result:

Categories