{% tabs %} {% tab title="Python" %}
class UndergroundSystem:
def __init__(self):
## 老子当dataframe 做
self.user_info = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
if id not in self.user_info:
table = {
"checkinT":[],
"checkoutT":[],
"checkinS":[],
"checkoutS":[],
}
table['checkinT'].append(t)
table['checkinS'].append(stationName)
self.user_info[id] = table
else:
self.user_info[id]['checkinT'].append(t)
self.user_info[id]['checkinS'].append(stationName)
def checkOut(self, id: int, stationName: str, t: int) -> None:
self.user_info[id]['checkoutT'].append(t)
self.user_info[id]['checkoutS'].append(stationName)
def getAverageTime(self, startStation: str, endStation: str) -> float:
time = 0
count = 0
for id in self.user_info.keys():
table = self.user_info[id]
for i in range(len(table["checkoutS"])):
if table["checkinS"][i] == startStation and table["checkoutS"][i]==endStation:
time += table['checkoutT'][i] - table['checkinT'][i]
count += 1
return time/count
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)
{% endtab %}
{% tab title="超时" %}
class UndergroundSystem:
def __init__(self):
## 老子当dataframe 做
self.user_info = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
if id not in self.user_info:
table = {
"checkinT":[],
"checkoutT":[],
"checkinS":[],
"checkoutS":[],
}
table['checkinT'].append(t)
table['checkinS'].append(stationName)
self.user_info[id] = table
else:
self.user_info[id]['checkinT'].append(t)
self.user_info[id]['checkinS'].append(stationName)
def checkOut(self, id: int, stationName: str, t: int) -> None:
self.user_info[id]['checkoutT'].append(t)
self.user_info[id]['checkoutS'].append(stationName)
def getAverageTime(self, startStation: str, endStation: str) -> float:
time = 0
count = 0
for id in self.user_info.keys():
table = self.user_info[id]
s = [i for i,v in enumerate(table["checkinS"]) if v == startStation]
e = [i for i,v in enumerate(table["checkoutS"]) if v == endStation]
idxs = list(set(s).intersection(set(e)))
for i in idxs:
time += table['checkoutT'][i] - table['checkinT'][i]
count += 1
return time/count
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)
{% endtab %} {% endtabs %}