小停車場有 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']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)
寫好了先點「執行」試試示例,沒問題再點「提交」。