Snowwhite 70/100
Здравейте,
Някой може ли да помогне със сортировката на задачата?
Условието е:
Snow White loves her dwarfs, but there are so many and she doesn't know how to order them. Does she order them by name? Or by color of their hat? Or by physics? She can't decide, so its up to you to write a program that does it for her.
You will be receiving several input lines which contain data about dwarfs in the following format:
{dwarfName} <:> {dwarfHatColor} <:> {dwarfPhysics}
The dwarfName and the dwarfHatColor are strings. The dwarfPhysics is an integer.
You must store the dwarfs in your program. There are several rules though:
- If 2 dwarfs have the same name but different color, they should be considered different dwarfs, and you should store both of them.
- If 2 dwarfs have the same name and the same color, store the one with the higher physics.
When you receive the command "Once upon a time", the input ends. You must order the dwarfs by physics in descending order and then by total count of dwarfs with the same hat color in descending order.
Then you must print them all.
Input
- The input will consists of several input lines, containing dwarf data in the format, specified above.
- The input ends when you receive the command "Once upon a time".
Output
- As output you must print the dwarfs, ordered in the way , specified above.
- The output format is: ({hatColor}) {name} <-> {physics}
Constraints
- The dwarfName will be a string which may contain any ASCII character except ' ' (space), '<', ':', '>'.
- The dwarfHatColor will be a string which may contain any ASCII character except ' ' (space), '<', ':', '>'.
- The dwarfPhysics will be an integer in range [0, 231 – 1].
- There will be no invalid input lines.
- If all sorting criteria fail, the order should be by order of input.
- Allowed working time / memory: 100ms / 16MB.
Examples
Input |
Output |
Pesho <:> Red <:> 2000 Tosho <:> Blue <:> 1000 Gosho <:> Green <:> 1000 Sasho <:> Yellow <:> 4500 Prakasho <:> Stamat <:> 1000 Once upon a time |
(Yellow) Sasho <-> 4500 (Red) Pesho <-> 2000 (Blue) Tosho <-> 1000 (Green) Gosho <-> 1000 (Stamat) Prakasho <-> 1000 |
Pesho <:> Red <:> 5000 Pesho <:> Blue <:> 10000 Pesho <:> Red <:> 10000 Gosho <:> Blue <:> 10000 Once upon a time |
(Blue) Pesho <-> 10000 (Blue) Gosho <-> 10000 (Red) Pesho <-> 10000
|
items = input() dwrafs = {} while items != "Once upon a time": tokens = items.split(" <:> ") name = tokens[0] color = tokens[1] physics = int(tokens[2]) id = name + ":" + color if id not in dwrafs: dwrafs[id] = 0 dwrafs[id] = max([dwrafs[id], physics]) items = input() sorted_dwrafs = dict(sorted(dwrafs.items(), key=lambda x: x[1], reverse=True)) for key, value in sorted_dwrafs.items(): tokens = key.split(":") print(f"({tokens[1]}) {tokens[0]} <-> {value}")
Благодаря Ви за съдействието
Бихте ли обяснили как colors[x[1][1]] работи точно? Опитах се с nested dicts да реша задачката, но точно заради тези шапки минах на друга логика.
@vigyriousx
x се достъпва като двумерен масив: x[ id, [physics, color]]
x[0] връща id-то
x[1] ни дава достъп до елеметите на [physics, color]
x[1][0] връща първия елемент, който в случая е physics
x[1][1] връща втория елемент, който в случая е color
С други думи, colors[x[1][1]] ще върне броя джуджета с цвета на джуджето, което се сортира в момента.
Благодаря!