Advertisement
GeorgiLukanov87

Static and Class Methods - Lab

Oct 27th, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. # Static and Class Methods - Lab
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/2430#3
  4. ======================================================================================================
  5. # 01. Calculator
  6. # 02. Shop
  7. # 03. Integer
  8. # 04. Hotel Rooms
  9. ======================================================================================================
  10. # 01. Calculator
  11.  
  12. from functools import reduce
  13.  
  14.  
  15. class Calculator:
  16.  
  17.     @staticmethod
  18.     def add(*args):
  19.         return reduce(lambda x, y: x + y, args)
  20.  
  21.     @staticmethod
  22.     def multiply(*args):
  23.         return reduce(lambda x, y: x * y, args)
  24.  
  25.     @staticmethod
  26.     def divide(*args):
  27.         return reduce(lambda x, y: x / y, args)
  28.  
  29.     @staticmethod
  30.     def subtract(*args):
  31.         return reduce(lambda x, y: x - y, args)
  32.  
  33.  
  34. ======================================================================================================
  35. # 02. Shop
  36.  
  37. class Shop:
  38.     def __init__(self, name: str, type: str, capacity: int):
  39.         self.name = name
  40.         self.type = type
  41.         self.capacity = capacity
  42.         self.items = {}
  43.  
  44.     @classmethod
  45.     def small_shop(cls, name: str, type: str):
  46.         return cls(name, type, 10)
  47.  
  48.     def add_item(self, item_name: str):
  49.         if self.capacity == 0:
  50.             return 'Not enough capacity in the shop'
  51.         self.capacity -= 1
  52.         if item_name not in self.items:
  53.             self.items[item_name] = 0
  54.         self.items[item_name] += 1
  55.         return f"{item_name} added to the shop"
  56.  
  57.     def remove_item(self, item_name: str, amount: int):
  58.         if item_name in self.items and self.items[item_name] >= amount:
  59.             self.items[item_name] -= amount
  60.             if self.items[item_name] == 0:
  61.                 del self.items[item_name]
  62.             return f"{amount} {item_name} removed from the shop"
  63.         return f'Cannot remove {amount} {item_name}'
  64.  
  65.     def __repr__(self):
  66.         return f'{self.name} of type {self.type} with capacity {self.capacity}'
  67.  
  68.  
  69. ======================================================================================================
  70. # 03. Integer
  71.  
  72. class Integer:
  73.     def __init__(self, value: int):
  74.         self.value = value
  75.  
  76.     @classmethod
  77.     def from_float(cls, float_value):
  78.         if type(float_value) == float:
  79.             return cls(int(float_value))
  80.         return "value is not a float"
  81.  
  82.     @classmethod
  83.     def from_roman(cls, value):
  84.         rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
  85.         int_val = 0
  86.         for i in range(len(value)):
  87.             if i > 0 and rom_val[value[i]] > rom_val[value[i - 1]]:
  88.                 int_val += rom_val[value[i]] - 2 * rom_val[value[i - 1]]
  89.             else:
  90.                 int_val += rom_val[value[i]]
  91.         return cls(int_val)
  92.  
  93.     @classmethod
  94.     def from_string(cls, value):
  95.         try:
  96.             if not isinstance(value, str):
  97.                 raise ValueError
  98.             return cls(int(value))
  99.         except ValueError:
  100.             return 'wrong type'
  101.  
  102. ======================================================================================================
  103. # 04. Hotel Rooms
  104. # file name: hotel.py
  105.  
  106. from project.room import Room
  107.  
  108.  
  109. class Hotel:
  110.     def __init__(self, name: str):
  111.         self.name = name
  112.         self.rooms = []
  113.         self.guests = 0
  114.  
  115.     @classmethod
  116.     def from_stars(cls, stars_count: int):
  117.         return cls(f"{stars_count} stars Hotel")
  118.  
  119.     def add_room(self, room: Room):
  120.         self.rooms.append(room)
  121.  
  122.     def take_room(self, room_number, people):
  123.         room = ([r for r in self.rooms if r.number == room_number])[0]
  124.         self.guests += people
  125.         return room.take_room(people)
  126.  
  127.     def free_room(self, room_number):
  128.         room = ([r for r in self.rooms if r.number == room_number])[0]
  129.         self.guests -= room.guests
  130.         return room.free_room()
  131.  
  132.     def status(self):
  133.         result = f"Hotel {self.name} has {self.guests} total guests\n"
  134.         result += f"Free rooms: {', '.join(([str(r.number) for r in self.rooms if not r.is_taken]))}\n"
  135.         result += f"Taken rooms: {', '.join(([str(r.number) for r in self.rooms if r.is_taken]))}"
  136.         return result
  137.  
  138. ------------------------------------------------------------------------------------------------------
  139. # file name: room.py
  140.  
  141. class Room:
  142.     def __init__(self, number: int, capacity: int):
  143.         self.number = number
  144.         self.capacity = capacity
  145.         self.guests = 0
  146.         self.is_taken = False
  147.  
  148.     def take_room(self, people):
  149.         if not self.is_taken and self.capacity >= people:
  150.             self.is_taken = True
  151.             self.guests += people
  152.             return
  153.         return f"Room number {self.number} cannot be taken"
  154.  
  155.     def free_room(self):
  156.         if not self.is_taken:
  157.             return f"Room number {self.number} is not taken"
  158.         self.is_taken = False
  159.         self.guests = 0
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement