-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_admin.py
209 lines (180 loc) · 8.65 KB
/
package_admin.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from random import choice
class Package():
def __init__(self, id, houseCoord, zipNumber, blockNumber, streetAddress, houseNumber):
self.id = id
self.houseCoord = houseCoord
self.zipNumber = zipNumber
self.blockNumber = blockNumber
self.streetAddress = streetAddress
self.houseNumber = houseNumber
def __repr__(self):
return str(self.id)
def __str__(self):
return str(self.id)
def __eq__(self, package):
return self.id == package.id
#Agregar que regrese paquetes entregados y paquetes activos
class PackageAdmin():
ZIP_CODES = ["27018", "44789", "89943"]
def __init__(self):
self.packagesToDeliver = []
self.houses = []
self.packagesAdm = {
#Zip Codes
"27018": {
#Blocks
"1": {
#Streets with homes
"1W": [],
"1N": [],
"1E": [],
"1S": [],
},
"2": {
#Streets with homes
"2W": [],
"2N": [],
"2E": [],
"2S": [],
},
"4":{
#Streets with homes
"4W": [],
"4N": [],
"4E": [],
"4S": [],
}
},
"44789": {
#Blocks
"5": {
#Streets with homes
"5W": [],
"5N": [],
"5E": [],
"5S": [],
},
"6": {
#Streets with homes
"6W": [],
"6N": [],
"6E": [],
"6S": [],
},
"3":{
#Streets with homes
"3W": [],
"3N": [],
"3E": [],
"3S": [],
}
},
"89943": {
#Blocks
"7": {
#Streets with homes
"7W": [],
"7N": [],
"7E": [],
"7S": [],
},
"8": {
#Streets with homes
"8W": [],
"8N": [],
"8E": [],
"8S": [],
},
"9":{
#Streets with homes
"9W": [],
"9N": [],
"9E": [],
"9S": [],
}
}
}
def selectPackagesForDelivery(self, limit = 10):
selectedPackagesNumber = 0
selectedPackages = {}
#look for packages until we fill the delivery vehicle or we run out of packages
if(len(self.packagesToDeliver) > 0):
#traverse packages to deliver FIFO
for package in self.packagesToDeliver: #O(n), total packages
#get package address information
packageZipNum = package.zipNumber
packageBlockNum = package.blockNumber
packageStreetAddress = package.streetAddress
#add package to selected packages for current delivery
selectedPackages[package.id] = package #add to packages to deliver
selectedPackagesNumber += 1 #increment packages selected indicator
self.removePackage(package) #remove package from packages
#Select packages near the current package delivery zone
#Look for packages in the same street
for p in self.packagesAdm[packageZipNum][packageBlockNum][packageStreetAddress]: #O(n), total packages
#check we have not reached limit and there exists packages to be delivered
if(selectedPackagesNumber < limit and len(self.packagesToDeliver) > 0):
#add package
selectedPackages[p.id] = p #add to packages to deliver
selectedPackagesNumber += 1 #increment packages selected indicator
#remove package from packages
self.removePackage(p) #O(n)
else:
return selectedPackages
#Look for packages in the same block
for street, packages in self.packagesAdm[packageZipNum][packageBlockNum].items(): #O(1), always 4 streets
#we skip current street
if(street != packageStreetAddress):
#check packages on each street
for p in packages: #O(n), total packages
#check we have not reached limit and there exists packages to be delivered
if(selectedPackagesNumber < limit and len(self.packagesToDeliver) > 0):
#add package
selectedPackages[p.id] = p #add to packages to deliver
selectedPackagesNumber += 1 #increment packages selected indicator
#remove package from packages
self.removePackage(p) #O(n)
else:
return selectedPackages
#Look for packages in the same zipCode
for block, streets in self.packagesAdm[packageZipNum].items(): #O(1) checking 2 blocks always
#skip block we have already checked
if(block != packageBlockNum):
#on each street
for street in streets: #O(1) checking 4 streets always
#look for packages
for p in self.packagesAdm[packageZipNum][block][street]: #O(n), number of packages
if(selectedPackagesNumber < limit and len(self.packagesToDeliver) > 0):
#add package
selectedPackages[p.id] = p #add to packages to deliver
selectedPackagesNumber += 1 #increment packages selected indicator
#remove package from packages
self.removePackage(p) #O(n)
else:
return selectedPackages
return selectedPackages
def removePackage(self, package):
#remove from list of packages to be delivered
packageIdx = self.packagesToDeliver.index(package)
self.packagesToDeliver.pop(packageIdx) #O(n) total packages
#remove from orders administration
ordersAdminPackageIdx = self.packagesAdm[package.zipNumber][package.blockNumber][package.streetAddress].index(package)
self.packagesAdm[package.zipNumber][package.blockNumber][package.streetAddress].pop(ordersAdminPackageIdx) #O(n)
def createHousePackages(self, numOrders = 1):
for _ in range(numOrders):
#select random house
house = choice(self.houses)
#create package
package = house.create_package()
#add package
self.packagesAdm[house.zipNumber][house.blockNumber][house.streetAddress].append(package)
self.packagesToDeliver.append(package) #packagesToDeliver, O(n)
def housesWithPendingPackages(self): # Returns a dictionary that associates a house coordiante with the number of packages
# assigned to that house, for every house that is currently expecting a delivery: {'houseCoord': numPackages, ...}
housesPenPackages = {}
for house in self.houses:
if len(house.packages) > 0:
housesPenPackages[house.houseCoord] = len(house.packages)
return housesPenPackages
def numPackagesToDeliver(self):
return len(self.packagesToDeliver)