summaryrefslogtreecommitdiff
path: root/py03/ex4
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-06-07 08:59:04 +0200
committeryctct <yctct@yctct.com>2026-06-07 08:59:04 +0200
commit15115b4c52bfda0d1cca9fa1155beecbb873ec35 (patch)
treeb3f0975e63eb04dcba732a78ce9bd9abda8acf01 /py03/ex4
First commit, add all files
Diffstat (limited to 'py03/ex4')
-rwxr-xr-xpy03/ex4/ft_inventory_system.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/py03/ex4/ft_inventory_system.py b/py03/ex4/ft_inventory_system.py
new file mode 100755
index 0000000..104652a
--- /dev/null
+++ b/py03/ex4/ft_inventory_system.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+# https://www.freecodecamp.org/learn/python-v9/review-dictionaries-and-sets/review-dictionaries-and-sets
+
+import sys
+
+
+def ft():
+ print("=== Inventory System Analysis ===")
+ # check for when there are no params given
+ if len(sys.argv) < 2:
+ print("Enter items following this format: <item_name>:<quantity>")
+ return
+ # initialize dictionary
+ inventory = {}
+ # init list to check for duplicates
+ seen = []
+ for arg in sys.argv[1:]:
+ try:
+ item, quantity = arg.split(':')
+ # print redundant item.s
+ if item in seen:
+ print(f"Redundant item '{item}' - discaring")
+ else:
+ seen.append(item)
+ try:
+ # check that quantities are int
+ int(quantity)
+ if int(quantity) < 0:
+ raise ValueError("quantity cannot be negative")
+ except ValueError as err:
+ print(f"Quantity error for '{item}': {err}")
+ else:
+ # populate dict
+ inventory[item] = quantity
+ except ValueError:
+ print(f"Error - invalid parameter '{arg.rstrip()}'")
+ if arg == sys.argv[-1] and arg == sys.argv[1]:
+ return
+ # check for when there are many invalid param and no valid params
+ if not seen:
+ return
+ if not inventory:
+ return
+ print(f"Got inventory: {inventory}")
+ item_list = []
+ for item in inventory.keys():
+ item_list.append(item)
+ print(f"Item list: {item_list}")
+ total_quantity = 0
+ for quantity in inventory.values():
+ total_quantity += int(quantity)
+ print(f"Total quantity for the "
+ f"{len(inventory.values())} items: {total_quantity}")
+ # compute percentages
+ for key in inventory:
+ per_of_total = round(100 * (int(inventory[key]) / total_quantity), 1)
+ print(f"Item {key} represents {per_of_total}%")
+ # look for item with max quantity
+ max_value = 0
+ max_key = item_list[0]
+ for key in inventory:
+ if max_value == 0:
+ max_value = int(inventory[key])
+ if int(inventory[key]) > max_value:
+ max_value = int(inventory[key])
+ max_key = key
+ print(f"Item most abundant: {max_key} with quantity {max_value}")
+ # look for item with min quantity
+ min_value = 0
+ min_key = item_list[0]
+ for key in inventory:
+ if min_value == 0:
+ min_value = int(inventory[key])
+ if int(inventory[key]) < min_value:
+ min_value = int(inventory[key])
+ min_key = key
+ print(f"Item least abundant: {min_key} with quantity {min_value}")
+ inventory.update({'magic item': '1'})
+ print(inventory)
+
+
+if __name__ == "__main__":
+ ft()