13.1 - Challenges
1 - Shortest athlete
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 smallest athlete.
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].age = 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)
2 - Username generator
Write a program that will read the data of 20 customers from customers.txt (forename, surname, dob) into an array of records. The program should then generate and display a username for each customer.
username = forename initial + surname initial + dob year
Example - John,Smith,12/03/2008
username = JS2008
JS2008 EJ2007 MB2008 SD2007 DW2008 OT2007 JA2008 ST2007 WJ2008 AW2007 EH2008 IM2007 LT2008 MG2007 AM2008 AR2007 BC2008 HR2007 HL2008 EL2007
John,Smith,12/03/2008 Emily,Johnson,25/07/2007 Michael,Brown,18/11/2008 Sarah,Davies,02/05/2007 Daniel,Wilson,30/09/2008 Olivia,Taylor,14/01/2007 James,Anderson,22/06/2008 Sophia,Thomas,08/12/2007 William,Jackson,17/04/2008 Ava,White,03/10/2007 Ethan,Harris,29/02/2008 Isla,Martin,11/08/2007 Lucas,Thompson,06/03/2008 Mia,Garcia,21/07/2007 Alexander,Martinez,15/11/2008 Amelia,Robinson,09/01/2007 Benjamin,Clark,27/05/2008 Harper,Rodriguez,13/09/2007 Henry,Lewis,04/02/2008 Ella,Lee,19/06/2007
| 1 | Read data into array of records | IN | |
| OUT | customers(forename, surname ,dob) | ||
| 2 | Generate username for each customer | IN | customers(forename, surname ,dob) |
| OUT |
2.1 for i from 0 to length of customers - 1 2.2 set forename_initial to first letter of customers[i].forename 2.3 set surname_initial to first letter of customers[i].surname 2.4 set year to characters 6 to 9 of customers[i].dob 2.5 set username to forename_initial + surname_initial + year 2.6 display username 2.7 end for
# Main customers = read_data() generate_usernames(customers)
from dataclasses import dataclass
@dataclass
class customer:
forename: str = ""
surname: str = ""
dob: str = ""
def read_data():
customers= [customer() for i in range(20)]
file = open("customers.txt", "r")
for i in range(20):
customers[i].forename, customers[i].surname, customers[i].dob = file.readline().strip().split(",")
file.close()
return customers
def generate_usernames(customers):
for i in range(len(customers)):
forename_initial = customers[i].forename[0]
surname_initial = customers[i].surname[0]
year = customers[i].dob[6:10]
username = forename_initial + surname_initial + year
print(username)
# Main
customers = read_data()
generate_usernames(customers)
from dataclasses import dataclass
# Define a record structure to store customer details
@dataclass
class customer:
forename: str = ""
surname: str = ""
dob: str = "" # Format: DD/MM/YYYY
# Function to read data from file into an array of records
def read_data():
# Create an array of records with 3 empty entries
customers = [customer() for i in range(20)]
# Open file for reading
file = open("customers.txt", "r")
# Read each line and store values in the array of records
for i in range(20):
customers[i].forename, customers[i].surname, customers[i].dob = file.readline().strip().split(",")
# Close file
file.close()
# Return the array of records
return customers
# Function to generate usernames from the array of records
def generate_usernames(customers):
# Loop through each record in the array
for i in range(len(customers)):
# Get first letter of forename
forename_initial = customers[i].forename[0]
# Get first letter of surname
surname_initial = customers[i].surname[0]
# Extract year from date of birth
year = customers[i].dob[6:10]
# Combine to form username
username = forename_initial + surname_initial + year
# Output username
print(username)
# Main program
# Read data into array of records
customers = read_data()
# Generate and display usernames
generate_usernames(customers)
3 - Even years
Write a program that will read the data of 10 users from data.txt (name, dob) into an array of records. The program should then display the names of all users born in an even year.
John Smith Michael Brown Daniel Wilson James Anderson William Jackson
John Smith,12/03/2008 Emily Jones,25/07/2007 Michael Brown,18/11/2008 Sarah White,02/05/2007 Daniel Wilson,30/09/2008 Olivia Taylor,14/01/2007 James Anderson,22/06/2008 Sophia Thomas,08/12/2007 William Jackson,17/04/2008 Ava Harris,03/10/2007
| 1 | Read data into array of records | IN | |
| OUT | users(name, dob) | ||
| 2 | Find and display all users born in an even year | IN | users(name, dob) |
| OUT |
2.1 for i from 0 to length of users - 1 2.2 set year to characters 6 to 9 of users[i].dob 2.3 convert year to integer 2.4 if year MOD 2 = 0 then 2.5 display users[i].name 2.6 end if 2.7 end for
# Main users = read_data() display_even_year(users)
from dataclasses import dataclass
@dataclass
class User:
name: str = ""
dob: str = "" # Format: DD/MM/YYYY
def read_data():
users = [User() for i in range(10)]
file = open("data.txt", "r")
for i in range(10):
users[i].name, users[i].dob = file.readline().strip().split(",")
file.close()
return users
def display_even_year(users):
for i in range(len(users)):
year = int(users[i].dob[6:10])
if year % 2 == 0:
print(users[i].name)
# Main
users = read_data()
display_even_year(users)
from dataclasses import dataclass
# Define a record structure to store user details
@dataclass
class User:
name: str = ""
dob: str = "" # Format: DD/MM/YYYY
# Function to read data from file into an array of records
def read_data():
# Create an array of records with 10 empty entries
users = [User() for i in range(10)]
# Open the file for reading
file = open("data.txt", "r")
# Read each line and store values in the array of records
for i in range(10):
# Read line, remove whitespace, split into name and dob
users[i].name, users[i].dob = file.readline().strip().split(",")
# Close the file
file.close()
# Return the array of records
return users
# Function to display names of users born in even years
def display_even_year(users):
# Loop through each record in the array
for i in range(len(users)):
# Extract year from date of birth
year = int(users[i].dob[6:10])
# Check if year is even
if year % 2 == 0:
# Display the user's name
print(users[i].name)
# Main program
# Read data into array of records
users = read_data()
# Display users born in even years
display_even_year(users)