Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Static and Class Methods - Lab
- # https://judge.softuni.org/Contests/Practice/Index/2430#3
- ======================================================================================================
- # 01. Calculator
- # 02. Shop
- # 03. Integer
- # 04. Hotel Rooms
- ======================================================================================================
- # 01. Calculator
- from functools import reduce
- class Calculator:
- @staticmethod
- def add(*args):
- return reduce(lambda x, y: x + y, args)
- @staticmethod
- def multiply(*args):
- return reduce(lambda x, y: x * y, args)
- @staticmethod
- def divide(*args):
- return reduce(lambda x, y: x / y, args)
- @staticmethod
- def subtract(*args):
- return reduce(lambda x, y: x - y, args)
- ======================================================================================================
- # 02. Shop
- class Shop:
- def __init__(self, name: str, type: str, capacity: int):
- self.name = name
- self.type = type
- self.capacity = capacity
- self.items = {}
- @classmethod
- def small_shop(cls, name: str, type: str):
- return cls(name, type, 10)
- def add_item(self, item_name: str):
- if self.capacity == 0:
- return 'Not enough capacity in the shop'
- self.capacity -= 1
- if item_name not in self.items:
- self.items[item_name] = 0
- self.items[item_name] += 1
- return f"{item_name} added to the shop"
- def remove_item(self, item_name: str, amount: int):
- if item_name in self.items and self.items[item_name] >= amount:
- self.items[item_name] -= amount
- if self.items[item_name] == 0:
- del self.items[item_name]
- return f"{amount} {item_name} removed from the shop"
- return f'Cannot remove {amount} {item_name}'
- def __repr__(self):
- return f'{self.name} of type {self.type} with capacity {self.capacity}'
- ======================================================================================================
- # 03. Integer
- class Integer:
- def __init__(self, value: int):
- self.value = value
- @classmethod
- def from_float(cls, float_value):
- if type(float_value) == float:
- return cls(int(float_value))
- return "value is not a float"
- @classmethod
- def from_roman(cls, value):
- rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
- int_val = 0
- for i in range(len(value)):
- if i > 0 and rom_val[value[i]] > rom_val[value[i - 1]]:
- int_val += rom_val[value[i]] - 2 * rom_val[value[i - 1]]
- else:
- int_val += rom_val[value[i]]
- return cls(int_val)
- @classmethod
- def from_string(cls, value):
- try:
- if not isinstance(value, str):
- raise ValueError
- return cls(int(value))
- except ValueError:
- return 'wrong type'
- ======================================================================================================
- # 04. Hotel Rooms
- # file name: hotel.py
- from project.room import Room
- class Hotel:
- def __init__(self, name: str):
- self.name = name
- self.rooms = []
- self.guests = 0
- @classmethod
- def from_stars(cls, stars_count: int):
- return cls(f"{stars_count} stars Hotel")
- def add_room(self, room: Room):
- self.rooms.append(room)
- def take_room(self, room_number, people):
- room = ([r for r in self.rooms if r.number == room_number])[0]
- self.guests += people
- return room.take_room(people)
- def free_room(self, room_number):
- room = ([r for r in self.rooms if r.number == room_number])[0]
- self.guests -= room.guests
- return room.free_room()
- def status(self):
- result = f"Hotel {self.name} has {self.guests} total guests\n"
- result += f"Free rooms: {', '.join(([str(r.number) for r in self.rooms if not r.is_taken]))}\n"
- result += f"Taken rooms: {', '.join(([str(r.number) for r in self.rooms if r.is_taken]))}"
- return result
- ------------------------------------------------------------------------------------------------------
- # file name: room.py
- class Room:
- def __init__(self, number: int, capacity: int):
- self.number = number
- self.capacity = capacity
- self.guests = 0
- self.is_taken = False
- def take_room(self, people):
- if not self.is_taken and self.capacity >= people:
- self.is_taken = True
- self.guests += people
- return
- return f"Room number {self.number} cannot be taken"
- def free_room(self):
- if not self.is_taken:
- return f"Room number {self.number} is not taken"
- self.is_taken = False
- self.guests = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement