XClose

COMP0233: Research Software Engineering With Python

Home
Menu

Solution: counting people in the maze

With this maze structure:

In [1]:
house = {
    'living' : {
        'exits': {
            'north' : 'kitchen',
            'outside' : 'garden',
            'upstairs' : 'bedroom'
        },
        'people' : ['Graham'],
        'capacity' : 2
    },
    'kitchen' : {
        'exits': {
            'south' : 'living'
        },
        'people' : [],
        'capacity' : 1
    },
    'garden' : {
        'exits': {
            'inside' : 'living'
        },
        'people' : ['David'],
        'capacity' : 3
    },
    'bedroom' : {
        'exits': {
            'downstairs' : 'living',
            'jump' : 'garden'
        },
        'people' : [],
        'capacity' : 1
    }
}

We can count the occupants and capacity like this:

In [2]:
capacity = 0
occupancy = 0
for name, room in house.items():
    capacity += room['capacity']
    occupancy += len(room['people'])
print(f"House can fit {capacity} people, and currently has: {occupancy}.")
House can fit 7 people, and currently has: 2.