Photo by REVOLT / Unsplash


Create a inventory search function for your friends shoe store
it should answer with available inventory OR ' ... not available'
search should be lower/ upper-case agnostic
brands = ['Nike', 'Adidas', 'Asics', 'Hoka', 'Other(Puma, Reebok, Saucony)']
inventory = [180, 395, 250, 170, 45, 30, 40]
>> Inventory search[enter brand]: NIKe
>> Nike: 180 pairs in stock
>> Inventory search[enter brand]: New Balance
>> Brand not available (New Balance)
# Bonus, add detailed handling for Other
>> Inventory search[enter brand]: other
>> Other(Puma, Reebok, Saucony): 115 pairs in stock
Have fun and remember to share code in an ’easy-to-copy’ format to make everyones life more wonderful πŸ₯³

Solution in Python3

# Define a list of brands and their respective inventory
brands = ['Nike', 'Adidas', 'Asics', 'Hoka', 'Other(Puma, Reebok, Saucony)']
inventory = [180, 395, 250, 170, 45, 30, 40]

# Define an empty dictionary to hold the stock information
stock = dict()

# Define a function to add a brand and its count to the stock dictionary
def add_to_stock(key, brand, count):
    stock[key] = {
        "label": brand,
        "count": count

# Iterate through the list of brands and add them to the stock dictionary
for i, brand in enumerate(brands):
    # If the brand does not have multiple sub-brands, add it directly to the dictionary
    if not brand.endswith(")"):
        count = inventory[i]
        add_to_stock(key=brand.lower(), brand=brand, count=count)
    # If the brand has multiple sub-brands, add them individually to the dictionary
        other_brands = brand[6:-1].split(", ")
        other_count = 0
        for j, other_brand in enumerate(other_brands):
            count = inventory[i+j]
                         brand=other_brand, count=count)
            other_count += count
        # Add the total count of all sub-brands to the "other" key in the dictionary
        add_to_stock(key="other", brand=brand, count=other_count)

# Continuously prompt the user to enter a brand and display its count in stock
while True:
    brand = input("Inventory search[enter brand]: ")
    # If the user enters an empty string, break out of the loop
    if not brand:
    key = brand.lower()
    # If the brand is in the stock dictionary, display its count
    if key in stock:
        label = stock[key]["label"]
        count = stock[key]["count"]
        print(f"{label}: {count} pairs in stock")
    # If the brand is not in the stock dictionary, display an error message
        print(f"Brand not available ({brand})")

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]