From efb6e892b72c12820864e5c6d17cc1d19bc530ba Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Mon, 7 Jul 2025 19:35:31 +0200 Subject: [PATCH 1/6] solution for final project --- .../solution/daniele_fiocca/index.py | 86 ++++++++++++++ .../solution/daniele_fiocca/main.py | 4 + .../solution/daniele_fiocca/modules.py | 106 ++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 projects/final-project/solution/daniele_fiocca/index.py create mode 100644 projects/final-project/solution/daniele_fiocca/main.py create mode 100644 projects/final-project/solution/daniele_fiocca/modules.py diff --git a/projects/final-project/solution/daniele_fiocca/index.py b/projects/final-project/solution/daniele_fiocca/index.py new file mode 100644 index 0000000..48e7488 --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/index.py @@ -0,0 +1,86 @@ +from modules import (add_object, ambient_explore, delete_object, home_list, + modify_object, search_object) + + +def menu(): + while True: + print("\nMENU - Household Items Cataloger") + print("1. Explore environment (recursive)") + print("2. Add item manually") + print("3. Search items") + print("4. Edit item (recursive)") + print("5. Delete item (recursive)") + print("6. Show entire catalog") + print("7. Exit") + + selection = input("\nSelect an option (1-7): ").strip() + + if selection == "1": + starting_ambient = input("Insert the name of the first ambient: ") + if starting_ambient: + ambient_explore(starting_ambient) + else: + print("Ambient name cannot be empty.") + + elif selection == "2": + add_object() + + elif selection == "3": + print("\nFound object by:\n1. Name\n2. Position\n3. Category") + sub_selection = input("Select (1-3): ").strip() + if sub_selection == "1": + key = "name" + elif sub_selection == "2": + key = "position" + elif sub_selection == "3": + key = "category" + else: + print("Invalid selection") + continue + value = input(f"Insert value for {key}: ").strip() + results = search_object(key, value) + if results: + print("\nResults found:") + for obj in results: + print(f"- {obj}") + else: + print("Object not found") + + elif selection == "4": + key = input("Modify - Found by (name/position/category): ").strip() + if key not in ["name", "position", "category"]: + print("Invalid key. Choose: name, position or category.") + continue + value = input(f"Insert value for {key}: ").strip() + if not value: + print("Value cannot be empty.") + continue + modify_object(home_list, key, value) + + elif selection == "5": + key = input("Delete - Search by (name/position/category): ").strip().lower() + if key not in ["name", "position", "category"]: + print("Invalid key. Choose: name, position or category.") + continue + value = input(f"Insert value for {key}: ").strip() + if not value: + print("Value cannot be empty.") + continue + delete_object(home_list, key, value) + + elif selection == "6": + if not home_list: + print("Empty catalogue.") + else: + print("\nObject in catalogue:") + for idx, obj in enumerate(home_list, start=1): + print( + f"{idx}. Name: {obj['name']} | Position: {obj['position']} | Category: {obj['category']}" + ) + + elif selection == "7": + print("Thank you for using. Good By") + break + + else: + print("Invalid Selection.") diff --git a/projects/final-project/solution/daniele_fiocca/main.py b/projects/final-project/solution/daniele_fiocca/main.py new file mode 100644 index 0000000..dd853b6 --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/main.py @@ -0,0 +1,4 @@ +from index import menu + +if __name__ == "__main__": + menu() diff --git a/projects/final-project/solution/daniele_fiocca/modules.py b/projects/final-project/solution/daniele_fiocca/modules.py new file mode 100644 index 0000000..2d99821 --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/modules.py @@ -0,0 +1,106 @@ +home_list = [] + + +def control_validation_input(prompt): + while True: + validation = input(prompt).strip().lower() + if validation in ["y", "n"]: + return validation + print("Please enter 'y' or 'n'.") + + +def non_empty_input(prompt): + while True: + value = input(prompt).strip() + if value: + return value + print("Input cannot be empty.") + + +def ambient_explore(ambient): + if control_validation_input(f"Are there objects in {ambient}? (y/n)\n") == "y": + while True: + name = non_empty_input("Insert the name of the object: \n") + category = non_empty_input("Insert the category: \n") + home_list.append({"name": name, "position": ambient, "category": category}) + if ( + control_validation_input(f"Insert another object in {ambient}? (y/n)\n") + != "y" + ): + break + + while ( + control_validation_input( + f"Are there subsections (e.g. drawers, shelves) in {ambient}? (y/n)\n" + ) + == "y" + ): + sub_name = non_empty_input(f"Insert subsection name of {ambient}: \n").lower() + new_ambient = f"{ambient} > {sub_name}" + ambient_explore(new_ambient) + + +def add_object(): + name = non_empty_input("Insert the object name: \n") + position = non_empty_input("Insert object position: \n") + category = non_empty_input("Insert category: \n") + home_list.append({"name": name, "position": position, "category": category}) + print("Object created successfully!") + + +def search_object(key, value): + return [obj for obj in home_list if obj.get(key) == value] + + +def modify_object(object_list, key, value, index=0): + if index >= len(object_list): + print("No object to verify") + return + + home_object = object_list[index] + if home_object.get(key) == value: + print("\nObject Found:") + print( + f"Name: {home_object['name']} | Position: {home_object['position']} | Category: {home_object['category']}" + ) + + if control_validation_input("Would modify this object? (y/n): ") == "y": + print("Which field do you want to change?") + print("1.Name\n2. Position\n3. Category") + while True: + field = input("Choose (1-3): ").strip() + if field == "1": + new_key = "name" + break + elif field == "2": + new_key = "position" + break + elif field == "3": + new_key = "category" + break + else: + print("Invalid selection. Chose 1, 2 or 3.") + + new_value = non_empty_input(f"Insert new value for {new_key}: ") + home_object[new_key] = new_value + print("Object modify success!") + modify_object(object_list, key, value, index + 1) + + +def delete_object(object_list, key, value, index=0): + if index >= len(object_list): + print("End of list, no object to verify.") + return + home_object = object_list[index] + if home_object.get(key) == value: + print("Found Object:") + print( + f"Name: {home_object['name']} | Position: {home_object['position']} | Category: {home_object['category']}" + ) + + if control_validation_input("Delete this object? (y/n): ") == "y": + del object_list[index] + print("Object deleted.\n") + delete_object(object_list, key, value, index) + return + delete_object(object_list, key, value, index + 1) From 2159e008dad3641416b597ff7ff1421b39e6f414 Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Sun, 13 Jul 2025 18:05:51 +0200 Subject: [PATCH 2/6] solution for final project --- .../solution/daniele_fiocca/data.py | 3 + .../solution/daniele_fiocca/index.py | 120 ++++--------- .../daniele_fiocca/recursive_modules.py | 165 ++++++++++++++++++ .../solution/daniele_fiocca/utils.py | 47 +++++ 4 files changed, 253 insertions(+), 82 deletions(-) create mode 100644 projects/final-project/solution/daniele_fiocca/data.py create mode 100644 projects/final-project/solution/daniele_fiocca/recursive_modules.py create mode 100644 projects/final-project/solution/daniele_fiocca/utils.py diff --git a/projects/final-project/solution/daniele_fiocca/data.py b/projects/final-project/solution/daniele_fiocca/data.py new file mode 100644 index 0000000..58de539 --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/data.py @@ -0,0 +1,3 @@ +from typing import List, Dict + +object_list: List[Dict[str, str]] = [] diff --git a/projects/final-project/solution/daniele_fiocca/index.py b/projects/final-project/solution/daniele_fiocca/index.py index 48e7488..0c1923d 100644 --- a/projects/final-project/solution/daniele_fiocca/index.py +++ b/projects/final-project/solution/daniele_fiocca/index.py @@ -1,86 +1,42 @@ -from modules import (add_object, ambient_explore, delete_object, home_list, - modify_object, search_object) +from recursive_modules import (add_object, recursive_delete_object, + recursive_environment_exploration, + recursive_modify_object, show_objects) +from utils import validated_menu_choice def menu(): while True: - print("\nMENU - Household Items Cataloger") - print("1. Explore environment (recursive)") - print("2. Add item manually") - print("3. Search items") - print("4. Edit item (recursive)") - print("5. Delete item (recursive)") - print("6. Show entire catalog") - print("7. Exit") - - selection = input("\nSelect an option (1-7): ").strip() - - if selection == "1": - starting_ambient = input("Insert the name of the first ambient: ") - if starting_ambient: - ambient_explore(starting_ambient) - else: - print("Ambient name cannot be empty.") - - elif selection == "2": - add_object() - - elif selection == "3": - print("\nFound object by:\n1. Name\n2. Position\n3. Category") - sub_selection = input("Select (1-3): ").strip() - if sub_selection == "1": - key = "name" - elif sub_selection == "2": - key = "position" - elif sub_selection == "3": - key = "category" - else: - print("Invalid selection") - continue - value = input(f"Insert value for {key}: ").strip() - results = search_object(key, value) - if results: - print("\nResults found:") - for obj in results: - print(f"- {obj}") - else: - print("Object not found") - - elif selection == "4": - key = input("Modify - Found by (name/position/category): ").strip() - if key not in ["name", "position", "category"]: - print("Invalid key. Choose: name, position or category.") - continue - value = input(f"Insert value for {key}: ").strip() - if not value: - print("Value cannot be empty.") - continue - modify_object(home_list, key, value) - - elif selection == "5": - key = input("Delete - Search by (name/position/category): ").strip().lower() - if key not in ["name", "position", "category"]: - print("Invalid key. Choose: name, position or category.") - continue - value = input(f"Insert value for {key}: ").strip() - if not value: - print("Value cannot be empty.") - continue - delete_object(home_list, key, value) - - elif selection == "6": - if not home_list: - print("Empty catalogue.") - else: - print("\nObject in catalogue:") - for idx, obj in enumerate(home_list, start=1): - print( - f"{idx}. Name: {obj['name']} | Position: {obj['position']} | Category: {obj['category']}" - ) - - elif selection == "7": - print("Thank you for using. Good By") - break - - else: - print("Invalid Selection.") + try: + print("HOME OBJECTS REGISTRY\n") + print("1. Explore environment (recursive)") + print("2. Add single object") + print("3. Modify objects") + print("4. Delete objects") + print("5. Display all objects") + print("6. Exit") + + choice = validated_menu_choice( + "Choose an option", ["1", "2", "3", "4", "5", "6"] + ) + + if choice == "1": + print("Take note of all objects you see. Enter empty name to stop.") + recursive_environment_exploration() + elif choice == "2": + print("Manually add a single object:") + add_object() + elif choice == "3": + recursive_modify_object() + elif choice == "4": + recursive_delete_object() + elif choice == "5": + show_objects() + elif choice == "6": + print("Thanks for using. Goodbye") + break + + except KeyboardInterrupt: + print("\nOperation interrupted. Returning to main menu...") + except Exception as e: + print(f"Unexpected error: {e}") + print("Returning to main menu...") diff --git a/projects/final-project/solution/daniele_fiocca/recursive_modules.py b/projects/final-project/solution/daniele_fiocca/recursive_modules.py new file mode 100644 index 0000000..b79a30e --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/recursive_modules.py @@ -0,0 +1,165 @@ +from typing import Dict, List, Optional + +from utils import (confirm_operation, object_name_exists, validated_input, + validated_menu_choice) + +from data import object_list + + +def get_input() -> Optional[Dict[str, str]]: + name = validated_input("Enter object name (empty to stop)", False) + if name is None: + return None + + if object_name_exists(name): + print(f"Warning: an object with name '{name}' already exists!") + if not confirm_operation("Do you want continue anyway?"): + return None + + category = validated_input("Enter category") + if category is None: + return None + + environment = validated_input("Enter environment/room") + if environment is None: + return None + + return {"name": name, "category": category, "environment": environment} + + +def recursive_environment_exploration() -> List[Dict[str, str]]: + try: + obj = get_input() + if obj is None: + return object_list + else: + object_list.append(obj) + print(f"Object '{obj['name']}' added!") + return recursive_environment_exploration() + except KeyboardInterrupt: + print("\nOperation interrupted by user.") + return object_list + + +def show_objects(index: int = 0) -> None: + if not object_list: + print("No object to display.") + return + + if index == 0: + print(f"{'#':<3} {'Name':<20} {'Category':<20} {'Environment':<20}") + + if index >= len(object_list): + print(f"Total objects: {len(object_list)}") + return + + obj = object_list[index] + print( + f"{index + 1:<3} {obj['name']:<20} {obj['category']:<20} {obj['environment']:<20}" + ) + show_objects(index + 1) + + +def add_object() -> None: + try: + obj = get_input() + if obj: + object_list.append(obj) + print("Object added successfully!") + else: + print("Operation cancelled.") + except KeyboardInterrupt: + print("\nOperation interrupted by user.") + + +def recursive_object_index_search(searched_name: str, index: int = 0) -> int: + if not searched_name or not searched_name.strip(): + return -1 + if index >= len(object_list): + return -1 + if object_list[index]["name"].lower() == searched_name.lower(): + return index + return recursive_object_index_search(searched_name, index + 1) + + +def recursive_modify_object() -> None: + if not object_list: + print("No objects to modify.") + return + + try: + name_to_modify = validated_input("Enter name to search for") + if name_to_modify is None: + print("Operation cancelled.") + return + + object_index = recursive_object_index_search(name_to_modify) + + if object_index < 0 or object_index >= len(object_list): + print("Object not found.") + return + + obj = object_list[object_index] + print( + f"Object Found: {obj['name']} - {obj['category']} - {obj['environment']}" + ) + + print("\nMODIFY OBJECT") + print("1. Modify name") + print("2. Modify category") + print("3. Modify environment") + print("4. Stop modification") + + choice = validated_menu_choice("Choose what to modify", ["1", "2", "3", "4"]) + + if choice == "4": + return + elif choice == "1": + new_name = validated_input("Enter new name") + if new_name: + object_list[object_index]["name"] = new_name + print("Name modified successfully") + elif choice == "2": + new_category = validated_input("Enter new category") + if new_category: + object_list[object_index]["category"] = new_category + print("Category modified successfully.") + elif choice == "3": + new_environment = validated_input("Enter new environment") + if new_environment: + object_list[object_index]["environment"] = new_environment + if confirm_operation("Do you want to modify something else fot this object?"): + recursive_modify_object() + except KeyboardInterrupt: + print("\nOperation interrupted by user.") + + +def recursive_delete_object() -> None: + if not object_list: + print("No object to delete.") + return + + try: + name_to_delete = validated_input("Enter the name of the object to delete") + if name_to_delete is None: + print("Operation cancelled.") + return + + index = recursive_object_index_search(name_to_delete) + if index != -1: + obj = object_list[index] + print( + f"Object found: {obj['name']} - {obj['category']} - {obj['environment']}" + ) + + if confirm_operation("Are you sure you want delete this object?"): + deleted_object = object_list.pop(index) + print(f"Object '{deleted_object['name']}' deleted successfully!") + else: + print("Deletion cancelled") + else: + print("Object not found") + if confirm_operation("Do you want to search fot another object to delete?"): + recursive_delete_object() + except KeyboardInterrupt: + print("\nOperation interrupted by user.") diff --git a/projects/final-project/solution/daniele_fiocca/utils.py b/projects/final-project/solution/daniele_fiocca/utils.py new file mode 100644 index 0000000..765f7a8 --- /dev/null +++ b/projects/final-project/solution/daniele_fiocca/utils.py @@ -0,0 +1,47 @@ +from typing import List, Optional + +from data import object_list + + +def validated_input(message: str, required: bool = True) -> Optional[str]: + while True: + value = input(message + ": ").strip() + + if value == "" and not required: + return None + elif value == "" and required: + print("This field is required. Please try again.") + continue + elif not value.replace(" ", "").isalnum() and not all( + c.isalnum() or c.isspace() for c in value + ): + print( + "Value contains invalid characters. Use only letters, numbers and spaces." + ) + continue + else: + return value + + +def validated_menu_choice(message: str, valid_options: List[str]) -> str: + while True: + choice = input(message + ": ").strip() + if choice in valid_options: + return choice + else: + print(f"Invalid choice. Available options: {', '.join(valid_options)}") + + +def confirm_operation(message: str) -> bool: + while True: + response = input(message + " (y/n): ").strip().lower() + if response in ["y", "yes"]: + return True + elif response in ["n", "no"]: + return False + else: + print("Enter 'y' for yes or 'n' for no.") + + +def object_name_exists(name: str) -> bool: + return any(obj["name"].lower() == name.lower() for obj in object_list) From 2512ce7c82f25244b6972df1b48c302ef42a09fb Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Mon, 14 Jul 2025 18:25:26 +0200 Subject: [PATCH 3/6] refactor functions in index.py and utils.py. Now all the functions are recursive --- .../solution/daniele_fiocca/index.py | 71 ++++++++++--------- .../solution/daniele_fiocca/utils.py | 59 ++++++++------- 2 files changed, 67 insertions(+), 63 deletions(-) diff --git a/projects/final-project/solution/daniele_fiocca/index.py b/projects/final-project/solution/daniele_fiocca/index.py index 0c1923d..0e4837f 100644 --- a/projects/final-project/solution/daniele_fiocca/index.py +++ b/projects/final-project/solution/daniele_fiocca/index.py @@ -5,38 +5,43 @@ def menu(): - while True: - try: - print("HOME OBJECTS REGISTRY\n") - print("1. Explore environment (recursive)") - print("2. Add single object") - print("3. Modify objects") - print("4. Delete objects") - print("5. Display all objects") - print("6. Exit") + try: + print("HOME OBJECTS REGISTRY\n") + print("1. Explore environment (recursive)") + print("2. Add single object") + print("3. Modify objects") + print("4. Delete objects") + print("5. Display all objects") + print("6. Exit") - choice = validated_menu_choice( - "Choose an option", ["1", "2", "3", "4", "5", "6"] - ) + choice = validated_menu_choice( + "Choose an option", ["1", "2", "3", "4", "5", "6"] + ) - if choice == "1": - print("Take note of all objects you see. Enter empty name to stop.") - recursive_environment_exploration() - elif choice == "2": - print("Manually add a single object:") - add_object() - elif choice == "3": - recursive_modify_object() - elif choice == "4": - recursive_delete_object() - elif choice == "5": - show_objects() - elif choice == "6": - print("Thanks for using. Goodbye") - break - - except KeyboardInterrupt: - print("\nOperation interrupted. Returning to main menu...") - except Exception as e: - print(f"Unexpected error: {e}") - print("Returning to main menu...") + if choice == "1": + print("Take note of all objects you see. Enter empty name to stop.") + recursive_environment_exploration() + menu() + elif choice == "2": + print("Manually add a single object:") + add_object() + menu() + elif choice == "3": + recursive_modify_object() + menu() + elif choice == "4": + recursive_delete_object() + menu() + elif choice == "5": + show_objects() + menu() + elif choice == "6": + print("Thanks for using. Goodbye") + return + except KeyboardInterrupt: + print("\nOperation interrupted. Returning to main menu...") + menu() + except Exception as e: + print(f"Unexpected error: {e}") + print("Returning to main menu...") + menu() diff --git a/projects/final-project/solution/daniele_fiocca/utils.py b/projects/final-project/solution/daniele_fiocca/utils.py index 765f7a8..92728fa 100644 --- a/projects/final-project/solution/daniele_fiocca/utils.py +++ b/projects/final-project/solution/daniele_fiocca/utils.py @@ -4,43 +4,42 @@ def validated_input(message: str, required: bool = True) -> Optional[str]: - while True: - value = input(message + ": ").strip() - - if value == "" and not required: - return None - elif value == "" and required: - print("This field is required. Please try again.") - continue - elif not value.replace(" ", "").isalnum() and not all( + value: str = input(message + ": ").strip() + + if value == "" and not required: + return None + elif value == "" and required: + print("This field is required. Please try again.") + return validated_input(message, required) + elif not value.replace(" ", "").isalnum() and not all( c.isalnum() or c.isspace() for c in value - ): - print( - "Value contains invalid characters. Use only letters, numbers and spaces." - ) - continue - else: - return value + ): + print( + "Value contains invalid characters. Use only letters, numbers and spaces." + ) + return validated_input(message, required) + else: + return value def validated_menu_choice(message: str, valid_options: List[str]) -> str: - while True: - choice = input(message + ": ").strip() - if choice in valid_options: - return choice - else: - print(f"Invalid choice. Available options: {', '.join(valid_options)}") + choice: str = input(message + ": ").strip() + if choice in valid_options: + return choice + else: + print(f"Invalid choice. Available options: {', '.join(valid_options)}") + return validated_menu_choice(message, valid_options) def confirm_operation(message: str) -> bool: - while True: - response = input(message + " (y/n): ").strip().lower() - if response in ["y", "yes"]: - return True - elif response in ["n", "no"]: - return False - else: - print("Enter 'y' for yes or 'n' for no.") + response: str = input(message + " (y/n): ").strip().lower() + if response in ["y", "yes"]: + return True + elif response in ["n", "no"]: + return False + else: + print("Enter 'y' for yes or 'n' for no.") + return confirm_operation(message) def object_name_exists(name: str) -> bool: From bcc7384e86ff19187b5d7f0bd4d3a1b773420b84 Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Tue, 15 Jul 2025 19:55:43 +0200 Subject: [PATCH 4/6] deleted modules.py --- .../solution/daniele_fiocca/modules.py | 106 ------------------ 1 file changed, 106 deletions(-) delete mode 100644 projects/final-project/solution/daniele_fiocca/modules.py diff --git a/projects/final-project/solution/daniele_fiocca/modules.py b/projects/final-project/solution/daniele_fiocca/modules.py deleted file mode 100644 index 2d99821..0000000 --- a/projects/final-project/solution/daniele_fiocca/modules.py +++ /dev/null @@ -1,106 +0,0 @@ -home_list = [] - - -def control_validation_input(prompt): - while True: - validation = input(prompt).strip().lower() - if validation in ["y", "n"]: - return validation - print("Please enter 'y' or 'n'.") - - -def non_empty_input(prompt): - while True: - value = input(prompt).strip() - if value: - return value - print("Input cannot be empty.") - - -def ambient_explore(ambient): - if control_validation_input(f"Are there objects in {ambient}? (y/n)\n") == "y": - while True: - name = non_empty_input("Insert the name of the object: \n") - category = non_empty_input("Insert the category: \n") - home_list.append({"name": name, "position": ambient, "category": category}) - if ( - control_validation_input(f"Insert another object in {ambient}? (y/n)\n") - != "y" - ): - break - - while ( - control_validation_input( - f"Are there subsections (e.g. drawers, shelves) in {ambient}? (y/n)\n" - ) - == "y" - ): - sub_name = non_empty_input(f"Insert subsection name of {ambient}: \n").lower() - new_ambient = f"{ambient} > {sub_name}" - ambient_explore(new_ambient) - - -def add_object(): - name = non_empty_input("Insert the object name: \n") - position = non_empty_input("Insert object position: \n") - category = non_empty_input("Insert category: \n") - home_list.append({"name": name, "position": position, "category": category}) - print("Object created successfully!") - - -def search_object(key, value): - return [obj for obj in home_list if obj.get(key) == value] - - -def modify_object(object_list, key, value, index=0): - if index >= len(object_list): - print("No object to verify") - return - - home_object = object_list[index] - if home_object.get(key) == value: - print("\nObject Found:") - print( - f"Name: {home_object['name']} | Position: {home_object['position']} | Category: {home_object['category']}" - ) - - if control_validation_input("Would modify this object? (y/n): ") == "y": - print("Which field do you want to change?") - print("1.Name\n2. Position\n3. Category") - while True: - field = input("Choose (1-3): ").strip() - if field == "1": - new_key = "name" - break - elif field == "2": - new_key = "position" - break - elif field == "3": - new_key = "category" - break - else: - print("Invalid selection. Chose 1, 2 or 3.") - - new_value = non_empty_input(f"Insert new value for {new_key}: ") - home_object[new_key] = new_value - print("Object modify success!") - modify_object(object_list, key, value, index + 1) - - -def delete_object(object_list, key, value, index=0): - if index >= len(object_list): - print("End of list, no object to verify.") - return - home_object = object_list[index] - if home_object.get(key) == value: - print("Found Object:") - print( - f"Name: {home_object['name']} | Position: {home_object['position']} | Category: {home_object['category']}" - ) - - if control_validation_input("Delete this object? (y/n): ") == "y": - del object_list[index] - print("Object deleted.\n") - delete_object(object_list, key, value, index) - return - delete_object(object_list, key, value, index + 1) From 21b67b9ee2c40f7ab119859c7857ca633ebca78e Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Tue, 15 Jul 2025 20:58:02 +0200 Subject: [PATCH 5/6] refactor --- .gitignore | 2 ++ .../solution/daniele_fiocca/recursive_modules.py | 6 +++--- projects/final-project/solution/daniele_fiocca/utils.py | 6 ++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 3d5d40e..504fe76 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ # Created by https://www.toptal.com/developers/gitignore/api/node,go,java,intellij,visualstudiocode,haskell,erlang,elixir,rust # Edit at https://www.toptal.com/developers/gitignore?templates=node,go,java,intellij,visualstudiocode,haskell,erlang,elixir,rust +__pycache__/ +.idea/ ### Elixir ### /_build /cover diff --git a/projects/final-project/solution/daniele_fiocca/recursive_modules.py b/projects/final-project/solution/daniele_fiocca/recursive_modules.py index b79a30e..21c5745 100644 --- a/projects/final-project/solution/daniele_fiocca/recursive_modules.py +++ b/projects/final-project/solution/daniele_fiocca/recursive_modules.py @@ -8,8 +8,8 @@ def get_input() -> Optional[Dict[str, str]]: name = validated_input("Enter object name (empty to stop)", False) - if name is None: - return None + if not name: + return name if object_name_exists(name): print(f"Warning: an object with name '{name}' already exists!") @@ -30,7 +30,7 @@ def get_input() -> Optional[Dict[str, str]]: def recursive_environment_exploration() -> List[Dict[str, str]]: try: obj = get_input() - if obj is None: + if not obj: return object_list else: object_list.append(obj) diff --git a/projects/final-project/solution/daniele_fiocca/utils.py b/projects/final-project/solution/daniele_fiocca/utils.py index 92728fa..581e680 100644 --- a/projects/final-project/solution/daniele_fiocca/utils.py +++ b/projects/final-project/solution/daniele_fiocca/utils.py @@ -3,12 +3,10 @@ from data import object_list -def validated_input(message: str, required: bool = True) -> Optional[str]: +def validated_input(message: str, required: bool = True) -> str: value: str = input(message + ": ").strip() - if value == "" and not required: - return None - elif value == "" and required: + if value == "" and required: print("This field is required. Please try again.") return validated_input(message, required) elif not value.replace(" ", "").isalnum() and not all( From 7a1cfbbd7c6991e25bac61c46b9fa592814bb3bc Mon Sep 17 00:00:00 2001 From: Daniele Fiocca Date: Tue, 15 Jul 2025 21:04:15 +0200 Subject: [PATCH 6/6] test --- .../python/solution/daniele_fiocca/main.py | 16 ++++++++++++ .../python/solution/daniele_fiocca/main.py | 26 +++++++++++++++++++ .../python/solution/daniele_fiocca/main.py | 14 ++++++++++ 3 files changed, 56 insertions(+) create mode 100644 projects/001-total-the-values/python/solution/daniele_fiocca/main.py create mode 100644 projects/002-recursive-decimal-to-binary/python/solution/daniele_fiocca/main.py create mode 100644 projects/003-string-edit-distance/python/solution/daniele_fiocca/main.py diff --git a/projects/001-total-the-values/python/solution/daniele_fiocca/main.py b/projects/001-total-the-values/python/solution/daniele_fiocca/main.py new file mode 100644 index 0000000..4adc84c --- /dev/null +++ b/projects/001-total-the-values/python/solution/daniele_fiocca/main.py @@ -0,0 +1,16 @@ +def add_value(): + value = input("Insert a number") + + if value == "": + return 0.0 + else: + try: + number = float(value) + return number + add_value() + except ValueError: + print("Not a number") + return add_value() + + +total = add_value() +print("The total is", total) diff --git a/projects/002-recursive-decimal-to-binary/python/solution/daniele_fiocca/main.py b/projects/002-recursive-decimal-to-binary/python/solution/daniele_fiocca/main.py new file mode 100644 index 0000000..7353fd6 --- /dev/null +++ b/projects/002-recursive-decimal-to-binary/python/solution/daniele_fiocca/main.py @@ -0,0 +1,26 @@ +def decimal_to_binary(number): + + if number == 0: + return "0" + elif number == 1: + return "1" + else: + return decimal_to_binary(number // 2) + str(number % 2) + + +def main(): + try: + number = int(input("Enter a number: ")) + if number < 0: + raise ValueError("Number cannot be negative") + else: + binary = decimal_to_binary(number) + print(binary) + except ValueError: + print("Invalid input") + +main() + + + + diff --git a/projects/003-string-edit-distance/python/solution/daniele_fiocca/main.py b/projects/003-string-edit-distance/python/solution/daniele_fiocca/main.py new file mode 100644 index 0000000..fc909ed --- /dev/null +++ b/projects/003-string-edit-distance/python/solution/daniele_fiocca/main.py @@ -0,0 +1,14 @@ +def distance(s, t): + if len(s) == 0: + return len(t) + elif len(t) == 0: + return len(s) + else: + cost = 0 if s[-1] == t[-1] else 1 + d1 = distance(s[:-1], t) + 1 + d2 = distance(s, t[:-1]) + 1 + d3 = distance(s[:-1], t[:-1]) + cost + return min(d1, d2, d3) + + +print(distance("prototype", "probleming"))