Skip to content

Instantly share code, notes, and snippets.

@lukeramsden
Created May 17, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeramsden/b194e94718b4006a7e4b171aa08197cb to your computer and use it in GitHub Desktop.
Save lukeramsden/b194e94718b4006a7e4b171aa08197cb to your computer and use it in GitHub Desktop.
2019 May/June CIE IGCSE 0478/21 Pre-release Tasks
day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
short_day_names = list(map(lambda d: d[:3], day_names))
route_names = ['Bus A', 'Bus B', 'Bus C', 'Bus D', 'Bus E', 'Bus F']
supplied_data = {
0: { # week 1
day_names[0]: [0, 0, 2, 1, -1, 0], # monday
day_names[1]: [0, 1, 0, 0, -1, -5], # tuesday
day_names[2]: [0, 0, -1, 0, -1, -5], # wednesday
day_names[3]: [2, 0, -1, 0, -2, -5], # thursday
day_names[4]: [2, 1, -2, 0, -4, -4] # friday
},
1: { # week 2
day_names[0]: [4, 2, -2, 0, -10, -3], # monday
day_names[1]: [0, 0, -3, 0, -2, -5], # tuesday
day_names[2]: [3, 0, -1, 0, 0, 0], # wednesday
day_names[3]: [4, 0, 0, 0, 0, 0], # thursday
day_names[4]: [-2, 0, 0, 0, 0, 0] # friday
},
2: { # week 3
day_names[0]: [-5, 1, -2, 2, 0, 0], # monday
day_names[1]: [0, 0, 0, 0, 1, -2], # tuesday
day_names[2]: [0, 0, 1, 0, 2, -3], # wednesday
day_names[3]: [3, 0, 1, 0, -3, 1], # thursday
day_names[4]: [4, 2, 1, 0, 1, 1] # friday
},
3: { # week 4
day_names[0]: [-1, 0, 1, 0, 1, 1], # monday
day_names[1]: [8, 0, -1, 0, 3, 0], # tuesday
day_names[2]: [1, 1, -1, 0, -1, 0], # wednesday
day_names[3]: [1, 0, 2, 0, 0, -2], # thursday
day_names[4]: [-2, 0, -2, 0, 0, -5] # friday
}
}
current_command = 0
def menu(data_supplied) -> int:
MAX_COMMAND = 5
print("1. Use supplied data")
print("2. Input your own data")
if data_supplied:
print("3. Output statistics")
print("4. Check specific day")
print(f"{MAX_COMMAND if data_supplied else 3}. Exit")
option = 0
try:
option = int(input("Enter an option: "))
except ValueError:
print("Option must be an integer")
else:
if option < 1 or option > (MAX_COMMAND if data_supplied else 3):
print("Option must be within range")
option = 0
return option if option != (MAX_COMMAND if data_supplied else 3) else -1
def input_data():
table = {}
for week in range(4):
table[week] = {}
for day in range(len(day_names)):
table[week][day_names[day]] = [None] * len(route_names)
for route in range(len(route_names)):
table[week][day_names[day]][route] = None
while table[week][day_names[day]][route] is None:
try:
table[week][day_names[day]][route] = \
int(input(f"{day_names[day]}{week + 1}/{route_names[route]}: "))
except ValueError:
print("Data must be an integer")
table[week][day_names[day]][route] = None
return table
def output_overall_stats(data):
late_arrivals_per = [0] * len(route_names)
avg_punc_per = [0] * len(route_names)
avg_late_per = [0] * len(route_names)
for week in range(4):
for day in range(len(day_names)):
for route in range(len(route_names)):
rt_data = data[week][day_names[day]][route]
late_arrivals_per[route] += (1 if rt_data < 0 else 0)
avg_punc_per[route] += rt_data
avg_late_per[route] += (abs(rt_data) if rt_data < 0 else 0)
print("Late Arrivals Per Route: ")
for route in range(len(route_names)):
print(f"{route_names[route]}: {late_arrivals_per[route]}")
print("Average Punctuality Per Route: ")
for route in range(len(route_names)):
avg_late = avg_punc_per[route]
if avg_late == 0:
print(f"{route_names[route]}: On time")
elif avg_late < 0:
print(f"{route_names[route]}: {abs(avg_late)} minute(s) late")
else:
print(f"{route_names[route]}: {abs(avg_late)} minute(s) ahead of time")
print("Average Minutes Late Per Route: ")
for route in range(len(route_names)):
avg_late = avg_late_per[route]
print(f"{route_names[route]}: {abs(avg_late)}")
##
print("Bus Route(s) With Most Late Days: ")
max_late = max(late_arrivals_per)
idxs_latest_routes = [i for i, j in enumerate(late_arrivals_per) if j == max_late]
latest_routes = ', '.join([route_names[r] for r in idxs_latest_routes])
print(f"{latest_routes} with {max_late} late arrivals{' each' if len(idxs_latest_routes) > 1 else ''}")
def output_specific_day(data):
day = None
week = None
while day is None or week is None:
userinput = input("Enter a day from Mon-Fri and a week from 1-4, e.g. Mon2, Fri3: ")
day = userinput[:3]
try:
week = int(userinput[3:4])
if week < 1 or week > 4:
raise ValueError()
week -= 1
except ValueError:
print("4th character is not a valid integer between 1 and 4.")
week = None
if day not in short_day_names:
print(f"Not a valid day. Valid days are: {', '.join(short_day_names)}")
day = None
day_name = day_names[short_day_names.index(day)]
day_data = data[week][day_name]
idx_late_routes = [i for i, j in enumerate(day_data) if j < 0]
print(f"Late route{'s are' if len(idx_late_routes) > 1 else ' is'} {', '.join([route_names[r] for r in idx_late_routes])}")
for idx in idx_late_routes:
print(f"{route_names[idx]} is {abs(day_data[idx])} minutes late")
data = {}
while current_command != -1:
current_command = 0
while current_command == 0:
current_command = menu(data != {})
if current_command == 1:
data = supplied_data
elif current_command == 2:
data = input_data()
elif current_command == 3:
output_overall_stats(data)
elif current_command == 4:
output_specific_day(data)
print('Goodbye!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment