SimeonTs

SUPyF Exam 10.03.2019 - 04. Listmons Monsters

Aug 13th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.93 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1511#3
  4.  
  5. SUPyF Exam 10.03.2019 - 04. Listmons Monsters
  6.  
  7. Problem:
  8. Input / Constraints
  9. An evil genius never works alone.  Listmon has own army. He wants to start tracking data for his army.
  10. You have been asked to create a data base for Listmon's monsters.
  11. A basic monster will always  have:
  12. • name -  string of any asci character without space and comma
  13. • id  – integer number
  14. • strength - integer point number
  15. • ugliness - integer number
  16. In his army he accepts only two types of monsters: Hydralisks and Zerglings.
  17. Both types has the same parameters as a basic monsters but also:
  18. • Hydralisk has another property – range - string of any asci character without space and comma
  19. • Zergling has another property – speed – integer number
  20. Listmon does not get in his army basic monsters. They must be one of the two types mentioned above.
  21. You will start receiving input data in format:
  22. * Hydralisk({name}, {id}, {strength}, {ugliness}, {range})
  23. -if there is no fifth parameter you should print '__init__() missing 1 required positional argument: 'range''
  24. -if the fifth parameter is not a string you should print 'Range must be string'
  25. In both cases you do not add the monster in DB
  26. * Zergling({name}, {id}, {strength}, {ugliness}, {speed})
  27. -if there is no fifth parameter you should print '__init__() missing 1 required positional argument: 'speed''
  28. -if the fifth parameter is not a string you should print 'Speed must be integer’
  29. In both cases you do not add the monster in DB
  30. Is it possible a basic monster to try to apply in the army. If you receive a command:
  31. *BasicMonster({name}, {id}, { strength }, {ugliness})
  32. You must print ‘Can't instantiate abstract class BaseMonster with abstract methods __init__’
  33. and you must  not add it in the DB.
  34.  
  35.  
  36. Output
  37. When you receive a command ‘stopAddingArmy’ you must print the information about the army in the following format:
  38.  
  39.  Overall speed of army: {speed}
  40.  Overall strength: {strength}
  41.  Hydralisk: {count}; Zergling: {count}
  42. Where {speed} is the sum of all Zerglings monsters speed in DB;
  43. {strength} is the sum of strength of all monsters;
  44. {count} is the number of all Hydralisks/Zerglings in the DB
  45.  
  46.  
  47. Examples
  48.  
  49. Input:
  50. Zergling('Pesho', 10, 10, 10, 10)
  51. Zergling('Pesho', 10, 10, 10, 20)
  52. Hydralisk('a', 100, 100, 100, 'min')
  53. Zergling('Pesho', 10, 10, 10, 30)
  54. stopAddingArmy
  55.  
  56. Output:
  57. Overall speed of army: 60
  58. Overall stength: 130
  59. Hydralisk: 1; Zergling: 3
  60.  
  61. Input:
  62. BaseMonster('A12', 150, 200, 300)
  63. Zergling('Pesho', 10, 10, 10, 'min')
  64. Hydralisk('a', 100, 100, 100, 10)
  65. Zergling('Pesho', 10, 10, 10)
  66. Hydralisk('a', 100, 100, 100)
  67. stopAddingArmy
  68.  
  69. Output:
  70. Can't instantiate abstract class BaseMonster with abstract methods __init__
  71. Speed must be integer
  72. Range must be string
  73. __init__() missing 1 required positional argument: 'speed'
  74. __init__() missing 1 required positional argument: 'range'
  75. Overall speed of army: 0
  76. Overall stength: 0
  77. Hydralisk: 0; Zergling: 0
  78. """
  79.  
  80. from abc import ABC, abstractmethod
  81.  
  82.  
  83. class BaseMonster(ABC):
  84.     @abstractmethod
  85.     def __init__(self, name: str, id_: int, strength: int, ugliness: int):
  86.         self.name = str(name)
  87.         self.id_ = int(id_)
  88.         self.strength = int(strength)
  89.         self.ugliness = int(ugliness)
  90.  
  91.  
  92. class Hydralisk(BaseMonster):
  93.     def __init__(self, name, id_, strength, ugliness, range):
  94.         BaseMonster.__init__(self, name, id_, strength, ugliness)
  95.         self.range = range
  96.  
  97.     @property
  98.     def range(self):
  99.         return self.__range
  100.  
  101.     @range.setter
  102.     def range(self, value):
  103.         if not isinstance(value, str):
  104.             raise Exception('Range must be string')
  105.         else:
  106.             self.__range = value
  107.  
  108.  
  109. class Zergling(BaseMonster):
  110.     def __init__(self, name, id_, strength, ugliness, speed):
  111.         BaseMonster.__init__(self, name, id_, strength, ugliness)
  112.         self.speed = speed
  113.  
  114.     @property
  115.     def speed(self):
  116.         return self.__speed
  117.  
  118.     @speed.setter
  119.     def speed(self, value):
  120.         if not isinstance(value, int):
  121.             raise Exception('Speed must be integer')
  122.         else:
  123.             self.__speed = value
  124.  
  125.  
  126. overall_speed_of_army = 0
  127. overall_strength = 0
  128. count_hydralisk = 0
  129. count_zergling = 0
  130.  
  131. while True:
  132.     data = input()
  133.     if data == "stopAddingArmy":
  134.         break
  135.     try:
  136.         monster = eval(data)
  137.         overall_strength += int(monster.strength)
  138.         if monster.__class__.__name__ == "Hydralisk":
  139.             count_hydralisk += 1
  140.         elif monster.__class__.__name__ == "Zergling":
  141.             count_zergling += 1
  142.             overall_speed_of_army += int(monster.speed)
  143.     except Exception as e:
  144.         print(e)
  145.  
  146. print(f"Overall speed of army: {overall_speed_of_army}")
  147. print(f"Overall stength: {overall_strength}")
  148. print(f"Hydralisk: {count_hydralisk}; Zergling: {count_zergling}")
Add Comment
Please, Sign In to add comment