
Week 10: In-Class#
Demo#
Demo 10.1: My Time#
When representing time in hours and minutes, both values should update correctly as time passes. For example, one minute after 14 hours and 59 minutes, the time should be 15 hours and 0 minutes.
Problem Analysis
How should the time change when one minute passes? Consider the following cases:
The time is 7 hours and 15 minutes.
The time is 23 hours and 59 minutes.
Demo
Write a class with a constructor that takes two integer parameters, hours and minutes, and initializes the corresponding attributes. The class should be able to return the time in a tuple with hours and minutes as integers. It should also include methods to increment the time by a specified number of hours and a specified number of minutes.
The expected behavior is shown below:
>>> my_time = MyTime(10, 23)
>>> my_time.increment_minutes(36)
>>> my_time.get_time()
(10, 59)
>>> my_time.increment_hours(13)
>>> my_time.get_time()
(23, 59)
>>> my_time.increment_minutes(1)
>>> my_time.get_time()
(0, 0)
>>> my_time.increment_hours(30)
>>> my_time.get_time()
(6, 0)
>>> my_time.increment_minutes(123)
>>> my_time.get_time()
(8, 3)
The class requirements are:
my_time.py
MyTime
A class to represent 24-hour time.
__init__(hours, minutes)
Initializes the time to the given hours and minutes.
Parameters:
|
|
The hour value (0 to 23). |
|
|
The minute value (0 to 59). |
get_time()
Returns the time currently represented in the class.
Returns:
|
A tuple (hours, minutes) representing the current time. |
increment_hours(hours)
Increments the hours.
Parameters:
|
|
The number of hours to increment. |
increment_minutes(minutes)
Increments the minutes.
Parameters:
|
|
The number of minutes to increment. |
Use the following script to check your class test_my_time.py. If your class fails the test in this script, it would also fail if you could hand it in.
Coding Practice#
Code 10.2: Weather Station Revisited#
In Code 8.3: Weather Station, you created a dictionary that represented a weather station, and you defined a function which took that dictionary as input and returned some processed information. You will now do something similar using classes. The advantage of using classes is that instead of having a dictionary that stores data and a function that processes that data, we can have a class that does both.
Create a class WeatherStation. The constructor method should take as input a string with the station name and a boolean indicating whether the station is operational. These inputs should be assigned to the attributes station_name and is_operational. The constructor should create two additional attributes wind_readings and temperature_readings and assign an empty list to each of them. Later you will change these attributes.
Before you continue, try to create an object cph_station of the class WeatherStation with the name "CPH Airport Weather Station" and operational status True.
Check
You should be able to achieve the behavior below.
>>> cph_station = WeatherStation("CPH Airport Weather Station", True)
>>> cph_station.station_name
'CPH Airport Weather Station'
>>> cph_station.is_operational
True
>>> cph_station.wind_readings
[]
>>> cph_station.temperature_readings
[]
Add two methods to the class WeatherStation. The first method should be named take_daily_wind_readings and take a list of floating-point numbers as input. The method should assign the input list to the attribute wind_readings, but only if the station is operational. If the station is not operational, the method should print a message saying that the station is not operational. The second method should be named take_daily_temperature_readings and take a list of floating-point numbers as input. The method should assign the input list to the attribute temperature_readings, also only if the station is operational. If the station is not operational, the method should print a message saying that the station is not operational.
Test your code.
Check
You should be able to achieve the behavior below.
>>> cph_station.take_daily_wind_readings([3.5, 6.2, 7.1, 12.1])
>>> cph_station.take_daily_temperature_readings([18.1, 18.3, 19.0, 18.5])
>>> cph_station.wind_readings
[3.5, 6.2, 7.1, 12.1]
>>> cph_station.temperature_readings
[18.1, 18.3, 19.0, 18.5]
Add a method get_highest_readings to the class WeatherStation. The method should output a tuple containing the maximum wind speed reading and the maximum temperature reading. If one or both of the readings are empty, the corresponding value in the tuple should be None.
Test your code.
Check
You should be able to achieve the behavior below.
>>> max_wind, max_temp = cph_station.get_highest_readings()
>>> max_wind
12.1
>>> max_temp
19.0
Code 10.3: Health Analysis Revisited#
In this exercise, we look back at Problem 2.14: Health Analysis. Recall how body mass index (BMI) and body fat percentage (BFP) are calculated from weight \(w\) in kilograms, height \(h\) in meters, and age \(y\) in years.
where \(s\) is equal to \(0\) for women and \(1\) for men.
Create a class PersonHealth. The constructor of the class should take as input the age, height, weight, and sex of the person. The age, height, and weight should be numerical values, as well as the corresponding attributes. The sex should be given to the constructor as a string, either 'm' or 'f'. The attribute sex should be assigned the value 1 if the input is 'm' and 0 if the input is 'f'.
Write a class method body_mass_index that calculates and returns the BMI of the person.
Write a class method body_fat_percentage that calculates and returns the body fat percentage.
Finally, write a class method get_health_description that returns a string with an informative description of the person’s health. The string should contain the age, BMI, and BFP of the person.
Check
You should be able to achieve the behavior below.
>>> person1 = PersonHealth(20, 1.70, 69, 'f')
>>> person1.get_health_description()
'Age: 20, BMI: 23.88, BFP: 27.85'
>>> person2 = PersonHealth(61, 1.92, 91, 'm')
>>> person2.get_health_description()
'Age: 61, BMI: 24.69, BFP: 27.45'
Problem Solving#
Problem 10.4: Simple Statistics#
We want to create a class to store a sequence of numbers, and to compute some basic statistics.
Write a class definition for the class SimpleStatistics. The constructor method should take a list of numbers as argument, and assign it to an attribute called samples. The class should have a get_mean method that returns the mean of the samples, and a get_standard_deviation method that returns the standard deviation of the samples. The mean \(\mu\) and the standard deviation \(\sigma\) should be calculated as
where \(N\) is the number of samples and \(x_i\) is the value of the \(i^\text{th}\) sample.
Finally, the class should have a method one_sample_ttest that, given an input value \(\mu_0\), computes the t-statistic
and returns True if \(|t| \geq 1.96\) and False otherwise.
Here is an example of using the class.
>>> samples = [0.1763, -1.2523, 0.9099, -0.0679, 0.1999, 0.1175, 0.143, 0.1118,
... 0.9854, -1.1836, -0.5868, 0.4839, 1.2069, 0.142, 0.2523]
>>> stat = SimpleStatistics(samples)
>>> stat.get_mean()
0.10922000000000003
>>> stat.get_standard_deviation()
0.6984926241557602
>>> stat.one_sample_ttest(0.0)
False
>>> stat.one_sample_ttest(0.1)
False
>>> stat.one_sample_ttest(-0.1)
False
>>> stat.one_sample_ttest(-0.25)
True
The filename and requirements are in the box below:
simple_statistics.py
SimpleStatistics
Class to get summary statistics and compute one-sample t-test.
__init__(samples)
Initialize the statistics class with the samples.
Parameters:
|
|
List of samples. |
get_mean()
Get the mean of the samples.
Returns:
|
The mean of the samples. |
get_standard_deviation()
Get the standard deviation of samples.
Returns:
|
The standard deviation of the samples. |
one_sample_ttest(mu_0)
Compute one sample t-value and determine whether it is outside the expected range.
Parameters:
|
|
Proposed mean. |
Returns:
|
True if the proposed mean is significantly different from the mean of the samples, else False. |
You should also test your class with test_simple_statistics.py.
Problem 10.5: Study Planner#
In this exercise, you will create a simple study planner to manage the courses a student has registered for in each course block (e.g., foundations, directions, projects, and electives). The planner will also track the remaining ECTS available in each course block.
Define a class named StudyPlanner. The constructor of this class should take two parameters: blocks, which is a list of strings with the names of the course blocks, and points, which is a list of integers indicating the maximum number of points allowed in the corresponding blocks. The constructor should create two dictionaries as attributes. The dictionary registered_courses has the names of the course blocks as keys and, initially, empty lists as values. The dictionary points_left has the names of the course blocks as keys and the corresponding maximum number of points as values.
In the StudyPlanner, define a method add_course that takes three values as input: a string with the course name, a string with the block the course should be added to, and an integer with ECTS points. The method should verify that there are enough points left in the course block to add the course. If there are, it should add the course name to the list in registered_courses under the specified course block and decrease the corresponding value in points_left by the number of ECTS points for the course. The method should return 1 if the course was added successfully, and -1 if the course could not be added.
Define a method print_courses that displays a message summarizing the registered courses and the remaining points for each course block. You may decide on the format of this message.
study_planner.py
StudyPlanner
Study planner class to manage courses in different course groups.
__init__(blocks, points)
Initialize the study planner.
Parameters:
|
|
The course block names. |
|
|
The maximum number of points allowed in course blocks. |
add_course(name, block, ects)
Add a course to the study planner.
Parameters:
|
|
The name of the course to add. |
|
|
The course block to add the course to. |
|
|
The number of ects points the course is worth. |
Returns:
|
-1 if unsuccessful, 1 if successful. |
print_courses()
Print an overview of the courses in the study planner.
Test your class using the following code.
Check
You should be able to use the class as shown below.
blocks = ['foundations', 'directions', 'projects', 'electives']
points = [55, 55, 25, 45]
planner = StudyPlanner(blocks, points)
planner.print_courses()
planner.add_course('Programming', 'foundations', 5)
planner.print_courses()
planner.add_course('Mathematics 1a', 'foundations', 10)
planner.add_course('Mathematics 1b', 'foundations', 10)
planner.add_course('Signals and systems', 'directions', 5)
planner.add_course('Optics and photonics', 'directions', 5)
planner.add_course('BSc project', 'projects', 15)
planner.add_course('Prototyping', 'projects', 10)
planner.add_course('Acoustics and noise', 'electives', 5)
planner.print_courses()
One possible output of the last call to print_courses is shown below, but the exact format is up to you.
Example
---------------
BLOCK FOUNDATIONS
Points left: 30
Courses:
Programming
Mathematics 1a
Mathematics 1b
BLOCK DIRECTIONS
Points left: 45
Courses:
Signals and systems
Optics and photonics
BLOCK PROJECTS
Points left: 0
Courses:
BSc project
Prototyping
BLOCK ELECTIVES
Points left: 40
Courses:
Acoustics and noise
---------------
Solutions to Demo Tasks
Solution to Demo 10.1: My Time
class MyTime:
def __init__(self, hours, minutes):
self.hours = hours
self.minutes = minutes
def get_time(self):
return self.hours, self.minutes
def increment_hours(self, hours):
self.hours += hours
self.hours = self.hours % 24
def increment_minutes(self, minutes):
self.minutes += minutes
self.increment_hours(self.minutes // 60)
self.minutes = self.minutes % 60