SimeonTs

SUPyF2 Dict-Exercise - 03. Legendary Farming

Oct 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#2
  4.  
  5. SUPyF2 Dict-Exercise - 03. Legendary Farming
  6.  
  7. Problem:
  8. You've done all the work and the last thing left to accomplish is to own a legendary item.
  9. However, it's a tedious process and it requires quite a bit of farming.
  10. Anyway, you are not too pretentious – any legendary item will do. The possible items are:
  11. • Shadowmourne – requires 250 Shards;
  12. • Valanyr – requires 250 Fragments;
  13. • Dragonwrath – requires 250 Motes;
  14. Shards, Fragments and Motes are the key materials and everything else is junk.
  15. You will be given lines of input, in the format:
  16. 2 motes 3 ores 15 stones
  17. Keep track of the key materials - the first one that reaches the 250 mark, wins the race.
  18. At that point you have to print that the corresponding legendary item is obtained.
  19. Then, print the remaining shards, fragments, motes, ordered by quantity in descending order,
  20. then by name in ascending order, each on a new line. Finally, print the collected junk items in alphabetical order.
  21. Input
  22. • Each line comes in the following format: {quantity} {material} {quantity} {material} … {quantity} {material}
  23. Output
  24. • On the first line, print the obtained item in the format: {Legendary item} obtained!
  25. • On the next three lines, print the remaining key materials in descending order by quantity
  26. o   If two key materials have the same quantity, print them in alphabetical order
  27. • On the final several lines, print the junk items in alphabetical order
  28. o   All materials are printed in format {material}: {quantity}
  29. o   The output should be lowercase, except for the first letter of the legendary
  30.  
  31. Examples:
  32. Input:
  33. 3 Motes 5 stones 5 Shards
  34. 6 leathers 255 fragments 7 Shards
  35.  
  36. Output:
  37. Valanyr obtained!
  38. fragments: 5
  39. shards: 5
  40. motes: 3
  41. leathers: 6
  42. stones: 5
  43.  
  44. Input:
  45. 123 silver 6 shards 8 shards 5 motes
  46. 9 fangs 75 motes 103 MOTES 8 Shards
  47. 86 Motes 7 stones 19 silver
  48.  
  49. Output:
  50. Dragonwrath obtained!
  51. shards: 22
  52. motes: 19
  53. fragments: 0
  54. fangs: 9
  55. silver: 123
  56. """
  57.  
  58. materials = {
  59.     "shards": 0,
  60.     "fragments": 0,
  61.     "motes": 0,
  62. }
  63. junk = {}
  64.  
  65. legendary_item = None
  66.  
  67. while True:
  68.     if legendary_item:
  69.         break
  70.     data = [item for item in input().split()]
  71.     for i in range(0, len(data), 2):
  72.         material = data[i + 1].lower()
  73.         quantity = int(data[i])
  74.         if material != "shards" and material != "fragments" and material != "motes":
  75.             if material not in junk:
  76.                 junk[material] = quantity
  77.             else:
  78.                 junk[material] += quantity
  79.  
  80.         else:
  81.             materials[material] += quantity
  82.  
  83.             if materials["shards"] >= 250:
  84.                 legendary_item = "Shadowmourne"
  85.                 materials["shards"] -= 250
  86.                 break
  87.             if materials["fragments"] >= 250:
  88.                 legendary_item = "Valanyr"
  89.                 materials["fragments"] -= 250
  90.                 break
  91.             if materials["motes"] >= 250:
  92.                 legendary_item = "Dragonwrath"
  93.                 materials["motes"] -= 250
  94.                 break
  95.  
  96. print(f"{legendary_item} obtained!")
  97.  
  98. for item, value in sorted(materials.items(), key=lambda x: (-x[1], x[0])):
  99.     print(f"{item}: {value}")
  100. for item, value in sorted(junk.items(), key=lambda x: x):
  101.     print(f"{item}: {value}")
Add Comment
Please, Sign In to add comment