summaryrefslogtreecommitdiff
path: root/py03/ex4/ft_inventory_system.py
blob: 104652a174dc99cb505491611c54f9ddfd9b7ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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()