# Output a flight schedule, formatted in military time

I have a CVRP problem that I solved in Python. There are 6 planes (T1-T6) and 3 airports (AUS,DAL,HOU). The airports have 1,2,3 gates respectively. Each airport has a minimum ground time of 25, 30, 35 minutes respectively- that each plane must wait after arrival before departing again. The planes can only fly between 0600 and 2200 (military time). The number of flights landing at any given airport at one time is constrained by the number of gates. I have a list called flight_schedule that has stored the initial 6 flights taking off at 0600 from each airport.

I have created a program that correctly outputs the flight schedule, however, I would like to condense it and make it "prettier" or more "pythonic" any suggestions would be greatly appreciated

Heres the code:

def military_time(minutes):
if minutes < 600:
h = minutes // 60
m = minutes % 60
return("{0:02d}{1:02d}".format(h, m))
elif 600 < minutes < 720:
h = minutes // 60
m = minutes % 60
return("{0:02d}{1:02d}".format(h, m))
elif minutes == 720:
h = 1200
return("1200")
else:
h = ((minutes-720) // 60) + 12
m = (minutes) % 60
return("{0:02d}{1:02d}".format(h, m))

start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

#Initialize the list
flights = []

#T1 AUS to DAL and back
while start < end:
flights.append(['T1', 'AUS', 'DAL', military_time(DepA), military_time(ArrD)])
DepD = ArrD + DW + 20
flights.append(['T1', 'DAL', 'AUS', military_time(DepD), military_time(ArrA)])
DepA = ArrA + AW + 20

#T2 DAL to HOU and back
start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

while start < end:
ArrH = DepD + DH
flights.append(['T2', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
DepH = ArrH + HW
ArrD= DepH + DH
flights.append(['T2', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
DepD = ArrD + DW
start = DepD + DH + HW + DH

#T3 HOU to AUS and back
start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

while start < end:
ArrA = DepH + AH
flights.append(['T3', 'HOU', 'AUS', military_time(DepH), military_time(ArrA)])
DepA = ArrA + AW + 30
ArrH= DepA + AH
flights.append(['T3', 'AUS', 'HOU', military_time(DepA), military_time(ArrH)])
DepH = ArrH + HW + 15
start = DepH + AH + HW + AH

#T4
start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

for i in range(10):
if start < end:
ArrH = DepD + DH
flights.append(['T4', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
DepH = ArrH + HW + 90
start = DepH + DH + DW
if start < end:
ArrD = DepH + DH
flights.append(['T4', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
DepD = ArrD + DW
start = DepD + DH + HW

#T5
start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360

for i in range(10):
if start < end:
ArrD = DepH + DH
flights.append(['T5', 'HOU', 'DAL', military_time(DepH), military_time(ArrD)])
DepD = ArrD + DW
start = DepD + DH + HW
if start < end:
ArrH = DepD + DH
flights.append(['T5', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
DepH = ArrH + HW + 90
start = DepH + DH + DW

#T6
start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360
T6DH = 455

for i in range(10):
if start < end:
ArrD = T6DH + DH
flights.append(['T6', 'HOU', 'DAL', military_time(T6DH), military_time(ArrD)])
DepD = ArrD + DW
start = DepD + DH+ HW
if start < end:
ArrH = DepD + DH
flights.append(['T6', 'DAL', 'HOU', military_time(DepD), military_time(ArrH)])
T6DH = ArrH + HW + 90
start = T6DH + DH + DW

#sort the flight schedule according to tail number and departure time
flight_schedule = sorted(flights, key = lambda x: x[0] + x[3])

print(flight_schedule)

file_name = 'flight_schedule.csv'

with open(file_name,'wt') as f:
for s in flight_schedule:
print(','.join(s), file=f)



1. If you're allowed to use other Python packages, the first thing I would suggest is using the Python datetime module in your military_time() function.

2. Try to name the variables into something useful, understandable, and unambiguous. Pretend you're writing code that you will read again in 6 months. AD is not a very useful variable name. Use something like time_AUS_DAL or flight_time_AUS_DAL. Don't sacrifice readability for brevity in code.

3. In any code, you should try to follow DRY (Don't Repeat Yourself) as much as possible. You have 6 loops, of which the first three are very similar and the last three are very similar. They're all essentially doing the same thing but with different parameters. You can thus refactor them into a function.

def get_entry(plane, airport1, airport2, start, end, flight_time1, flight_time2, ground_time1, ground_time2, ...):
# ... your code here ...
return [plane, airport1, airport2, ....]

4. Instead of redeclaring this chunk 6 times,

start = 360
end = 1320

# Flight times
AH = 45
DH = 65

#Ground times
AW = 25
DW = 30
HW = 35

#Start times
DepA = 360
DepD = 360
DepH = 360
ArrA = 360
ArrD = 360
ArrH = 360


do those calculations in local variables inside your refactored function. There's no need to redeclare DepA, DepD, etc, since you overwrite them in the while loop anyway.

1. You can also use lists/dictionaries to store this that chunk. You can do something like the following:

flight_times = {
'AD': 50, 'AH': 45, 'DH': 65
}
....


This allows you to iterate through it to generate your flight schedule.

• Thank you for your helpful suggestions @omgimanerd. As far as using datetime module, not allowed. Commented Jul 7, 2017 at 14:42