Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created March 31, 2019 08:52
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 gsinclair/ab1f3463d6cbed9c6c4db550f393afd3 to your computer and use it in GitHub Desktop.
Save gsinclair/ab1f3463d6cbed9c6c4db550f393afd3 to your computer and use it in GitHub Desktop.
Pre-release-practice 2 (reaction times for students from Saturn and Mars)
# Pre-release practice 2
#
# Students are aged 12-16.
# Students belong to house Saturn or Mars.
# We will represent that with "S" or "M".
ALLOWABLE_AGES = range(12,17)
ALLOWABLE_HOUSES = ["S", "M"]
# The program gets information from the user to store
# - age (integer)
# - house (string)
# - reaction time (float)
#
# There are 650 students in the school, so we will set up arrays of
# that length, plus 1 so we can index 1..650 instead of 0..649.
# Of course, we won't actually enter that much data.
SCHOOL_POPULATION = 650
AGE = [None] * (SCHOOL_POPULATION + 1)
HOUSE = [None] * (SCHOOL_POPULATION + 1)
RTIME = [None] * (SCHOOL_POPULATION + 1)
# Returns four values:
# * valid (True/False) * age * house * reaction time
def input_single_student_data():
age = int(input("Enter age (12-16): "))
house = input("Enter house (S/M): ")
rtime = float(input("Enter reaction time (sec): "))
# Validate
if age in ALLOWABLE_AGES and house in ALLOWABLE_HOUSES and rtime > 0:
return (True, age, house, rtime)
else:
return (False, age, house, rtime)
# TASK 1: input and store records for a sample of students.
# Return the number of records stored.
def collect_input_data():
# Find out how many records we are collecting.
n = int(input("How many students? "))
# Set an index to work towards that number.
i = 1
while i <= n:
print("")
print("Data for student", i)
valid, age, house, rtime = input_single_student_data()
if valid:
# Store the data.
AGE[i] = age
HOUSE[i] = house
RTIME[i] = rtime
# Increment the index.
i = i + 1
else:
# Print error message.
print("Invalid data; not stored.")
# The index stays where it is.
return n
# TASK 2: output house based statistics
# We run through the data, calculating the _sum_ and _count_ for
# both Saturn and Mars. Then display both averages.
# Parameter _n_ is the number of records stored.
def output_house_based_statistics(n):
sum_saturn = 0.0
sum_mars = 0.0
count_saturn = 0
count_mars = 0
# We look at every record from 0 to (n-1).
for i in range(1, n+1):
# Is the house Saturn or Mars?
if HOUSE[i] == "S":
count_saturn = count_saturn + 1
sum_saturn = sum_saturn + RTIME[i]
else:
count_mars = count_mars + 1
sum_mars = sum_mars + RTIME[i]
# Now we can display the averages. But check first that there are records.
if count_saturn > 0:
average = sum_saturn / count_saturn
print("Average reaction time for Saturn:", average)
else:
print("No records for Saturn, so no average")
if count_mars > 0:
average = sum_mars / count_mars
print("Average reaction time for Mars:", average)
else:
print("No records for Mars, so no average")
# TASK 3: output statistics based on user input
# Parameter _n_ is the number of records stored.
def output_selected_statistics(n):
# Collect and validate the age and house.
target_age = int(input("Age: "))
target_house = input("House: ")
if target_age in ALLOWABLE_AGES and target_house in ALLOWABLE_HOUSES:
# We are calculating average and slowest reaction time.
total = 0.0
count = 0
lowest = 1000000000000 # An impossibly high value, sure to be updated.
# We look at every record from 0 to (n-1).
for i in range(1, n+1):
if AGE[i] == target_age and HOUSE[i] == target_house:
count = count + 1
total = total + RTIME[i]
if RTIME[i] < lowest:
# We have a new value for 'lowest'.
lowest = RTIME[i]
# We are now in a position to give the outputs desired.
if count > 0:
average = total / count
print("Average reaction time (sec):", average)
print("Lowest reaction time (sec): ", lowest)
else:
print("No records match your input")
else:
print("Invalid input(s).")
def program():
# Task 1. The return value 'n' is the number of records stored.
print("Task 1:Collecting input data")
n = collect_input_data()
# Task 2.
print("")
print("Task 2: Printing averges for each house")
output_house_based_statistics(n)
# Task 3.
print("")
print("Task 3: Printing selected statistics")
output_selected_statistics(n)
# Run the program.
program()
@Mudit-999
Copy link

Can I have a version of just the code to avoid confusion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment