Kodewick
Start learning →
Mini challenge←→0 / 80

Small car park

#062Mini challengeClassesLists7 min

A small car park has spaces spaces. Finish arrive(plate): let the car in (put the plate into self.cars) only if there's a free space. Finish leave(plate): take the plate out only if that car is inside; otherwise do nothing.

Examples
still_parked(2, [("in", "A1"), ("in", "B2"), ("in", "C3")])→['A1', 'B2']
still_parked(2, [("in", "A1"), ("out", "A1"), ("in", "C3")])→['C3']
main.py
class ParkingLot:
    def __init__(self, spaces):
        self.spaces = spaces
        self.cars = []

    def arrive(self, plate):
        # your code here: if there's a free space, put the plate into self.cars
        pass

    def leave(self, plate):
        # your code here: if the plate is in self.cars, take it out
        pass


def still_parked(spaces, events):
    lot = ParkingLot(spaces)
    for action, plate in events:
        if action == "in":
            lot.arrive(plate)
        else:
            lot.leave(plate)
    return sorted(lot.cars)
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.