Compare commits

...

3 Commits

Author SHA1 Message Date
83efbc7cc0 ex3 finished + flake for ex1 2026-02-05 12:41:58 +01:00
3158ce4eca 03/03 WIP 2026-02-05 11:21:14 +01:00
eab1167091 FIX: Make 02 flake8 complient 2026-02-02 14:41:10 +01:00
7 changed files with 76 additions and 55 deletions

View File

@@ -15,6 +15,7 @@ def check_temperature(temp_str: str) -> int | None:
print("Error: " + temp_str + "°C", ex) print("Error: " + temp_str + "°C", ex)
return (None) return (None)
if __name__ == "__main__": if __name__ == "__main__":
print("=== Garden Temperature checker ===") print("=== Garden Temperature checker ===")
print("\nTesting temperature: 25") print("\nTesting temperature: 25")

View File

@@ -1,7 +1,7 @@
def water_plants(plant_list: list[str | None]) -> None: def water_plants(plant_list: list[str | None]) -> None:
"""Display watering plant if plant is a string""" """Display watering plant if plant is a string"""
for plant in plant_list: for plant in plant_list:
if plant == None: if not plant:
raise ValueError raise ValueError
print("Watering", plant) print("Watering", plant)
@@ -14,7 +14,7 @@ def test_watering_system() -> None:
try: try:
print("Opening watering system") print("Opening watering system")
water_plants(plants) water_plants(plants)
except: except ValueError:
print("Error: Cannot water None - invalid plant!") print("Error: Cannot water None - invalid plant!")
finally: finally:
print("Closing watering system (cleanup)") print("Closing watering system (cleanup)")

View File

@@ -1,14 +1,19 @@
def check_plant_health(plant_name: str, water_level: int, sunlight_hours: int) -> None: def check_plant_health(plant_name: str, water_level: int,
sunlight_hours: int) -> None:
if not plant_name or not plant_name[0]: if not plant_name or not plant_name[0]:
raise ValueError("Error: Plant name cannot be empty") raise ValueError("Error: Plant name cannot be empty")
elif water_level < 1: elif water_level < 1:
raise ValueError(f"Error: Water level {water_level} is too low (min 1)") raise ValueError(f"Error: Water level {water_level}\
is too low (min 1)")
elif water_level > 10: elif water_level > 10:
raise ValueError(f"Error: Water level {water_level} is too high (max 10)") raise ValueError(f"Error: Water level {water_level}\
is too high (max 10)")
elif sunlight_hours < 2: elif sunlight_hours < 2:
raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too low (min 2)") raise ValueError(f"Error: Sunlight hours {sunlight_hours}\
is too low (min 2)")
elif sunlight_hours > 12: elif sunlight_hours > 12:
raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too high (max 12)") raise ValueError(f"Error: Sunlight hours {sunlight_hours}\
is too high (max 12)")
else: else:
print("Plant '" + plant_name + "' is healthy!") print("Plant '" + plant_name + "' is healthy!")

View File

@@ -34,7 +34,8 @@ class Plant:
__water_level: int __water_level: int
__sunlight_hours: int __sunlight_hours: int
def __init__(self, name: str, water_level: int, sunlight_hours: int) -> None: def __init__(self, name: str, water_level: int,
sunlight_hours: int) -> None:
if name == "": if name == "":
raise Exception("Plant name cannot be empty") raise Exception("Plant name cannot be empty")
self.set_name(name) self.set_name(name)
@@ -65,9 +66,11 @@ class Plant:
def set_sunlight_hours(self, sunlight_hours: int) -> None: def set_sunlight_hours(self, sunlight_hours: int) -> None:
if sunlight_hours < 2: if sunlight_hours < 2:
raise SunlightError(f"Sunlight hours {sunlight_hours} is too low (min 2)") raise SunlightError(f"Sunlight hours {sunlight_hours}\
is too low (min 2)")
if sunlight_hours > 12: if sunlight_hours > 12:
raise SunlightError(f"Sunlight hours {sunlight_hours} is too high (max 12)") raise SunlightError(f"Sunlight hours {sunlight_hours}\
is too high (max 12)")
self.__sunlight_hours = sunlight_hours self.__sunlight_hours = sunlight_hours
def water_plant(self) -> None: def water_plant(self) -> None:
@@ -88,7 +91,6 @@ class GardenManager:
def __str__(self) -> str: def __str__(self) -> str:
return f"Caught WaterTankError: {self.message}" return f"Caught WaterTankError: {self.message}"
def __init__(self) -> None: def __init__(self) -> None:
self.garden = [] self.garden = []
self.__water_tank_level = 0 self.__water_tank_level = 0
@@ -115,15 +117,20 @@ class GardenManager:
try: try:
for plant in self.garden: for plant in self.garden:
if plant.get_water_level() < 1: if plant.get_water_level() < 1:
raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too low (min 1)") raise WaterError(f"{plant.get_name()}: Water level\
{plant.get_water_level()} is too low (min 1)")
elif plant.get_water_level() > 10: elif plant.get_water_level() > 10:
raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too high (max 10)") raise WaterError(f"{plant.get_name()}: Water level\
{plant.get_water_level()} is too high (max 10)")
elif plant.get_sunlight_hours() < 2: elif plant.get_sunlight_hours() < 2:
raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too low (min 2)") raise SunlightError(f"{plant.get_name()}: Sunlight hours\
{plant.get_sunlight_hours()} is too low (min 2)")
elif plant.get_sunlight_hours() > 12: elif plant.get_sunlight_hours() > 12:
raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too high (max 12)") raise SunlightError(f"{plant.get_name()}: Sunlight hours\
{plant.get_sunlight_hours()} is too high (max 12)")
else: else:
print(f"{plant.get_name()}: healthy (water: {plant.get_water_level()}, sun: {plant.get_sunlight_hours()})") print(f"{plant.get_name()}: healthy (water:\
{plant.get_water_level()}, sun: {plant.get_sunlight_hours()})")
except (WaterError, SunlightError) as err: except (WaterError, SunlightError) as err:
print(err) print(err)

