Skip to content

Instantly share code, notes, and snippets.

@perevertysh

perevertysh/rooms.py

Created Oct 23, 2020
Embed
What would you like to do?
from typing import List
class Room:
def __init__(self, rank: int, name: str = ""):
self.name = name
self.rank = rank
def __str__(self):
return f"{self.name}: {self.rank}"
def __repr__(self):
return str(self.rank)
limits = [{i, 2 * i} for i in range(0, 26)]
@staticmethod
def get_rank_list(rooms: List) -> List:
return [room.rank for room in rooms]
@staticmethod
def show_rooms(rooms: List) -> List:
return [str(room) for room in rooms]
@staticmethod
def ad_least_twice(rooms: List) -> bool:
for room in rooms:
for divider_room in rooms:
if divider_room.rank / room.rank >= 2.0:
return True
return False
@staticmethod
def exactly_twice(rooms: List) -> bool:
ranks = Room.get_rank_list(rooms)
for rank in ranks:
if 2 * rank in ranks:
return True
return False
@staticmethod
def constrained_exactly_twice(rooms: List) -> bool:
ranks = {room.rank for room in rooms}
for limit in Room.limits:
if limit.issubset(ranks):
return True
return False
import unittest
from room import Room
class TestRoomMethods(unittest.TestCase):
def check_room_method(self, method, items_true, items_false):
check_true = method(items_true)
check_false = method(items_false)
self.assertTrue(
check_true,
f"True != {check_true}, {Room.show_rooms(items_true)}"
)
self.assertFalse(
check_false,
f"False != {check_false}, {Room.show_rooms(items_false)}"
)
def test_at_least_twice(self):
"""Check method Room.at_least_twice"""
rooms_true = [Room(name=f"room_{i}", rank=i) for i in range(1, 100, 2)]
rooms_false = [Room(name=f"room_{i}", rank=i) for i in range(20, 39)]
print(f"""Check method Room.at_least_twice: \n"
true sequence: {rooms_true} \n
false sequence: {rooms_false}\n""")
self.check_room_method(
Room.ad_least_twice,
rooms_true,
rooms_false
)
def test_exactly_twice(self):
"""Check method Room.exactly_twice"""
rooms_true = [Room(name=f"room_{i}", rank=i) for i in range(1, 100)]
rooms_false = [Room(name=f"room_{i}", rank=i) for i in range(20, 39)]
print(f"""Check method Room.exactly_twice: \n
true sequence: {rooms_true} \n
false sequence: {rooms_false}\n""")
self.check_room_method(
Room.exactly_twice,
rooms_true,
rooms_false
)
def test_constrained_exactly_twice(self):
"""Check method Room.constrained_exactly_twice"""
rooms_true = [Room(name=f"room_{i}", rank=i) for i in range(10, 51)]
rooms_false = [Room(name=f"room_{i}", rank=i) for i in range(20, 39)]
print(f"""Check method Room.constrained_exactly_twice: \n
true sequence: {rooms_true} \n
false sequence: {rooms_false}\n""")
self.check_room_method(
Room.constrained_exactly_twice,
rooms_true,
rooms_false
)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment