Advertisement
SimeonTs

SUPyF2 Dict-More-Ex. - 05. Dragon Army

Oct 26th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.32 KB | None | 0 0
  1. """
  2. Dictionaries - More Exercises
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1738#4
  4.  
  5. SUPyF2 Dict-More-Ex. - 05. Dragon Army
  6.  
  7. Problem:
  8. Heroes III is the best game ever. Everyone loves it and everyone plays it all the time.
  9. Stamat is no exclusion to this rule. His favorite units in the game are all types of dragons –
  10. black, red, gold, azure etc… He likes them so much that he gives them names and keeps logs of their stats:
  11. damage, health and armor. The process of aggregating all the data is quite tedious,
  12. so he would like to have a program doing it. Since he is no programmer, it's your task to help him.
  13. You need to categorize dragons by their type. For each dragon, identified by name, keep information about his stats.
  14. Type is preserved as in the order of input, but dragons are sorted alphabetically by name.
  15. For each type, you should also print the average damage, health and armor of the dragons.
  16. For each dragon, print his own stats.
  17. There may be missing stats in the input, though. If a stat is missing you should assign it default values.
  18. Default values are as follows: health 250, damage 45, and armor 10. Missing stat will be marked by null.
  19. The input is in the following format {type} {name} {damage} {health} {armor}.
  20. Any of the integers may be assigned null value. See the examples below for better understanding of your task.
  21. If the same dragon is added a second time, the new stats should overwrite the previous ones.
  22. Two dragons are considered equal if they match by both name and type.
  23. Input
  24. • On the first line, you are given number N -> the number of dragons to follow
  25. • On the next N lines, you are given input in the above described format.
  26. There will be single space separating each element.
  27. Output
  28. • Print the aggregated data on the console
  29. • For each type, print average stats of its dragons in format {Type}::({damage}/{health}/{armor})
  30. • Damage, health and armor should be rounded to two digits after the decimal separator
  31. • For each dragon, print its stats in format -{Name} -> damage: {damage}, health: {health}, armor: {armor}
  32. Constraints
  33. • N is in range [1…100]
  34. • The dragon type and name are one word only, starting with capital letter.
  35. • Damage health and armor are integers in range [0 … 100000] or null
  36.  
  37. Examples:
  38. Input:
  39. 5
  40. Red Bazgargal 100 2500 25
  41. Black Dargonax 200 3500 18
  42. Red Obsidion 220 2200 35
  43. Blue Kerizsa 60 2100 20
  44. Blue Algordox 65 1800 50
  45.  
  46. Output:
  47. Red::(160.00/2350.00/30.00)
  48. -Bazgargal -> damage: 100, health: 2500, armor: 25
  49. -Obsidion -> damage: 220, health: 2200, armor: 35
  50. Black::(200.00/3500.00/18.00)
  51. -Dargonax -> damage: 200, health: 3500, armor: 18
  52. Blue::(62.50/1950.00/35.00)
  53. -Algordox -> damage: 65, health: 1800, armor: 50
  54. -Kerizsa -> damage: 60, health: 2100, armor: 20
  55.  
  56. Input:
  57. 4
  58. Gold Zzazx null 1000 10
  59. Gold Traxx 500 null 0
  60. Gold Xaarxx 250 1000 null
  61. Gold Ardrax 100 1055 50
  62.  
  63. Output:
  64. Gold::(223.75/826.25/17.50)
  65. -Ardrax -> damage: 100, health: 1055, armor: 50
  66. -Traxx -> damage: 500, health: 250, armor: 0
  67. -Xaarxx -> damage: 250, health: 1000, armor: 10
  68. -Zzazx -> damage: 45, health: 1000, armor: 10
  69. """
  70.  
  71. # -------------------------------------------------------------------------------------------------------
  72. # There are two ways to sort This problem one of them will be to use Objects/Classes and that is it:
  73. # -------------------------------------------------------------------------------------------------------
  74.  
  75. """
  76. all_dragons = []
  77. type_dragons = []
  78.  
  79.  
  80. class Dragon:
  81.    def __init__(self, type_dragon: str, name: str, damage, health, armor):
  82.        self.type_dragon = type_dragon
  83.        self.name = name
  84.        self.damage = damage
  85.        self.health = health
  86.        self.armor = armor
  87.        self.add_dragon()
  88.  
  89.    def add_dragon(self):
  90.        global all_dragons
  91.        global type_dragons
  92.        dragon_in_all_dragons = False
  93.        for dragon in all_dragons:
  94.            if dragon.name == self.name:
  95.                if dragon.type_dragon == self.type_dragon:
  96.                    dragon_in_all_dragons = True
  97.                    dragon.damage = self.damage
  98.                    dragon.health = self.health
  99.                    dragon.armor = self.armor
  100.        if not dragon_in_all_dragons:
  101.            all_dragons += [self]
  102.        if self.type_dragon not in type_dragons:
  103.            type_dragons += [self.type_dragon]
  104.  
  105.    @property
  106.    def damage(self):
  107.        return self.__damage
  108.  
  109.    @damage.setter
  110.    def damage(self, value):
  111.        if value != "null":
  112.            self.__damage = int(value)
  113.        else:
  114.            self.__damage = 45
  115.  
  116.    @property
  117.    def health(self):
  118.        return self.__health
  119.  
  120.    @health.setter
  121.    def health(self, value):
  122.        if value != "null":
  123.            self.__health = int(value)
  124.        else:
  125.            self.__health = 250
  126.  
  127.    @property
  128.    def armor(self):
  129.        return self.__armor
  130.  
  131.    @armor.setter
  132.    def armor(self, value):
  133.        if value != "null":
  134.            self.__armor = int(value)
  135.        else:
  136.            self.__armor = 10
  137.  
  138.  
  139. for each_time in range(int(input())):
  140.    data = input().split()
  141.    dg = Dragon(data[0], data[1], data[2], data[3], data[4])
  142.  
  143. for type_dr in type_dragons:
  144.    current_types = []
  145.    current_total_damage = 0
  146.    current_total_health = 0
  147.    current_total_armor = 0
  148.    for dr in all_dragons:
  149.        if dr.type_dragon == type_dr:
  150.            current_types += [dr]
  151.            current_total_damage += dr.damage
  152.            current_total_health += dr.health
  153.            current_total_armor += dr.armor
  154.    average_damage = current_total_damage / len(current_types)
  155.    average_health = current_total_health / len(current_types)
  156.    average_armor = current_total_armor / len(current_types)
  157.    print(f"{type_dr}::({average_damage:.2f}/{average_health:.2f}/{average_armor:.2f})")
  158.    for d in sorted(current_types, key=lambda x: x.name):
  159.        print(f"-{d.name} -> damage: {d.damage}, health: {d.health}, armor: {d.armor}")
  160. """
  161.  
  162. # -------------------------------------------------------------------------------------------------------
  163. # The Second way will be to use Dictionaries and that is it:
  164. # -------------------------------------------------------------------------------------------------------
  165.  
  166. all_dragons = {}
  167.  
  168. for how_many_times in range(int(input())):
  169.     n_d_t, n_d_n, n_d_d, n_d_h, n_d_a = input().split()
  170.  
  171.     if n_d_d == "null":
  172.         n_d_d = 45
  173.     if n_d_h == "null":
  174.         n_d_h = 250
  175.     if n_d_a == "null":
  176.         n_d_a = 10
  177.  
  178.     if n_d_t not in all_dragons:
  179.         all_dragons[n_d_t] = {
  180.             n_d_n: [int(n_d_d), int(n_d_h), int(n_d_a)]}
  181.     else:
  182.         all_dragons[n_d_t].update({
  183.             n_d_n: [int(n_d_d), int(n_d_h), int(n_d_a)]})
  184.  
  185. for dragon_type, all_dragons in all_dragons.items():
  186.     total_damage, total_health, total_armor = 0, 0, 0
  187.     for dragon_name, dragon_data in all_dragons.items():
  188.         total_damage += dragon_data[0]
  189.         total_health += dragon_data[1]
  190.         total_armor += dragon_data[2]
  191.     all_dr = len(all_dragons)
  192.     print(f"{dragon_type}::({(total_damage / all_dr):.2f}/{(total_health / all_dr):.2f}/{(total_armor / all_dr):.2f})")
  193.     for dragon_name, data in sorted(all_dragons.items(), key=lambda x: x):
  194.         print(f"-{dragon_name} -> damage: {data[0]}, health: {data[1]}, armor: {data[2]}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement