C
C#•6mo 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•6mo ago
I have been trying to solve this for quite some time to no avail, need help!
cathei
cathei•6mo ago
Sounds like homework. what have you tried?
Grafi
GrafiOP•6mo 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•6mo ago
So do OrderBy instead of OrderByDescending ?
Grafi
GrafiOP•6mo ago
Nahh now the resoult isnt even simialr to expected one
cathei
cathei•6mo ago
I guess you want to sort the group tho Individually g.Key is length not string
Grafi
GrafiOP•6mo 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•6mo 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•6mo ago
they should be soretd by strings length stringList: new[] { "bc", "sd", "ac", "sdf", "ewr" }, expected: new[] { "CCD", "RF" });
cathei
cathei•6mo 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•6mo 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•6mo ago
Try and see with backticks `
Grafi
GrafiOP•6mo ago
Okey, the first one works
cathei
cathei•6mo ago
what's the first one
Grafi
GrafiOP•6mo 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•6mo ago
And what doesn't work?
Grafi
GrafiOP•6mo 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•6mo ago
$code
MODiX
MODiX•6mo 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•6mo 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•6mo ago
$codegif
Grafi
GrafiOP•6mo 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•6mo ago
Better
Grafi
GrafiOP•6mo ago
4th one
cathei
cathei•6mo 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•6mo 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•6mo ago
I'd say "B" "E" "E" "M" "T" is preferrable as it maintains the order 😛
Grafi
GrafiOP•6mo 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•6mo ago
But does not matter by the spec They both right Whoever made that test case did the bad job
Grafi
GrafiOP•6mo ago
no even if I change this one the next one is even worse
cathei
cathei•6mo 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•6mo 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•6mo ago
I assume that's just saying you need to sort it which you did
Grafi
GrafiOP•6mo ago
Then what's the issue
cathei
cathei•6mo ago
That they did the bad job making test case?
Grafi
GrafiOP•6mo ago
Nahh, there has to be a solution which satisfies the parameters
cathei
cathei•6mo ago
Ehhh good luck, not an interesting guessing game
Grafi
GrafiOP•6mo ago
Okey I have changed the test case lets see if it goes thru
cathei
cathei•6mo 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•6mo 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•6mo 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•6mo ago
nahh I do not belive this whole this time
Grafi
GrafiOP•6mo ago
No description
Grafi
GrafiOP•6mo ago
God bless you and your soul
cathei
cathei•6mo ago
Lol well done
Want results from more Discord servers?
Add your server