C
C#•7mo ago
Grafi

LINQ

A sequence of non-empty strings of Latin alphabet characters stringList is given. Among all strings of the same length, sorted in ascending order, select the last character from each string, converting it to uppercase, and from the received characters, compose a string. Arrange the resulting sequence of strings in descending order of their lengths.
46 Replies
Grafi
GrafiOP•7mo ago
I have been trying to solve this for quite some time to no avail, need help!
cathei
cathei•7mo ago
Sounds like homework. what have you tried?
Grafi
GrafiOP•7mo ago
It is, The closest I have got was this return stringList .GroupBy(s => s.Length) .OrderByDescending(g => g.Key) .Select(g => string.Concat(g.Select(s => char.ToUpper(s.Last())))) .OrderByDescending(s => s.Length); But in this version they are ordered according to ascending list when they should be descending
cathei
cathei•7mo ago
So do OrderBy instead of OrderByDescending ?
Grafi
GrafiOP•7mo ago
Nahh now the resoult isnt even simialr to expected one
cathei
cathei•7mo ago
I guess you want to sort the group tho Individually g.Key is length not string
Grafi
GrafiOP•7mo ago
STEP 1: Select the last character from each string and convert it to uppercase STEP 2: Order the original list in descending order STEP 3: Create new strings by combining the last characters from step 1 in order from step 2. Thats how i see the task, im quite new to this so i don't know the syntax that well
cathei
cathei•7mo ago
Again you want to sort individual group by string, not groups by length like you are doing rn i.e. You'd want to sort each g
Grafi
GrafiOP•7mo ago
they should be soretd by strings length stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" });
cathei
cathei•7mo ago
That's for results Which is .OrderByDescending(s => s.Length) there What is wrong is .OrderByDescending(g => g.Key) You are basically sorting the IEnumerable<IEnumerable<string>> , not individual IEnumerable<string>
Grafi
GrafiOP•7mo ago
return stringList .GroupBy(s => s.Length) .Select(g => string.Concat(g.OrderBy(s => s).Select(s => char.ToUpper(s.Last())))) .OrderByDescending(s => s.Length); ? Btw how do you darken the code part?
cathei
cathei•7mo ago
Try and see with backticks `
Grafi
GrafiOP•7mo ago
Okey, the first one works
cathei
cathei•7mo ago
what's the first one
Grafi
GrafiOP•7mo ago
yield return ( stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" }); yield return ( stringList: new[] { "du", "the", "ace", "run", "cut" }, expected: new[] { "ETNE", "U" }); yield return ( stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" }, expected: new[] { "B", "E", "E", "M", "T" }); yield return ( stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" }, expected: new[] { "YTR", "D", "T", "E", "R" });
cathei
cathei•7mo ago
And what doesn't work?
Grafi
GrafiOP•7mo ago
yield return ( stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" }); yield return ( stringList: new[] { "du", "the", "ace", "run", "cut" }, expected: new[] { "ETNE", "U" }); yield return ( stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" }, expected: new[] { "B", "E", "E", "M", "T" }); yield return ( stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" }, expected: new[] { "YTR", "D", "T", "E", "R" }); this the orignal but it doesnt make sense be cose it has T in swim yield return ( stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" }); yield return ( stringList: new[] { "du", "the", "ace", "run", "cut" }, expected: new[] { "ETNE", "U" }); yield return ( stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" }, expected: new[] { "B", "E", "E", "T", "M" }); yield return ( stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" }, expected: new[] { "YTR", "D", "T", "E", "R" });
cathei
cathei•7mo ago
$code
MODiX
MODiX•7mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Grafi
GrafiOP•7mo ago
cs // yield return ( stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" }); yield return ( stringList: new[] { "du", "the", "ace", "run", "cut" }, expected: new[] { "ETNE", "U" }); yield return ( stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" }, expected: new[] { "B", "E", "E", "T", "M" }); yield return ( stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" }, expected: new[] { "YTR", "D", "T", "E", "R" });
cathei
cathei•7mo ago
$codegif
Grafi
GrafiOP•7mo ago
yield return (
stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" },
expected: new[] { "CCD", "RF" });
yield return (
stringList: new[] { "du", "the", "ace", "run", "cut" },
expected: new[] { "ETNE", "U" });
yield return (
stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" },
expected: new[] { "B", "E", "E", "T", "M" });
yield return (
stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" },
expected: new[] { "YTR", "D", "T", "E", "R" })
yield return (
stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" },
expected: new[] { "CCD", "RF" });
yield return (
stringList: new[] { "du", "the", "ace", "run", "cut" },
expected: new[] { "ETNE", "U" });
yield return (
stringList: new[] { "ab", "attribute", "cheese", "swim", "cut" },
expected: new[] { "B", "E", "E", "T", "M" });
yield return (
stringList: new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" },
expected: new[] { "YTR", "D", "T", "E", "R" })
cathei
cathei•7mo ago
Better
Grafi
GrafiOP•7mo ago
4th one
cathei
cathei•7mo ago
Anyways I wouldn't say it is "wrong" since the order is descending by length and they didn't mention tiebreaker It's just undefined there
Grafi
GrafiOP•7mo ago
I mean it's strange because i have to take last character, but now it expects T but returns M So in place of T i have an M, I mean if the first case worked why not here
cathei
cathei•7mo ago
I'd say "B" "E" "E" "M" "T" is preferrable as it maintains the order 😛
Grafi
GrafiOP•7mo ago
cs return stringList
.GroupBy(s => s.Length)
.Select(g => string.Concat(g.OrderBy(s => s).Select(s => char.ToUpper(s.Last()))))
.OrderByDescending(s => s.Length);
cs return stringList
.GroupBy(s => s.Length)
.Select(g => string.Concat(g.OrderBy(s => s).Select(s => char.ToUpper(s.Last()))))
.OrderByDescending(s => s.Length);
cathei
cathei•7mo ago
But does not matter by the spec They both right Whoever made that test case did the bad job
Grafi
GrafiOP•7mo ago
no even if I change this one the next one is even worse
cathei
cathei•7mo ago
That also doesn't matter As long as "YTR" is the first element The rest is same length so order doesn't matter there
Grafi
GrafiOP•7mo ago
But wait it doesn't make sense, the task states that the lists are ordered in ascending order which they are clearly are not
cathei
cathei•7mo ago
I assume that's just saying you need to sort it which you did
Grafi
GrafiOP•7mo ago
Then what's the issue
cathei
cathei•7mo ago
That they did the bad job making test case?
Grafi
GrafiOP•7mo ago
Nahh, there has to be a solution which satisfies the parameters
cathei
cathei•7mo ago
Ehhh good luck, not an interesting guessing game
Grafi
GrafiOP•7mo ago
Okey I have changed the test case lets see if it goes thru
cathei
cathei•7mo ago
{ "YTR", "D", "T", "E", "R" }
{ "YTR", "D", "R", "E", "T" }
{ "YTR", "E", "D", "T", "R" }
{ "YTR", "D", "T", "E", "R" }
{ "YTR", "D", "R", "E", "T" }
{ "YTR", "E", "D", "T", "R" }
Whatever those all should be considered "correct" when requirement is just "Arrange the resulting sequence of strings in descending order of their lengths."
Grafi
GrafiOP•7mo ago
Alright thanks Yo, I have figure it out, if multiple groups have the same length they should maintain their current order which is alphabetical, the problem is i have got no cluq how to implemnt it using only System.Linq
cathei
cathei•7mo ago
So they do want you to sort things alphabetical order first? Well take that OrderBy out of Select and put it before GroupBy then Probably should've done that in the first place
Grafi
GrafiOP•7mo ago
nahh I do not belive this whole this time
Grafi
GrafiOP•7mo ago
No description
Grafi
GrafiOP•7mo ago
God bless you and your soul
cathei
cathei•7mo ago
Lol well done
Want results from more Discord servers?
Add your server