From acc6d274c2cad7cd7fad4a1656a01243da6e2c6f Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Thu, 29 Jan 2026 12:18:19 +0100 Subject: [PATCH] Corrected ex1, ready to push --- 01/ex0/ft_garden_intro.py | 2 +- 01/ex1/ft_garden_data.py | 18 ++-- 01/ex2/ft_plant_growth.py | 27 +++-- 01/ex3/ft_plant_factory.py | 66 ++++++++----- 01/ex4/ft_garden_security.py | 27 +++-- 01/ex5/ft_plant_types.py | 70 ++++++++++--- 01/ex6/ft_garden_analytics.py | 180 +++++++++++++++++++++++++--------- 7 files changed, 279 insertions(+), 111 deletions(-) diff --git a/01/ex0/ft_garden_intro.py b/01/ex0/ft_garden_intro.py index f2da79a..3e38e6d 100644 --- a/01/ex0/ft_garden_intro.py +++ b/01/ex0/ft_garden_intro.py @@ -5,7 +5,7 @@ if __name__ == "__main__": print("=== Welcome to My Garden ===") print("Plant:", name) - print("Height:", height, "cm") + print("Height: ", height, "cm", sep="") print("Age:", age, "days") print("") print("=== End of Program ===") diff --git a/01/ex1/ft_garden_data.py b/01/ex1/ft_garden_data.py index 131f51c..28c7b0c 100644 --- a/01/ex1/ft_garden_data.py +++ b/01/ex1/ft_garden_data.py @@ -1,22 +1,24 @@ -class Plants: +class Plant: name: str height: int days: int - def __init__(self, name, height, days): + def __init__(self, name: str, height: int, days: int) -> None: + """Init plant with his value""" self.name = name self.height = height self.days = days - def print_plant(self): - print(self.name.capitalize() + ":", self.height, end="") - print("cm,", self.days, "days old") + def print_plant(self) -> None: + """Display plant informations""" + print(self.name, ": ", self.height, + "cm, ", self.days, " days old", sep="") if __name__ == "__main__": - x = Plants("rose", 25, 30) - y = Plants("sunflower", 80, 45) - z = Plants("cactus", 15, 120) + x = Plant("rose", 25, 30) + y = Plant("sunflower", 80, 45) + z = Plant("cactus", 15, 120) print("=== Garden Plant Registry ===") x.print_plant() y.print_plant() diff --git a/01/ex2/ft_plant_growth.py b/01/ex2/ft_plant_growth.py index f8480bc..e0d636f 100644 --- a/01/ex2/ft_plant_growth.py +++ b/01/ex2/ft_plant_growth.py @@ -3,28 +3,35 @@ class Plants: height: int days: int - def __init__(self, name, height, days): + def __init__(self, name: str, height: int, days: int) -> None: + """Init plant with his value""" self.name = name self.height = height self.days = days - def get_info(self): - print(self.name.capitalize() + ":", self.height, end="") - print("cm,", self.days, "days old") + def get_info(self) -> None: + """Display plant informations""" + print(self.name, ": ", self.height, + "cm, ", self.days, " days old", sep="") - def grow(self): + def grow(self) -> None: + """Increase height""" self.height = self.height + 1 - def age(self): + def age(self) -> None: + """Increase days by one and grow plant""" self.days = self.days + 1 self.grow() if __name__ == "__main__": - x = Plants("rose", 25, 1) - start = x.days; + x = Plants("rose", 25, 30) + start = x.days + print("=== Day 1 ===") x.get_info() - for n in range(1, 8): + for n in range(1, 7): x.age() + print(f"=== Day {x.days - start + 1} ===") x.get_info() - print("Growth this week: +", x.days - start, "cm"); + print("Growth this week: +", + x.days - start, "cm", sep="") diff --git a/01/ex3/ft_plant_factory.py b/01/ex3/ft_plant_factory.py index 5d090c8..639127b 100644 --- a/01/ex3/ft_plant_factory.py +++ b/01/ex3/ft_plant_factory.py @@ -1,36 +1,56 @@ -class Plants: +class Plant: name: str height: int - p_age: int + days: int - def __init__(self, name, height, age): - self.name = name - self.height = height - self.p_age = age + def get_info(self) -> None: + """Display plant informations""" + print(self.name, " (", self.height, + "cm, ", self.days, " days)", sep="") - def get_info(self): - print(self.name.capitalize() + ":", self.height, end="") - print("cm,", self.p_age, "days old") - - def grow(self): + def grow(self) -> None: + """Increase height""" self.height = self.height + 1 - def age(self): - self.p_age = self.p_age + 1 + def age(self) -> None: + """Increase days by one and grow plant""" + self.days = self.days + 1 self.grow() + def __init__(self, name: str, height: int, age: int) -> None: + """Init plant with his values and display his values""" + self.name = name + self.height = height + self.days = age + print("Created: ", end="") + self.get_info() -if __name__ == "__main__": - plant_1 = Plants("Rose", 50, 2) - plant_2 = Plants("Chrysanthem", 30, 1) - plant_3 = Plants("Rosemary", 60, 5) - plant_4 = Plants("Cucumber", 40, 3) - plant_5 = Plants("Salade", 15, 4) - plants = [plant_1, plant_2, plant_3, plant_4, plant_5] + +class PlantFactory: + @staticmethod + def create_plants(plants: list[tuple[str, + int, int]]) -> list[Plant | None]: + """Create list of plants by list of arguments""" + i = 0 + for n in plants: + i = i + 1 + new_plants: list[Plant | None] = [None] * i + i = 0 + for n in plants: + new_plants[i] = Plant(n[0], n[1], n[2]) + i = i + 1 + return new_plants + + +if __name__ == "__main__": + plants = PlantFactory.create_plants([ + ("Rose", 50, 2), + ("Chrysanthem", 30, 1), + ("Rosemary", 60, 5), + ("Cucumber", 40, 3), + ("Salade", 15, 4) + ]) i = 0 for n in plants: - print("Created:", end=" ") - n.get_info() i = i + 1 print("\nTotal plants created:", i) - diff --git a/01/ex4/ft_garden_security.py b/01/ex4/ft_garden_security.py index 4561e96..56a312a 100644 --- a/01/ex4/ft_garden_security.py +++ b/01/ex4/ft_garden_security.py @@ -4,33 +4,44 @@ class SecurePlant: __age: int def set_height(self, height: int) -> None: + """Set height to plant""" if height < 0: - print("Invalide operation attempted: height", height, "cm [REJECTED]") - + print("Invalide operation attempted: height ", + height, "cm [REJECTED]", sep="") + print("Secrity: Negative height rejected") else: self.__height = height + print("Height updated: ", height, "cm [OK]", sep="") def set_age(self, age: int) -> None: + """Set age to plant""" if age < 0: print("Invalide operation attempted: age", age, "days [REJECTED]") + print("Security: Negative age rejected") else: self.__age = age + print("Age updated:", age, "days [OK]") def get_height(self) -> int: + """Get plant height""" return self.__height def get_age(self) -> int: + """Get plant age""" return self.__age - def __init__(self, name: str, height: int, age: int): + def __init__(self, name: str, height: int, age: int) -> None: + """Init plant with his value""" self.name = name - self.__height = height - self.__age = age + self.set_height(height) + self.set_age(age) + print("Plant created:", name) if __name__ == "__main__": plant = SecurePlant("Rose", 10, 3) - print(plant.name, plant.get_height(), plant.get_age()) - plant.set_height(-10) + plant.set_height(25) plant.set_age(30) - print(plant.name, plant.get_height(), plant.get_age()) + plant.set_height(-10) + print("Current plant: ", plant.name, " (", plant.get_height(), + "cm, ", plant.get_age(), " days)", sep="") diff --git a/01/ex5/ft_plant_types.py b/01/ex5/ft_plant_types.py index 705958a..1f81a6d 100644 --- a/01/ex5/ft_plant_types.py +++ b/01/ex5/ft_plant_types.py @@ -1,37 +1,58 @@ +from typing_extensions import override + + class Plant: name: str height: int age: int - def __init__(self, name, height, age): + def __init__(self, name: str, height: int, age: int) -> None: + """Init plant with his value""" self.name = name self.height = height self.age = age - def get_info(self): - print(self.name.capitalize() + ":", self.height, end="") - print("cm,", self.age, "days old") + def get_info(self) -> None: + """Display plant informations""" + print(f"{self.name}: {self.height}cm, {self.age} days") class Flower(Plant): color: str - def bloom(self): + def bloom(self) -> None: + """Make flower blooming""" print(self.name + " is blooming beautifully !") - def __init__(self, name, height, age, color): + @override + def get_info(self) -> None: + """Display Flower info""" + print(f"{self.name} (Flower): {self.height}cm,", + f"{self.age} days, {self.color} color") + + def __init__(self, name: str, height: int, age: int, color: str) -> None: + """Init flower with his value""" super().__init__(name=name, height=height, age=age) self.color = color class Tree(Plant): - trunk_diameter: str + trunk_diameter: int - def produce_shade(self): + def produce_shade(self) -> None: + """Produce shade and display his size""" shade_size = self.height * self.trunk_diameter / 1000 - print(self.name, "provides", shade_size, "square meter") + print(self.name, "provides", shade_size, "square meter of shade") - def __init__(self, name, height, age, trunk_diameter): + @override + def get_info(self) -> None: + """Display Tree info""" + print(f"{self.name} (Tree): {self.height}cm,", + f"{self.age} days, {self.trunk_diameter}cm diameter") + + def __init__(self, name: str, height: int, + age: int, trunk_diameter: int) -> None: + """Init tree with his value""" super().__init__(name=name, height=height, age=age) self.trunk_diameter = trunk_diameter @@ -40,7 +61,19 @@ class Vegetable(Plant): harvest_season: str nutritional_value: str - def __init__(self, name, height, age, hs, nv): + def print_nutritional_value(self) -> None: + """Display nutritional value""" + print(f"{self.name} is rich in {self.nutritional_value}") + + @override + def get_info(self) -> None: + """Display Vegetable info info""" + print(f"{self.name} (Vegetable): {self.height}cm,", + f"{self.age} days, {self.harvest_season} harvest") + + def __init__(self, name: str, height: int, + age: int, hs: str, nv: str) -> None: + """Init vegetable with his value""" super().__init__(name=name, height=height, age=age) self.harvest_season = hs self.nutritional_value = nv @@ -49,11 +82,16 @@ class Vegetable(Plant): if __name__ == "__main__": flower0 = Flower("Rose", 30, 5, "red") flower1 = Flower("Chrysanthem", 50, 1, "yellow") - tree0 = Tree("Platane", 300, 6, 30) - tree1 = Tree("Sapin", 700, 15, 50) + tree0 = Tree("Spruce", 300, 6, 30) + tree1 = Tree("Oak", 700, 15, 50) vegetable0 = Vegetable("Salade", 30, 1, "Summer", "Vitamine c") - vegetable1 = Vegetable("tomato", 200, 2, "Autumn", "Vitamine d") - print(flower0.name, flower0.height, flower0.age, flower0.color) + vegetable1 = Vegetable("Tomato", 200, 2, "Autumn", "Vitamine d") + + flower0.get_info() flower0.bloom() + print("") + tree0.get_info() tree0.produce_shade() - print(vegetable0.name, "is rich in", vegetable0.nutritional_value) + print("") + vegetable0.get_info() + vegetable0.print_nutritional_value() diff --git a/01/ex6/ft_garden_analytics.py b/01/ex6/ft_garden_analytics.py index e85e978..b609346 100644 --- a/01/ex6/ft_garden_analytics.py +++ b/01/ex6/ft_garden_analytics.py @@ -1,90 +1,179 @@ class Plant: name: str height: int - kind: str + total_growth: int + __kind: str - def __init__(self, name: str, height: int): + def __init__(self, name: str, height: int) -> None: + """Init plant with his value""" self.name = name self.height = height - self.kind = "regular" + self.__kind = "regular" + self.total_growth = 0 - @classmethod - def grow(self): + def get_kind(self) -> str: + """Return __kind value""" + return self.__kind + + def set_kind(self, kind: str) -> None: + """Set __kind value""" + self.__kind = kind + + def grow(self) -> None: + """Grow the plant""" self.height = self.height + 1 + self.total_growth += 1 + class FloweringPlant(Plant): color: str - blooming = False + blooming: bool = False - def bloom (self): + def bloom(self) -> None: + """Set blooming to true""" self.blooming = True print(self.name, "is blooming beautifully !") - def __init__(self, name, height, color): + def __init__(self, name: str, height: int, color: str) -> None: + """Init Flowering plant with his value""" super().__init__(name=name, height=height) self.color = color - self.kind = "flowering" + self.set_kind("flowering") class PrizeFlower(FloweringPlant): prize: int - def __init__(self, name, height, color, prize): + def __init__(self, name: str, height: int, color: str, prize: int) -> None: + """Init prize flower with his value""" super().__init__(name=name, height=height, color=color) self.prize = prize - self.kind = "prize" + self.set_kind("prize") + class GardenManager: - __gardens = {} + gardens: dict[str, list[Plant | FloweringPlant | PrizeFlower]] + nb_gardens: int + + def __init__(self) -> None: + """Init garden manager""" + self.gardens = {} + self.nb_gardens = 0 class GardenStats: - @staticmethod - def garden_report(garden: []): - i = 0 - for n in garden: - i = i + 1 - n = 0 - while n < i: - print(garden[n].name + ":", end=" ") - print(garden[n].height, "cm", end="") - if (garden[n].kind == "flowering" or garden[n].kind == "prize"): - print(",", garden[n].color, "flowers", end=" ") - if (garden[n].blooming): - print("(blooming)", end="") - if (garden[n].kind == "prize"): - print(", Prize points:", garden[n].prize, end="") + @classmethod + def garden_report(cls, + garden: list[Plant | + FloweringPlant | + PrizeFlower] | None) -> None: + """Start all garden report""" + if garden is not None: + cls.flower_in_garden(garden) print("") - n = n + 1 + cls.added_plant_report(garden) + cls.plants_in_garden(garden) + else: + print("Garden not provide") + @staticmethod + def flower_in_garden(garden: list[Plant | + FloweringPlant | + PrizeFlower]) -> None: + """List flower in garden and display their status""" + for n in garden: + print("-", n.name + ":", end=" ") + print(n.height, "cm", end="") + if n.get_kind() == "flowering" or n.get_kind() == "prize": + print(",", n.color, "flowers", end="") + if n.blooming: + print(" (blooming)", end="") + if n.get_kind() == "prize": + print(", Prize points:", n.prize, end="") + print("") - @classmethod - def get_garden(self, garden: str) -> []: - return self.__gardens[garden] + @staticmethod + def added_plant_report(garden: list[Plant | + FloweringPlant | + PrizeFlower]) -> None: + """Report number of plant added to garden""" + total_growth = 0 + total_plants = 0 + for n in garden: + total_plants += 1 + total_growth += n.total_growth + print(f"Plants added: {total_plants},", + f"Total growth: {total_growth}cm") + @staticmethod + def plants_in_garden(garden: list[Plant | + FloweringPlant | + PrizeFlower]) -> None: + """Report number of plant by types""" + nb_regular = 0 + nb_flowering = 0 + nb_prize = 0 + for n in garden: + if n.get_kind() == "regular": + nb_regular += 1 + elif n.get_kind() == "flowering": + nb_flowering += 1 + elif n.get_kind() == "prize": + nb_prize += 1 + print(f"Plant types: {nb_regular} regular,", + f"{nb_flowering} flowering, {nb_prize} prize flowers") - @classmethod - def add_plant(self, garden: str, new_plant: Plant): - if garden in self.__gardens: + def get_nb_gardens(self) -> int: + """return the number of garden in the manager""" + return self.nb_gardens + + def get_garden(self, garden: str) -> list[Plant | + FloweringPlant | + PrizeFlower] | None: + """Return the plant list of the garden name provide if exist""" + if garden in self.gardens: + return self.gardens[garden] + else: + return None + + def add_plant(self, garden: str, new_plant: Plant | + FloweringPlant | + PrizeFlower) -> None: + """Add one plant to the garden if exist""" + if garden in self.gardens: i = 0 - for n in self.__gardens[garden]: + for n in self.gardens[garden]: i = i + 1 - new = [None] * (i + 1) - j = 0; + new: list[Plant | + FloweringPlant | + PrizeFlower] = [Plant("", 0)] * (i + 1) + j = 0 while j < i: - new[j] = self.__gardens[garden][j] + new[j] = self.gardens[garden][j] j = j + 1 new[i] = new_plant - self.__gardens[garden] = new - print("Added", new_plant.name, "to", garden, "'s garden") + self.gardens[garden] = new + print("Added ", new_plant.name, " to ", + garden, "'s garden", sep="") else: print("Garden not found") - @classmethod def create_garden_network(self, garden_name: str) -> None: - if garden_name in self.__gardens: + """Create a garden by name provide if it not exist""" + if garden_name in self.gardens: print("garden already exist") else: - self.__gardens[garden_name] = [] + self.gardens[garden_name] = [] + self.nb_gardens += 1 + + def grow_all(self, garden: str) -> None: + """Make all plant of one garden to grow""" + if garden in self.gardens: + print(f"{garden} is helping all plants grow...") + for n in self.gardens[garden]: + print(f"{n.name} grew 1cm") + n.grow() + else: + print("Garden not found") if __name__ == "__main__": @@ -99,6 +188,7 @@ if __name__ == "__main__": manager.add_plant("Alice", rose) manager.add_plant("Alice", sunflower) manager.add_plant("Bob", oak) - - + manager.grow_all("Alice") + manager.grow_all("Alice") + print("\n\nGardenReport") manager.GardenStats.garden_report(manager.get_garden("Alice"))