View File

@@ -3,7 +3,8 @@ import sys
if __name__ == "__main__": if __name__ == "__main__":
args = sys.argv args = sys.argv
if len(args) <= 1: if len(args) <= 1:
print("No scores provided. Usage: python3 ft_score_analytics.py <score1> <score2> ...") print("No scores provided. Usage: python3\
ft_score_analytics.py <score1> <score2> ...")
else: else:
scores = [0] * (len(args) - 1) scores = [0] * (len(args) - 1)
i = 0 i = 0

View File

@@ -1,35 +1,42 @@
class Player: def tracker_system(players: dict[str, list[str]]) -> None:
__name: str print("=== Achievement Tracker System ===\n")
__achievements: set[str] for player in players:
print(f"Player {player} achievements: {set((players[player]))}")
def __init__(self, name:str, achievements: set[str]) -> None: print("\n=== Achievement Analytics ===")
self.__name = name unique_achievements: set[str] = set(())
self.__achievements = achievements for player in players:
unique_achievements = unique_achievements | set((players[player]))
def get_name(self) -> str: print(f"All unique achievements: {unique_achievements}")
return self.__name print(f"Total unique achievements: {len(unique_achievements)}")
common_achievements: set[str] = unique_achievements
def get_achievements(self) -> set[str]: for player in players:
return self.__achievements common_achievements = common_achievements & set((players[player]))
print(f"\nCommon to all players: {common_achievements}")
player_rare: dict[str, set[str]] = {}
for player in players:
temp = set((players[player]))
for other in players:
if other != player:
temp = temp - set((players[other]))
player_rare[player] = temp
rare_achievements: set[str] = set(())
for n in player_rare:
rare_achievements = rare_achievements | player_rare[n]
print(f"Rare achievements (1 player): {rare_achievements}\n")
a_vs_b_common = set((players["Alice"])) & set((players["Bob"]))
print(f"Alice vs Bob common: {a_vs_b_common}")
alice_unique = set((players["Alice"])) - set((players["Bob"]))
print(f"Alice unique: {alice_unique}")
bob_unique = set((players["Bob"])) - set((players["Alice"]))
print(f"Bob unique: {bob_unique}")
if __name__ == "__main__": if __name__ == "__main__":
players = [ players = {
Player("Alice", set(('first_kill', 'level_10', 'treasure_hunter', 'speed_demon'))), "Alice": ['first_kill', 'level_10',
Player("Bob",set(('first_kill', 'level_10', 'boss_slayer', 'collector'))), 'treasure_hunter', 'speed_demon'],
Player("Charlie", set(('level_10', 'treasure_hunter', 'boss_slayer', 'speed_demon', 'perfectionist'))) "Bob": ['first_kill', 'level_10', 'boss_slayer', 'collector'],
] "Charlie": ['level_10', 'treasure_hunter', 'boss_slayer',
print("=== Achievement Tracker System ===\n") 'speed_demon', 'perfectionist']
for n in players: }
print(f"Player {n.get_name()} achievements: {n.get_achievements()}") tracker_system(players)
print("\n=== Achievement Analytics ===")
unique_achievements = players[0].get_achievements() | players[1].get_achievements() | players[2].get_achievements()
print(f"All unique achievements: {unique_achievements}")
print(f"Total unique achievements: {len(unique_achievements)}")
common_achievements = players[0].get_achievements() & players[1].get_achievements() & players[2].get_achievements()
print(f"\nCommon to all players: {common_achievements}")
p0_achievements = players[0].get_achievements() - players[1].get_achievements() - players[2].get_achievements()
p1_achievements = players[0].get_achievements() - players[1].get_achievements() - players[2].get_achievements()
p2_achievements = players[0].get_achievements() - players[1].get_achievements() - players[2].get_achievements()
print(f"Rare achievements (1 player): {rare_achievements}")