From 23a8afa777894f143a45903f9b2de2a68c8266a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AF=D1=80=D0=BE=D1=81=D0=BB=D0=B0=D0=B2=20=D0=A8=D0=B5?= =?UTF-8?q?=D0=B2=D1=87=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Fri, 15 Nov 2024 11:37:10 +0200 Subject: [PATCH] lockdown_cafe --- app/__init__.py | 0 app/cafe.py | 21 ++++++++++++ app/errors.py | 17 ++++++++++ app/main.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 app/__init__.py create mode 100644 app/cafe.py create mode 100644 app/errors.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/cafe.py b/app/cafe.py new file mode 100644 index 00000000..5654a458 --- /dev/null +++ b/app/cafe.py @@ -0,0 +1,21 @@ +import datetime +from app.errors import (NotVaccinatedError, NotWearingMaskError, + OutdatedVaccineError) + + +class Cafe: + def __init__(self, name: str) -> None: + self.name = name + + def visit_cafe(self, visitor: dict) -> str: + if "vaccine" not in visitor: + raise NotVaccinatedError() + + expiration_date = visitor["vaccine"]["expiration_date"] + if expiration_date < datetime.date.today(): + raise OutdatedVaccineError() + + if not visitor.get("wearing_a_mask", False): + raise NotWearingMaskError() + + return f"Welcome to {self.name}" diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 00000000..6f60eeac --- /dev/null +++ b/app/errors.py @@ -0,0 +1,17 @@ +class VaccineError(Exception): + pass + + +class NotVaccinatedError(VaccineError): + def __init__(self) -> None: + super().__init__("Visitor is not vaccinated.") + + +class OutdatedVaccineError(VaccineError): + def __init__(self) -> None: + super().__init__("Visitor's vaccine is outdated.") + + +class NotWearingMaskError(Exception): + def __init__(self) -> None: + super().__init__("Visitor is not wearing a mask.") diff --git a/app/main.py b/app/main.py index fa56336e..b82deb70 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,86 @@ -# write your code here +from app.cafe import Cafe +import datetime + + +def go_to_cafe(friends: list[dict], cafe: Cafe) -> str: + masks_needed = 0 + all_vaccinated = True + + for friend in friends: + if "vaccine" not in friend: + all_vaccinated = False + continue + + expiration_date = friend["vaccine"]["expiration_date"] + if expiration_date < datetime.date.today(): + all_vaccinated = False + + if not friend.get("wearing_a_mask", False): + masks_needed += 1 + + if not all_vaccinated: + return "All friends should be vaccinated" + + if masks_needed > 0: + return f"Friends should buy {masks_needed} masks" + + return f"Friends can go to {cafe.name}" + + +# Example usage +if __name__ == "__main__": + kfc = Cafe("KFC") + + friends = [ + { + "name": "Alisa", + "vaccine": { + "expiration_date": datetime.date.today() + }, + "wearing_a_mask": True + }, + { + "name": "Bob", + "vaccine": { + "expiration_date": datetime.date.today() + }, + "wearing_a_mask": True + }, + ] + + print(go_to_cafe(friends, kfc)) + + friends = [ + { + "name": "Alisa", + "vaccine": { + "expiration_date": datetime.date.today() + }, + "wearing_a_mask": False + }, + { + "name": "Bob", + "vaccine": { + "expiration_date": datetime.date.today() + }, + "wearing_a_mask": False + }, + ] + + print(go_to_cafe(friends, kfc)) + + friends = [ + { + "name": "Alisa", + "wearing_a_mask": True + }, + { + "name": "Bob", + "vaccine": { + "expiration_date": datetime.date.today() + }, + "wearing_a_mask": True + }, + ] + + print(go_to_cafe(friends, kfc))