From 9808196ebbe8d815c3fa2458462f58a9349c87c2 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sun, 24 Apr 2022 22:32:12 +0430 Subject: update --- 1396/main.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 1396/main.go (limited to '1396/main.go') diff --git a/1396/main.go b/1396/main.go new file mode 100644 index 0000000..2af27fd --- /dev/null +++ b/1396/main.go @@ -0,0 +1,66 @@ +package main + +import "fmt" + +type Trip struct { + station string + t int +} + +type Info struct { + count int + sum int +} + +type UndergroundSystem struct { + checkInTable map[int]Trip + timeTable map[string]Info +} + +func Constructor() UndergroundSystem { + var us UndergroundSystem + us.checkInTable = make(map[int]Trip) + us.timeTable = make(map[string]Info) + return us +} + +func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) { + this.checkInTable[id] = Trip{station: stationName, t: t} +} + +func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) { + begin := this.checkInTable[id].station + key := begin + ":" + stationName + time := t - this.checkInTable[id].t + entry, _ := this.timeTable[key] + entry.count++ + entry.sum += time + this.timeTable[key] = entry +} + +func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 { + key := startStation + ":" + endStation + return float64(this.timeTable[key].sum) / float64(this.timeTable[key].count) + +} + +/** + * Your UndergroundSystem object will be instantiated and called as such: + * obj := Constructor(); + * obj.CheckIn(id,stationName,t); + * obj.CheckOut(id,stationName,t); + * param_3 := obj.GetAverageTime(startStation,endStation); + */ + +func main() { + undergroundSystem := Constructor() + undergroundSystem.CheckIn(10, "Leyton", 3) + undergroundSystem.CheckOut(10, "Paradise", 8) // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5 + fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 5.00000, (5) / 1 = 5 + undergroundSystem.CheckIn(5, "Leyton", 10) + undergroundSystem.CheckOut(5, "Paradise", 16) // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6 + fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 5.50000, (5 + 6) / 2 = 5.5 + undergroundSystem.CheckIn(2, "Leyton", 21) + undergroundSystem.CheckOut(2, "Paradise", 30) // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9 + fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 6.66667, (5 + 6 + 9) / 3 = 6.66667 +} -- cgit v1.2.3