6.4 - Sports day
Problem
A school sports day records the names of athletes and their race times.
The program should:
- Store the athlete names and their times in two arrays.
- Have a procedure to find and display the fastest athlete.
- Have a procedure to count and display how many athletes ran under 12 seconds.
- Have a procedure to search for a given athlete by name and display their time if found.
The main program should only contain calls to these three procedures.
Example run
Fastest athlete: David with a time of 10.9 seconds Number of athletes under 12 seconds: 2 Enter the name of the athlete to search for: Carla Carla finished in 13.2 seconds
Arrays
names = ["Alice", "Ben", "Carla", "David", "Ella"] times = [12.4, 11.8, 13.2, 10.9, 12.0]
| 1 | Display the name of the fastest athlete | IN | names, times |
| 2 | Display how many athletes ran under 12 seconds | IN | times |
| 3 | Search for a name and display their time | IN | names, target_name |
Use the standard algorithms to solve this problem.
# Main
names = ["Alice", "Ben", "Carla", "David", "Ella"]
times = [12.4, 11.8, 13.2, 10.9, 12.0]
show_fastest(names, times)
count_under_12(times)
target = input("Enter the name of the athlete to search for: ")
find_runner(names, target)
def show_fastest(names, times):
fastest_time = times[0]
fastest_name = names[0]
for i in range(1, len(times)):
if times[i] < fastest_time:
fastest_time = times[i]
fastest_name = names[i]
print("Fastest athlete:", fastest_name, "with a time of", fastest_time, "seconds")
def count_under_12(times):
count = 0
for i in range(len(times)):
if times[i] < 12:
count = count + 1
print("Number of athletes under 12 seconds:", count)
def find_runner(names, target_name):
found = False
for i in range(len(names)):
if names[i] == target_name:
print(target_name, "finished in", times[i], "seconds")
found = True
if found == False:
print(target_name, "was not found in the competition.")
# Main
names = ["Alice", "Ben", "Carla", "David", "Ella"]
times = [12.4, 11.8, 13.2, 10.9, 12.0]
show_fastest(names, times)
count_under_12(times)
target = input("Enter the name of the athlete to search for: ")
find_runner(names, target)
def show_fastest(names, times): # procedure definition – 'names' and 'times' are parameters (formal)
fastest_time = times[0]
fastest_name = names[0]
for i in range(1, len(times)):
if times[i] < fastest_time:
fastest_time = times[i]
fastest_name = names[i]
print("Fastest athlete:", fastest_name, "with a time of", fastest_time, "seconds")
def count_under_12(times): # procedure definition – 'times' is the parameter (formal)
count = 0
for i in range(len(times)): if times[i] < 12:
count = count + 1
print("Number of athletes under 12 seconds:", count)
def find_runner(names, target_name): # procedure definition – 'names' and 'target_name' are parameters (formal)
found = False
for i in range(len(names)):
if names[i] == target_name:
print(target_name, "finished in", times[i], "seconds")
found = True
if found == False:
print(target_name, "was not found in the competition.")
# Main
names = ["Alice", "Ben", "Carla", "David", "Ella"]
times = [12.4, 11.8, 13.2, 10.9, 12.0]
show_fastest(names, times) # procedure call – actual parameters 'names' and 'times' are passed in
count_under_12(times) # procedure call – actual parameter 'times' is passed in
target = input("Enter the name of the athlete to search for: ")
find_runner(names, target) # procedure call – actual parameters 'names' and 'target' are passed in