Advertisement
SimeonTs

SUPyF Objects and Classes - 06. Animals

Aug 12th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. """
  2. Objects and Classes
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/950#5
  4.  
  5. SUPyF Objects and Classes - 06. Animals
  6.  
  7. Problem:
  8.  
  9. You have been given the task to create classes for several sophisticated animals.
  10. Create a class Dog which has a name (string), age (int) and number_of_legs (int).
  11. Create a class Cat which has a name (string), age (int) and intelligence_quotient (int).
  12. Create a class Snake which has a name (string), age(int) and cruelty_coefficient (int).
  13. Create a method in each class which is called produce_sound(). The method should print on the console a string depending
  14. on the class:
  15.    - If it’s a Dog, you should print “I'm a Distinguishedog, and I will now produce a distinguished sound! Bau Bau.”
  16.    - It it’s a Cat, you should print “I'm an Aristocat, and I will now produce an aristocratic sound! Myau Myau.”
  17.    - If it’s a Snake, you should print:
  18.        “I'm a Sophistisnake, and I will now produce a sophisticated sound! Honey, I'm home.”
  19. Now for the real deal. You will receive several input commands, which will register animals or make them produce sounds,
  20. until you receive the command “I’m your Huckleberry”.
  21. The commands will be in the following format:
  22. {class} {name} {age} {parameter}
  23. The class will be either “Dog”, “Cat” or “Snake”. The name will be a simple string,
  24. which can contain any ASCII character BUT space. The age will be an integer. The parameter, will be an integer.
  25. Depending on the class it would either be number of legs, IQ, or cruelty coefficient.
  26. Register each animal, and keep them in collections, by your choice, so that you can ACCESS THEM BY NAME.
  27. You will most likely need 3 collections, to store the different animals inside them.
  28. Between the register commands you might receive a command in the following format:
  29. talk {name}
  30. You must then make the animal with the given name, produce a sound.
  31.  
  32. When you receive the ending command, you should print every animal in the following format:
  33. • If it’s a Dog, you should print “Dog: {name}, Age: {age}, Number Of Legs: {numberOfLegs}”
  34. • It it’s a Cat, you should print “Cat: {name}, Age: {age}, IQ: {intelligenceQuotient}”
  35. • If it’s a Snake, you should print “Snake: {name}, Age: {age}, Cruelty: {crueltyCoefficient}”
  36. Print first the Dogs, then the Cats, and lastly – The Snakes.
  37.  
  38. Constraints
  39. -   You can assume that there will be no duplicate names (even in different animals).
  40. -   All input data will be valid. There will be no invalid input lines.
  41. -   The name in the talk command, will always be existent.
  42.  
  43. Examples:
  44. ------------------------------------------------------------------------------------
  45.    Input:
  46. Dog Sharo 3 4
  47. Cat Garfield 5 200
  48. Snake Alex 25 1000
  49. talk Sharo
  50. talk Garfield
  51. talk Alex
  52. I'm your Huckleberry
  53.    Output:
  54. I'm a Distinguishedog, and I will now produce a distinguished sound! Bau Bau.
  55. I'm an Aristocat, and I will now produce an aristocratic sound! Myau Myau.
  56. I'm a Sophistisnake, and I will now produce a sophisticated sound! Honey, I'm home.
  57. Dog: Sharo, Age: 3, Number Of Legs: 4
  58. Cat: Garfield, Age: 5, IQ: 200
  59. Snake: Alex, Age: 25, Cruelty: 1000
  60. ------------------------------------------------------------------------------------
  61.    Input:
  62. Dog Bau 5 10
  63. Cat Myau 5 100
  64. Dog Georgi 20 1000
  65. Cat Bojo 4 20
  66. talk Bojo
  67. I'm your Huckleberry
  68.    Output:
  69. I'm an Aristocat, and I will now produce an aristocratic sound! Myau Myau.
  70. Dog: Bau, Age: 5, Number Of Legs: 10
  71. Dog: Georgi, Age: 20, Number Of Legs: 1000
  72. Cat: Myau, Age: 5, IQ: 100
  73. Cat: Bojo, Age: 4, IQ: 20
  74. ------------------------------------------------------------------------------------
  75. """
  76.  
  77.  
  78. class Animal:
  79.     def __init__(self, type_of, name, age, extra):
  80.         self.type_of = type_of
  81.         self.name = name
  82.         self.age = age
  83.         self.extra = extra
  84.  
  85.     def produce_sound(self):
  86.         if self.type_of == "Dog":
  87.             return print("I'm a Distinguishedog, and I will now produce a distinguished sound! Bau Bau.")
  88.         elif self.type_of == "Cat":
  89.             return print("I'm an Aristocat, and I will now produce an aristocratic sound! Myau Myau.")
  90.         elif self.type_of == "Snake":
  91.             return print("I'm a Sophistisnake, and I will now produce a sophisticated sound! Honey, I'm home.")
  92.  
  93.     def animal_type_sort(self):
  94.         global animals, dogs, cats, snakes
  95.         animals += [animal]
  96.         if self.type_of == "Dog":
  97.             dogs += [animal]
  98.         elif self.type_of == "Cat":
  99.             cats += [animal]
  100.         elif self.type_of == "Snake":
  101.             snakes += [animal]
  102.  
  103.  
  104. animals = []
  105. dogs = []
  106. cats = []
  107. snakes = []
  108.  
  109. while True:
  110.     inp = input()
  111.     data = [item for item in inp.split(" ")]
  112.     if inp == "I'm your Huckleberry":
  113.         break
  114.     elif data[0] == "talk":
  115.         for animal in animals:
  116.             if animal.name == data[1]:
  117.                 animal.produce_sound()
  118.                 continue
  119.     else:
  120.         animal = Animal(data[0], data[1], data[2], data[3])
  121.         animal.animal_type_sort()
  122.  
  123. for dog in dogs:
  124.     print(f"Dog: {dog.name}, Age: {dog.age}, Number Of Legs: {dog.extra}")
  125. for cat in cats:
  126.     print(f"Cat: {cat.name}, Age: {cat.age}, IQ: {cat.extra}")
  127. for snake in snakes:
  128.     print(f"Snake: {snake.name}, Age: {snake.age}, Cruelty: {snake.extra}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement