Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.1 kB
5
Indexable
Never
def stock_fruits(warehouses):
    total_tons = 0

    for key, value in warehouses.items():
        if isinstance(value, dict):
            total_tons += stock_fruits(value)
        elif isinstance(value, list):
            for fruit in value:
                total_tons += fruit["tons"]

    return total_tons


warehouses = {
    "Warehouse Lisboa": [
        {"name": "apples", "tons": 4},
        {"name": "oranges", "tons": 10},
        {"name": "lemons", "tons": 50}
    ],
    "Warehouse Cascais": {
        "Branch 1": [
            {"name": "apples", "tons": 10},
            {"name": "oranges", "tons": 24}
        ],
        "Branch 2": [
            {"name": "apples", "tons": 16},
            {"name": "oranges", "tons": 8}
        ]
    },
    "Warehouse Oeiras": {
        "Branch 1": {
            "Sub Branch 1": {
                "Sub sub Branch 1": [
                    {"name": "lemons", "tons": 10}
                ]
            },
            "Branch 2": [
                {"name": "apples", "tons": 3}
            ]
        }
    }
}

total_tons = stock_fruits(warehouses)
print(total_tons)