04 SoftUni Beer Pong Lambda and LINQ - Exercises Problem: Sorting Nested Dicitonary with ThenBy
Здравейте
Може ли някой да обясни защо на този код последния сортинг не сортира :
.ThenByDescending(x => x.Value.Values) ?
Няма ли вариант да реша проблема без да презаписвам dictionary-to..?
foreach (KeyValuePair<string,Dictionary<string,int>> item in myDictionary
.Where(x => myDictionary[x.Key].Count > 2 )
.OrderByDescending(x => x.Value.Sum(h => x.Value[h.Key]))
.ThenByDescending(x => x.Value.Values)
)
Result:
Pesho|TechModule|32
Kaloyan|TechModule|14
Meto|TechModule|24
Pesho|Destroyers|20
Gosho|Destroyers|35
Stefan|Destroyers|12
expected:
Pesho|TechModule|32
Meto|TechModule|24
Kaloyan|TechModule|14
Gosho|Destroyers|35
Pesho|Destroyers|20
Stefan|Destroyers|12
-------------
Задача.
In SoftUni it is time for the annual beer pong tournament. Last year the competition was a total disaster, because nobody was in “condition” to keep track of the scores. Now it is your job to write a program, which will keep track of the total results. The competition’s rules are simple:
- contestants are divided into teams
- Every team should have exactly 3 participants
- After a team has a total of 3 participants, any further attempt to add a participant should be ignored.
- If a team has less than 3 participants – it gets disqualified and should not be printed.
Until you receive the command “stop the game” you will receive lines of input in the following format:
“team|player|pointsMade”
The total score of the team is the sum of the points made from every player.
Print every team, which has enough players in the following format:
{teamPosition}. {teamName}; Players: ###{firstPlayerName}: {points} ###{secondPlayerName}: {points} ###{thirdPlayerName}: {points} |
Order the teams by total points in descending order and order each team’s players by the amount of points made, again in descending order.
Constraints
- You will not receive a player’s name, which already exists in the team it’s trying to be inserted into.
- Points will be in the interval [0…2147483647].
- The total points of the teams and the points of the players in the same team will not be equal.
Качих го:)
Problema koito me muchi e che iskam da napravq sortiraneto edin put v purviq foreach kato printiram a ne na na 2-riq..
Най- добре според мен е първо да извадиш сбора от резултата на отборите //.OrderByDescending(a => a.Value.Sum(p => p.Value)) , а след това в foreacha да завъртиш втори foreach за вътрешното Dictionary и да подредиш самите участници //.OrderByDescending(k => k.Value).
Така както ти си ги направил ще ти подреди отбора с най- много точки и ако има два отбора с еднакви точки чак тогава ще влезеш в //.ThenByDescending(x => x.Value.Values) .
Питай, ако има нещо неясно или ако се затрудняваш ми прати твое решение, за да го обсъдим.
Taka i ne namerih reshenie da go napravq na edin put sortiraneto no naistina e po dobre da go sloja v vtoriq foreach kakto ti predloji.
Sled kato gledah reshenieto ot lektora si napravih 100% no edinstvenata razlika ot moeto reshenie e nachina po koito iztrivam element ot dictionary.
Moje li da popitam ako i se zanimvava zashto stava tova?
Shte kacha reshenieto koeto vadi 80 % i tova s 100%. Sled analiz edinstvenoto koeto promenqm po koda ot 80 % da ide na 100 % e nachinna na delete na dictionary.. ili tezi 3 reda:
exlain: Kogato pravq flatten na nqkoi element, sled kato dobavq vsichki elementi v flattened dict nakraq trqbva da delete key ot No-flattened dictionary . Probvah s 2 nachina da delete-na key ot No-flattened dict :
s 80 % :
normalProduct = normalProduct.Where(x => x.Key != type).ToDictionary(x => x.Key, y => y.Value);
normalProduct.Add(type, new Dictionary<string, string>());
s 100%
normalProduct.Remove(type);
normalProduct.Add(type, new Dictionary<string, string>());
Kato karam s debug, nqma vidima razlika kakvo se sluchva:(
80% solution: https://pastebin.com/fzwTxwNe
100% solution: https://pastebin.com/gm0FtQBZ