-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatorTaxi.py
124 lines (108 loc) · 4.68 KB
/
gatorTaxi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
from RBT import RBT
from minHeap import MinHeap,Ride
class gatorTaxi:
def __init__(self):
self.min_heap = MinHeap()
self.rbt = RBT()
#insert a ride node
def insert_ride(self, rideNumber, rideCost, tripDuration,output_file):
if self.rbt.search(rideNumber) is None:
self.min_heap.insert(rideNumber, rideCost, tripDuration)
self.rbt.insert(rideNumber, rideCost, tripDuration)
else:
# print("Duplicate RideNumber.")
output_file.write("Duplicate RideNumber\n")
#print a specific ride using ride number
def print_ride(self, rideNumber, output_file):
node = self.rbt.search(rideNumber)
if node is None:
# print("(0,0,0)")
output_file.write("(0,0,0)\n")
else:
# print(f"({node.rideNumber},{node.rideCost},{node.tripDuration})")
output_file.write(f"({node.rideNumber},{node.rideCost},{node.tripDuration})\n")
#print rides on a specific range
def print_rides(self, rideNumber1, rideNumber2, output_file):
nodes = self.rbt.search_range(rideNumber1, rideNumber2)
if len(nodes) == 0:
# print("(0,0,0)")
output_file.write("(0,0,0)\n")
else:
ride_str = ""
for node in nodes:
ride_str += f"({node.rideNumber},{node.rideCost},{node.tripDuration}),"
ride_str = ride_str[:-1] # remove trailing comma and space
# print(ride_str)
output_file.write(ride_str + "\n")
#update the Ride if destination changed
def update_trip_duration(self, rideNumber, newTripDuration):
node = self.rbt.search(rideNumber)
if node is None:
return
if newTripDuration <= node.tripDuration:
node.tripDuration = newTripDuration
self.min_heap.update(node.rideNumber, node.rideCost, node.tripDuration)
return
elif newTripDuration <= 2 * node.tripDuration:
node.rideCost += 10
node.tripDuration = newTripDuration
self.min_heap.update(node.rideNumber, node.rideCost, node.tripDuration)
else:
self.cancel_ride(rideNumber)
#delete the ride using ride number
def cancel_ride(self, rideNumber):
node = self.rbt.search(rideNumber)
if node is None:
return
self.rbt.delete(node)
self.min_heap.delete(rideNumber)
#get the ride with lowet cost
def get_next_ride(self, output_file):
node = self.min_heap.remove_min()
if node is None:
# print("No active ride requests")
output_file.write("No active ride requests\n")
else:
# print(f"({node.rideNumber},{node.rideCost},{node.tripDuration})")
output_file.write(f"({node.rideNumber},{node.rideCost},{node.tripDuration})\n")
self.rbt.delete(self.rbt.search(node.rideNumber))
def main():
if len(sys.argv) < 2:
# print("Enter input and output file names and try again!!")
return
input_file= sys.argv[1]
output_file = "output_file.txt"
with open(input_file, "r") as f, open(output_file, "w") as output_file:
lines =f.readlines()
for line in lines:
command, args = line.strip().split('(')
args = (args.replace(')',""))
#using if - else statement to check the commannd
if command != "GetNextRide":
argsList = list(map(int, args.split(',')))
if command == "Insert":
rideNumber = int(argsList[0])
rideCost = int(argsList[1])
tripDuration = int(argsList[2])
gator_taxi.insert_ride(rideNumber, rideCost, tripDuration, output_file)
elif command == "Print":
if len(argsList) == 1:
rideNumber = int(argsList[0])
gator_taxi.print_ride(rideNumber, output_file)
elif len(argsList) == 2:
rideNumber1 = int(argsList[0])
rideNumber2 = int(argsList[1])
gator_taxi.print_rides(rideNumber1, rideNumber2, output_file)
elif command == "UpdateTrip":
rideNumber = int(argsList[0])
newTripDuration = int(argsList[1])
gator_taxi.update_trip_duration(rideNumber, newTripDuration)
elif command == "GetNextRide":
gator_taxi.get_next_ride(output_file)
elif command == "CancelRide":
rideNumber = int(argsList[0])
gator_taxi.cancel_ride(rideNumber)
if __name__ == "__main__":
gator_taxi = gatorTaxi()
main()