Kodewick
开始学 →
小挑战←→0 / 80

小停车场

#062小挑战类和对象列表约 7 分钟

小停车场有 spaces 个车位。写完 arrive(plate):还有空位才让车进来(车牌放进 self.cars),满了就不进。leave(plate):车在里面才拿掉,不在就什么也不做。

示例
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):
        # 在这里写:还有空位,就把车牌放进 self.cars
        pass

    def leave(self, plate):
        # 在这里写:车牌在 self.cars 里,就把它拿掉
        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 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。