primeiro commit com exercicios de Exercism realizados até esta data

This commit is contained in:
2026-10-17 22:10:27 +01:00
commit 58fccbe27a
708 changed files with 35038 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
"""Functions to keep track and alter inventory."""
def create_inventory(items):
"""Create a dict that tracks the amount (count) of each element on the `items` list.
:param items: list - list of items to create an inventory from.
:return: dict - the inventory dictionary.
"""
inventory: dict = {}
for item in items:
inventory[item] = items.count(item)
return inventory
def add_items(inventory, items):
"""Add or increment items in inventory using elements from the items `list`.
:param inventory: dict - dictionary of existing inventory.
:param items: list - list of items to update the inventory with.
:return: dict - the inventory updated with the new items.
"""
# inventory is empty, create new one and return inventory
if inventory == {}:
return create_inventory(items)
# increment items that already exists in inventory
for item in inventory:
inventory[item] += items.count(item)
# add new items that doesnt exist in inventory
# the new items can only be added after incrementing items that already
# exists in inventory to avoid adding the new items again
for item in items:
if item not in inventory:
inventory[item] = items.count(item)
return inventory
def decrement_items(inventory, items):
"""Decrement items in inventory using elements from the `items` list.
:param inventory: dict - inventory dictionary.
:param items: list - list of items to decrement from the inventory.
:return: dict - updated inventory with items decremented.
"""
for item in inventory:
if item in items:
inventory[item] -= items.count(item)
if inventory[item] < 0:
inventory[item] = 0
return inventory
def remove_item(inventory, item):
"""Remove item from inventory if it matches `item` string.
:param inventory: dict - inventory dictionary.
:param item: str - item to remove from the inventory.
:return: dict - updated inventory with item removed. Current inventory if item does not match.
"""
if item in inventory:
inventory.pop(item)
return inventory
def list_inventory(inventory):
"""Create a list containing only available (item_name, item_count > 0) pairs in inventory.
:param inventory: dict - an inventory dictionary.
:return: list of tuples - list of key, value pairs from the inventory dictionary.
"""
inventory_list: list[tuple] = []
# conventional
# for item in inventory:
# if inventory[item] > 0:
# inventory_list.append((item, inventory[item]))
# using list comprehension
inventory_list = [
(item, inventory[item]) for item in inventory if inventory[item] > 0
]
return inventory_list