SimeonTs

SUPyF2 Dict-More-Ex. - 04. Snowwhite

Oct 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. """
  2. Dictionaries - More Exercises
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1738#3
  4.  
  5. SUPyF2 Dict-More-Ex. - 04. Snowwhite
  6.  
  7. Problem:
  8. Snow White loves her dwarfs, but there are so many and she doesn't know how to order them.
  9. Does she order them by name? Or by color of their hat? Or by physics? She can't decide,
  10. so its up to you to write a program that does it for her.
  11. You will be receiving several input lines which contain data about dwarfs in the following format:
  12. {dwarfName} <:> {dwarfHatColor} <:> {dwarfPhysics}
  13. The dwarfName and the dwarfHatColor are strings. The dwarfPhysics is an integer.
  14. You must store the dwarfs in your program. There are several rules though:
  15. • If 2 dwarfs have the same name but different color, they should be considered different dwarfs,
  16. and you should store both of them.
  17. • If 2 dwarfs have the same name and the same color, store the one with the higher physics.
  18. When you receive the command "Once upon a time", the input ends.
  19. You must order the dwarfs by physics in descending order and then by total count of dwarfs with the same hat color
  20. in descending order.
  21. Then you must print them all.
  22. Input
  23. • The input will consists of several input lines, containing dwarf data in the format, specified above.
  24. • The input ends when you receive the command "Once upon a time".
  25. Output
  26. • As output you must print the dwarfs, ordered in the way , specified above.
  27. • The output format is: ({hatColor}) {name} <-> {physics}
  28. Constraints
  29. • The dwarfName will be a string which may contain any ASCII character except ' ' (space), '<', ':', '>'.
  30. • The dwarfHatColor will be a string which may contain any ASCII character except ' ' (space), '<', ':', '>'.
  31. • The dwarfPhysics will be an integer in range [0, 231 – 1].
  32. • There will be no invalid input lines.
  33. • If all sorting criteria fail, the order should be by order of input.
  34. • Allowed working time / memory: 100ms / 16MB.
  35.  
  36. Examples:
  37. Input:
  38. Pesho <:> Red <:> 2000
  39. Tosho <:> Blue <:> 1000
  40. Gosho <:> Green <:> 1000
  41. Sasho <:> Yellow <:> 4500
  42. Prakasho <:> Stamat <:> 1000
  43. Once upon a time
  44.  
  45. Output:
  46. (Yellow) Sasho <-> 4500
  47. (Red) Pesho <-> 2000
  48. (Blue) Tosho <-> 1000
  49. (Green) Gosho <-> 1000
  50. (Stamat) Prakasho <-> 1000
  51.  
  52. Input:
  53. Pesho <:> Red <:> 5000
  54. Pesho <:> Blue <:> 10000
  55. Pesho <:> Red <:> 10000
  56. Gosho <:> Blue <:> 10000
  57. Once upon a time
  58.  
  59. Output:
  60. (Blue) Pesho <-> 10000
  61. (Blue) Gosho <-> 10000
  62. (Red) Pesho <-> 10000
  63. """
  64.  
  65.  
  66. class Dwarf:
  67.     def __init__(self, name: str, hat_color: str, physics: int):
  68.         self.name = name
  69.         self.hat_color = hat_color
  70.         self.physics = physics
  71.         self.color_count = 0
  72.  
  73.  
  74. all_dwarfs = []
  75. colors = {}
  76.  
  77. while True:
  78.     data = input().split(" <:> ")
  79.     if data[0] == "Once upon a time":
  80.         break
  81.     dwarf_name = data[0]
  82.     dwarf_hat_color = data[1]
  83.     dwarf_physics = int(data[2])
  84.  
  85.     dwarf_is_present = False
  86.     for dwarf in all_dwarfs:
  87.         if dwarf.name == dwarf_name and dwarf.hat_color == dwarf_hat_color:
  88.             dwarf_is_present = True
  89.             if dwarf_physics > dwarf.physics:
  90.                 dwarf.physics = dwarf_physics
  91.     if not dwarf_is_present:
  92.         new_dwarf = Dwarf(name=dwarf_name, hat_color=dwarf_hat_color, physics=dwarf_physics)
  93.         all_dwarfs.append(new_dwarf)
  94.     if dwarf_hat_color not in colors:
  95.         colors[dwarf_hat_color] = 1
  96.     elif dwarf_hat_color in colors and not dwarf_is_present:
  97.         colors[dwarf_hat_color] += 1
  98.  
  99. for dwarf in all_dwarfs:
  100.     for color, value in colors.items():
  101.         if dwarf.hat_color == color:
  102.             dwarf.color_count = value
  103.  
  104. for dwarf in sorted(all_dwarfs, key=lambda x: (-x.physics, -x.color_count)):
  105.     print(f"({dwarf.hat_color}) {dwarf.name} <-> {dwarf.physics}")
Add Comment
Please, Sign In to add comment