13.1 - Shortest Athlete
Problem
A program is required to identify the shortest member of an athletics club.
Write a program that will read the data of 20 athletes from athletes.txt (name, height) into an array of records. The program should then find and display the name and height of the shortest athlete.
Example Run
Amelia is the smallest athlete with a height of 164cm.
Tom,180 Alex,175 Sam,190 Rachel,168 Liam,182 Mia,165 Noah,178 Olivia,170 Lucas,185 Emma,167 Ethan,181 Ava,169 Mason,183 Isla,166 Logan,177 Sophia,172 James,179 Amelia,164 Daniel,186 Grace,171
| 1 | Read data into array of records | IN | |
| OUT | athletes(name, height) | ||
| 2 | Find and display shortest athlete | IN | athletes(name, height) |
| OUT |
2.1 set shortest to first athlete height 2.2 set shortest_pos to 0 2.3 for i from 1 to length of athletes - 1 2.4 if current athletes height < shortest then 2.5 set shortest to current athletes height 2.6 set shortest_pos to i 2.7 end if 2.8 end for 2.9 display athletes[shortest_pos].name and shortest
# Main athletes = read_data() find_shortest(athletes)
from dataclasses import dataclass
@dataclass
class Athlete:
name: str = ""
height: int = 0
def read_data():
athletes = [Athlete() for i in range(20)]
file = open("athletes.txt", "r")
for i in range(20):
athletes[i].name, athletes[i].height = file.readline().strip().split(",")
athletes[i].height = int(athletes[i].height)
file.close()
return athletes
def find_shortest(athletes):
shortest = athletes[0].height
shortest_pos = 0
for i in range(1, len(athletes)):
if athletes[i].height < shortest:
shortest = athletes[i].height
shortest_pos = i
print("The shortest athelete is", athletes[shortest_pos].name)
print("They are", shortest ,"cm tall")
# Main
athletes = read_data()
find_shortest(athletes)
from dataclasses import dataclass
# Define a record called Athlete
# Each athlete has a name and a height
@dataclass
class Athlete:
name: str = ""
height: int = 0
# Function to read athlete data from a file
def read_data():
# Create an array of 20 Athlete records
athletes = [Athlete() for i in range(20)]
# Open the file for reading
file = open("athletes.txt", "r")
# Read 20 lines from the file
for i in range(20):
# Read a line, remove newline, and split by comma
# Store the values in the athlete record
athletes[i].name, athletes[i].height = file.readline().strip().split(",")
# Convert height from string to integer
athletes[i].height = int(athletes[i].height)
# Close the file
file.close()
# Return the array of athlete records
return athletes
# Function to find the shortest athlete
def find_shortest(athletes):
# Assume the first athlete is the shortest
shortest = athletes[0].height
shortest_pos = 0
# Loop through the rest of the athletes
for i in range(1, len(athletes)):
# If a shorter athlete is found
if athletes[i].height < shortest:
# Update the shortest height
shortest = athletes[i].height
# Store the position of the shortest athlete
shortest_pos = i
# Display the shortest athlete
print("The shortest athlete is", athletes[shortest_pos].name)
print("They are", shortest, "cm tall")
# Main program
# Read athlete data from the file
athletes = read_data()
# Find and display the shortest athlete
find_shortest(athletes)
Extension 1
Add an additional procedure to find the tallest athlete.
Extension 2
Add an additional procedure that allows the user to search for an athlete by name and display their height.
If no athlete is found, an error message should be displayed.