summaryrefslogtreecommitdiff
path: root/py01
diff options
context:
space:
mode:
Diffstat (limited to 'py01')
-rwxr-xr-xpy01/ex0/ft_garden_intro.py16
-rwxr-xr-xpy01/ex1/ft_garden_data.py35
-rwxr-xr-xpy01/ex2/ft_plant_growth.py41
-rwxr-xr-xpy01/ex3/ft_plant_factory.py43
-rwxr-xr-xpy01/ex4/ft_garden_security.py91
-rwxr-xr-xpy01/ex5/ft_plant_types.py131
-rwxr-xr-xpy01/ex6/ft_garden_analytics.py227
7 files changed, 584 insertions, 0 deletions
diff --git a/py01/ex0/ft_garden_intro.py b/py01/ex0/ft_garden_intro.py
new file mode 100755
index 0000000..a8f45b7
--- /dev/null
+++ b/py01/ex0/ft_garden_intro.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python3
+
+
+def ft_garden_name() -> None:
+ print("=== Welcome to My Garden ===")
+ name = "Rose"
+ height = "25cm"
+ age = "30 days"
+ print("Plant: " + name)
+ print("Height: " + height)
+ print("Age: " + age)
+ print("=== End of Program === ")
+
+
+if __name__ == "__main__":
+ ft_garden_name()
diff --git a/py01/ex1/ft_garden_data.py b/py01/ex1/ft_garden_data.py
new file mode 100755
index 0000000..e5d8485
--- /dev/null
+++ b/py01/ex1/ft_garden_data.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+
+# Create a class
+class Plant:
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ self.name = name
+ self.height = height
+ self.age = age
+
+# Method to grow the age
+ def grow_age(self: 'Plant') -> None:
+ self.age += 1
+
+# Method to grow the height
+ def grow_height(self: 'Plant') -> None:
+ self.height *= 1.04
+
+# Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(self.name + ": " + str(round(self.height, 1)) + "cm, "
+ + str(self.age) + " days old")
+
+
+print("=== Garden Plant Registry ===")
+
+
+def ft_garden_data() -> None:
+ Params = [("Rose", 25, 30), ("Sunflower", 80, 45), ("Cactus", 15, 120)]
+ for param in Params:
+ Plant(*param).show()
+
+
+if __name__ == "__main__":
+ ft_garden_data()
diff --git a/py01/ex2/ft_plant_growth.py b/py01/ex2/ft_plant_growth.py
new file mode 100755
index 0000000..c516181
--- /dev/null
+++ b/py01/ex2/ft_plant_growth.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+# Create a class
+class Plant:
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ self.name_is = name
+ self.height_is = height
+ self.age_is = age
+
+# Method to grow the age
+ def age(self: 'Plant') -> None:
+ self.age_is += 1
+
+# Method to grow the height
+ def grow(self: 'Plant') -> None:
+ self.height_is *= 1.04
+
+# Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(f" {self.name_is}: {str(round(self.height_is, 1))}cm,"
+ f"{str(self.age_is)} days old")
+
+
+rose = Plant("Rose", 25, 30)
+
+
+def ft_garden_growth(plant: Plant) -> None:
+ print("=== Garden Plant Growth ===")
+ plant.show()
+ start = plant.height_is
+ count = 7
+ for i in range(1, count + 1):
+ print("=== Day " + str(i) + " ===")
+ plant.age()
+ plant.grow()
+ plant.show()
+ print("Growth this week: " + str(round(plant.height_is - start, 1)) + "cm")
+
+
+if __name__ == "__main__":
+ ft_garden_growth(rose)
diff --git a/py01/ex3/ft_plant_factory.py b/py01/ex3/ft_plant_factory.py
new file mode 100755
index 0000000..15669af
--- /dev/null
+++ b/py01/ex3/ft_plant_factory.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+
+# Create a class
+class Plant():
+
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ self.name_is = name
+ self.height_is = height
+ self.age_is = age
+
+# Method to grow the age
+ def age(self: 'Plant') -> None:
+ self.age_is += 1
+
+# Method to grow the height
+ def height(self: 'Plant') -> None:
+ self.height_is *= 1.04
+
+# Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(self.name_is + ": " + str(round(self.height_is, 1)) + "cm, "
+ + str(self.age_is) + " days old")
+
+
+Params = [("Rose", 25, 30),
+ ("Oak", 200, 365),
+ ("Cactus", 5, 90),
+ ("Sunflower", 80, 45),
+ ("Fern", 15, 120)]
+
+
+def ft_plant_factory() -> None:
+ print("=== Plant Factory Output ===")
+ for param in Params:
+ obj = Plant(param[0], param[1], param[2]) # instantiate the object
+ # obj = Plant(*param) # instantiate the object and unpack with *
+ print("Created: ", end='')
+ obj.show()
+
+
+if __name__ == "__main__":
+ ft_plant_factory()
diff --git a/py01/ex4/ft_garden_security.py b/py01/ex4/ft_garden_security.py
new file mode 100755
index 0000000..ef983b9
--- /dev/null
+++ b/py01/ex4/ft_garden_security.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+
+
+# Create a class
+class Plant():
+
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ if age < 0:
+ print("Plant age cannot be negative.")
+ print("Plant age rejected.")
+ print("Plant age was set to 0.")
+ self.name_is = name
+ self._height_is = height
+ self._age_is = 0
+ if height < 0:
+ print("Plant height cannot be negative.")
+ print("Plant height rejected.")
+ print("Plant height was set to 0.")
+ self.name_is = name
+ self._height_is = 0
+ self._age_is = age
+ if height > 0 and age > 0:
+ self.name_is = name
+ self._height_is = height
+ self._age_is = age
+
+# Method to grow the age
+ def grow_age(self: 'Plant') -> None:
+ self._age_is += 1
+
+# Method to grow the height
+ def grow_height(self: 'Plant') -> None:
+ self._height_is *= 1.04
+
+# Getter methods to access private properties
+ def get_age(self: 'Plant') -> int:
+ return self._age_is
+
+ def get_height(self: 'Plant') -> float:
+ return self._height_is
+
+# Setter methods to modify properties
+ def set_age(self: 'Plant', age: int) -> None:
+ if age > 0:
+ self._age_is = age
+ print("Plant age was updated.")
+ else:
+ print("Plant age cannot be negative.")
+ print("Plant age rejected.")
+
+ def set_height(self: 'Plant', height: float) -> None:
+ if height > 0:
+ self._height_is = round(height)
+ print("Plant height was updated.")
+ else:
+ print("Plant height cannot be negative.")
+ print("Plant height rejected.")
+
+# Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(self.name_is + ": " + str(self.get_height()) + "cm, "
+ + str(self.get_age()) + " days old")
+
+
+def ft_plant_security() -> None:
+ print("=== Garden Security System ===")
+ rose = Plant("Rose", -10, 5)
+ print("Plant created: ", end='')
+ rose.show()
+ print()
+ new_age = 10
+ new_height = 50
+ print(f"Trying to update height to {new_height} and age to {new_age}...")
+ print()
+ rose.set_height(new_height)
+ rose.set_age(new_age)
+ print()
+ print("Current state: ", end='')
+ rose.show()
+ print()
+ print("Updating height to 15 and age to 20...")
+ print()
+ rose.set_height(15)
+ rose.set_age(20)
+ print()
+ print("Current state: ", end='')
+ rose.show()
+
+
+if __name__ == "__main__":
+ ft_plant_security()
diff --git a/py01/ex5/ft_plant_types.py b/py01/ex5/ft_plant_types.py
new file mode 100755
index 0000000..d306117
--- /dev/null
+++ b/py01/ex5/ft_plant_types.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python3
+
+# Create a class
+class Plant():
+
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ self.name_is = name
+ self._height_is = height
+ self._age_is = age
+
+# Method to grow the age
+ def age(self: 'Plant') -> None:
+ self._age_is += 1
+
+# Method to grow the height
+ def grow(self: 'Plant') -> None:
+ self._height_is *= 1.02
+
+# Getter methods to access private properties
+ def get_age(self: 'Plant') -> int:
+ return self._age_is
+
+ def get_height(self: 'Plant') -> float:
+ return self._height_is
+
+# Setter methods to modify properties
+ def set_age(self: 'Plant', age: int) -> None:
+ if age > 0:
+ self._age_is = age
+ print("Plant age was updated.")
+ else:
+ print("Plant age cannot be negative.")
+
+ def set_height(self: 'Plant', height: int) -> None:
+ if height > 0:
+ self._height_is = round(height)
+ print("Plant height was updated.")
+ else:
+ print("Plant height cannot be negative.")
+
+# Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(self.name_is + ": " + str(round(self.get_height(), 1)) + "cm, "
+ + str(self.get_age()) + " days old")
+
+
+class Flower(Plant):
+
+ def __init__(self: 'Flower', name: str, height: int,
+ age: int, color: str) -> None:
+ super(). __init__(name, height, age)
+ self.color_is = color
+ self.isblooming = 0
+
+ def bloom(self: 'Flower') -> None:
+ self.isblooming = 1
+
+ def show(self: 'Flower') -> None:
+ super().show()
+ print(f" Color: {self.color_is}")
+ if self.isblooming == 1:
+ print(self.name_is + " is blooming beautifully!")
+ else:
+ print(self.name_is + " has not bloomed yet")
+
+
+class Tree(Plant):
+
+ def __init__(self: 'Tree', name: str, height: int, age: int,
+ trunk_diameter: int) -> None:
+ super(). __init__(name, height, age)
+ self.trunk_diameter_is = trunk_diameter
+ self.is_producing_shade = 0
+
+ def produce_shade(self: 'Tree') -> None:
+ self.is_producing_shade = 1
+ print(self.name_is + " now produces a shade of "
+ + str(self.get_height())
+ + "cm long and "
+ + str(round(self.trunk_diameter_is, 1))
+ + "cm wide.")
+
+ def show(self: 'Tree') -> None:
+ super().show()
+
+ print(f" Trunk diameter: {self.trunk_diameter_is}cm")
+
+
+class Vegetable(Plant):
+
+ def __init__(self: 'Vegetable', name: str, height: int, age: int,
+ harvest_season: str, nutritional_value: int) -> None:
+ super(). __init__(name, height, age)
+ self.harvest_season_is = harvest_season
+ self.nutritional_value_is = nutritional_value
+
+ def update_nutritional_value(self: 'Vegetable') -> None:
+ self.nutritional_value_is = 20
+
+ def show(self: 'Vegetable') -> None:
+ super().show()
+
+ print(f" Harvest season: {self.harvest_season_is}")
+ if self.get_age() >= 20:
+ self.update_nutritional_value()
+ print(f" Nutritional value: {self.nutritional_value_is}")
+
+
+if __name__ == "__main__":
+
+ print("=== Garden Plant Types ===")
+ print("=== Flower")
+ rose = Flower("Rose", 5, 15, "red")
+ rose.show()
+ print("[asking the rose to bloom]")
+ rose.bloom()
+ rose.show()
+ print("=== Tree")
+ oak = Tree("Oak", 200, 365, 5)
+ oak.show()
+ print("[asking the oak to produce shade]")
+ oak.produce_shade()
+ print("=== Vegetable")
+ tomato = Vegetable("Tomato", 5, 10, "April", 0)
+ tomato.show()
+ print("[make tomato grow and age for 20 days]")
+ for _ in range(20):
+ tomato.age()
+ tomato.grow()
+ tomato.update_nutritional_value()
+ tomato.show()
diff --git a/py01/ex6/ft_garden_analytics.py b/py01/ex6/ft_garden_analytics.py
new file mode 100755
index 0000000..8cd7ef0
--- /dev/null
+++ b/py01/ex6/ft_garden_analytics.py
@@ -0,0 +1,227 @@
+#!/usr/bin/env python3
+
+
+# Create a class
+class Plant():
+
+ # Create attributes of class Plant
+ def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
+ if not all([name, age, height]):
+ self.anon()
+ else:
+ self.name_is = name
+ self._height_is = height
+ self._age_is = age
+ # instantiating an object of Stat by append brackets to Stat
+ self.stat = self.Stat()
+
+ # if None is passed as a parameter
+ def anon(self: 'Plant') -> None:
+ self.name_is = "Unknown plant"
+ self._height_is = 0
+ self._age_is = 0
+ self.stat = self.Stat()
+
+# Create a nested class
+ class Stat:
+ def __init__(self) -> None:
+ # objects attributes
+ self._age_counter = 0
+ self._grow_counter = 0
+ self._show_counter = 0
+ self.produce_shade_counter = 0
+
+ def counter(self) -> None:
+ self._age_counter += 1
+ self._grow_counter += 1
+ self._show_counter += 1
+ self.produce_shade_counter += 1
+
+ # Method to grow the age
+ def age(self: 'Plant') -> None:
+ self._age_is += 1
+ self.stat._age_counter += 1
+
+# Method to grow the height
+ def grow(self: 'Plant') -> None:
+ self._height_is *= 1.02
+ self.stat._grow_counter += 1
+
+# Getter methods to access private properties
+ def get_age(self: 'Plant') -> int:
+ return self._age_is
+
+ def get_height(self: 'Plant') -> float:
+ return self._height_is
+
+# Setter methods to modify properties
+ def set_age(self: 'Plant', age: int) -> None:
+ if age > 0:
+ self._age_is = age
+ print("Plant age was updated.")
+ else:
+ print("Plant age cannot be negative.")
+
+ def set_height(self: 'Plant', height: int) -> None:
+ if height > 0:
+ self._height_is = round(height)
+ print("Plant height was updated.")
+ else:
+ print("Plant height cannot be negative.")
+
+ # static method does not take object
+ @staticmethod
+ def check_age(a) -> bool:
+ return a >= 365
+
+ # Method to show the plant information
+ def show(self: 'Plant') -> None:
+ print(f" {self.name_is}: {str(round(self.get_height(), 1))}cm,"
+ f" {str(self.get_age())} days old")
+ self.stat._show_counter += 1
+
+ @classmethod
+ def create_anonymous(cls) -> 'Plant':
+ return cls("Unknown plant", 0, 0)
+
+
+def show(stat) -> None:
+ print(f" Stats: {stat._grow_counter} grow,",
+ f"{stat._age_counter} age,",
+ f"{stat._show_counter} show")
+
+
+class Flower(Plant):
+
+ def __init__(self: 'Flower', name: str, height: int,
+ age: int, color: str) -> None:
+ super(). __init__(name, height, age)
+ # attributes
+ self.color = color
+ self.isblooming = 0
+
+ def bloom(self: 'Flower') -> None:
+ self.isblooming = 1
+
+ def show(self: 'Flower') -> None:
+ super().show()
+ print(f" Color: {self.color}")
+ if self.isblooming == 1:
+ print(f" {self.name_is} is blooming beautifully!")
+ else:
+ print(f" {self.name_is} has not bloomed yet")
+
+
+class Seed(Flower):
+ def __init__(self: 'Seed', name: str, height: int,
+ age: int, color: str) -> None:
+ super(). __init__(name, height, age, color)
+ self.color = color
+ self.has_seed = 0
+
+ def show(self: 'Seed') -> None:
+ super().show()
+ if self.isblooming == 1:
+ self.has_seed = 42
+ print(f" Seeds: {self.has_seed}")
+ else:
+ print(" Seeds: 0")
+
+
+class Tree(Plant):
+
+ def __init__(self: 'Tree', name: str, height: int,
+ age: int, trunk_diameter: int) -> None:
+ super(). __init__(name, height, age)
+ self.trunk_diameter = trunk_diameter
+ self.is_producing_shade = 0
+
+ def produce_shade(self: 'Tree') -> None:
+ self.is_producing_shade = 1
+ self.stat.produce_shade_counter += 1
+ print(f" {self.name_is} now produces a shade of,",
+ f"{str(self.get_height())}cm long",
+ f"and {str(round(self.trunk_diameter, 1))}cm wide.")
+
+ def show(self: 'Tree') -> None:
+ super().show()
+
+ print(f" Trunk diameter: {self.trunk_diameter}cm")
+
+ def stats(self: 'Tree') -> None:
+ print(f" Stats: {self.stat._grow_counter} grow,",
+ f"{self.stat._age_counter} age,",
+ f"{self.stat._show_counter} show,",
+ f"{self.stat.produce_shade_counter} shade")
+
+
+class Vegetable(Plant):
+
+ def __init__(self: 'Vegetable', name: str, height: int, age: int,
+ harvest_season: int, nutritional_value: int) -> None:
+ super(). __init__(name, height, age)
+ self.harvest_season_is = harvest_season
+ self.nutritional_value_is = nutritional_value
+
+ def update_nutritional_value(self: 'Vegetable') -> None:
+ self.nutritional_value_is = 20
+
+ def show(self: 'Vegetable') -> None:
+ super().show()
+
+ print(f" Harvest season: {self.harvest_season_is}")
+ if self.get_age() >= 20:
+ self.update_nutritional_value()
+ print(f" Nutritional value: {self.nutritional_value_is}")
+
+
+if __name__ == "__main__":
+
+ print("=== Garden Statistics ===")
+ print("=== Check year-old")
+ rose = Flower("Rose", 5, 30, "red")
+ print(" Is 30 days more than a year?",
+ f"-> {Plant.check_age(30)}")
+ rose = Flower("Rose", 5, 400, "red")
+ print(" Is 400 days more than a year?",
+ f"-> {Plant.check_age(444)}")
+ print()
+ print("=== Flower")
+ rose = Flower("Rose", 5, 15, "red")
+ rose.show()
+ print(" [statistics for Rose]")
+ show(rose.stat)
+ print(" [asking the rose to grow and bloom]")
+ rose.bloom()
+ rose.grow()
+ rose.show()
+ print(" [statistics for Rose]")
+ show(rose.stat)
+ print()
+ oak = Tree("Oak", 200, 365, 5)
+ print("=== Tree")
+ oak.show()
+ print(" [statistics for Oak]")
+ oak.stats()
+ print(" [asking the oak to produce shade]")
+ oak.produce_shade()
+ print(" [statistics for Oak]")
+ oak.stats()
+ print()
+ print("=== Seed")
+ sunflower = Seed("Sunflower", 80, 45, "yellow")
+ sunflower.show()
+ print(" [statistics for Sunflower]")
+ show(sunflower.stat)
+ print(" [make sunflower grow, age and bloom]")
+ sunflower.grow()
+ sunflower.bloom()
+ sunflower.age()
+ sunflower.show()
+ show(sunflower.stat)
+ print()
+ print("=== Anonymous")
+ anonymous = Plant.create_anonymous()
+ anonymous.show()
+ print(" [statistics for Unknown plant]")
+ show(anonymous.stat)