Question: 1 . Create a file called thermostat.py , and in the file create a class called Your next task is to implement a function for

1.Create a file called thermostat.py, and in the file create a class called Your next task is to implement a function for this class called add_schedule. Other than the self parameter, this function should take a time (string) and a temperature (float) as parameters. It should store the time-temperature pair (where time is the key and temperature is the value) in the schedules dictionary. If the time already exists, its value will be overwritten by the new temperature; otherwise it will create a new entry. You can assume the time string always represents a valid time. After this function is implemented, you can add schedules to your Thermostat object by for instance:
dev = Thermostat(75)
dev.add_schedule('08:00',60.4)
dev.add_schedule('19:35',75.5)
dev.add_schedule('09:30',58.2)
dev.add_schedule('22:39',68.2)
Note that the user doesnt need to add schedules in increasing/ascending order of time -- the schedules can be added in any arbitrary ordering of times.
Hint: Do NOT ask the user for any input in the function.. Your first task is to implement its constructor : other than the self parameter, it should take a number value as the default temperature parameter -- this parameter should be given a default value of 68, so in the case the user doesnt provide this parameter, the default temperature will be 68.
In the constructor you should create two instance variables / attributes: one to store the default temperature, and another to store the schedules like described above. The most straightforward data type for schedules is a dictionary, as you can use it to store time-temperature pairs. So in the constructor, you should assign it to an empty dictionary indicating there is no schedule yet. Once you have the constructor implemented, you can create objects of the Thermostat class by using, for instance: Thermostat() or dev = Thermostat(75)
Hint: Do NOT ask the user for any input in the function.
2.11,86.29,91.63,92.42,94.5
Ryanne Rusty,A,96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32
res': [96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32], 'average': 80.29}]
Tip: this function can be easily implemented using list comprehension.
2. Your next task is to implement a function for this class called add_schedule. Other than the self parameter, this function should take a time (string) and a temperature (float) as parameters. It should store the time-temperature pair (where time is the key and temperature is the value) in the schedules dictionary. If the time already exists, its value will be overwritten by the new temperature; otherwise it will create a new entry. You can assume the time string always represents a valid time. After this function is implemented, you can add schedules to your Thermostat object by for instance:
dev = Thermostat(75)
dev.add_schedule('08:00',60.4)
dev.add_schedule('19:35',75.5)
dev.add_schedule('09:30',58.2)
dev.add_schedule('22:39',68.2)
Note that the user doesnt need to add schedules in increasing/ascending order of time -- the schedules can be added in any arbitrary ordering of times.
Hint: Do NOT ask the user for any input in the function.
3. Now you will implement the special function __str__ which casts this object to a string. Specifically, this function should return a string (note: return, NOT print) formatted as the following:
Default temperature: {default_temp} degrees
{TIME1}{VALUE1} degrees
{TIME2}{VALUE2} degrees
......
where {default_temp} is the actual value of the default temperature, and following from that are the time-temperature pairs sorted in increasing/ascending order of time, one on each line. Because the string should contain multiple lines, at the end of each line there must be a newline character '
', except the last line, which should NOT have a trailing newline character.
You dont need to implement the sorting algorithm yourself: Python provides a sorted() function, which can take a dictionary and returns a list that contains the keys sorted in ascending order. For example, in our case, sorted(self.schedules) returns a list of times sorted in ascending order.
When you are done, test this function by calling print(Thermostat()), it should print out just one line:
Default temperature: 68
which is what it should look like when no schedule is in the thermostat yet. Then, call print(dev) where dev has 4 schedules as seen in Section 2 above. This should print out 5 lines:
Default temperature: 75 degrees
08:0060.4 degrees
09:3058.2 degrees
19:3575.5 degrees
22:3968.2 degrees
Hint: Do NOT ask the user for any input in the function.
4. Your last task is to implement a function called get_target_temperature. It should take an arbitrary query time (string) as the parameter, and it should return the target temperature at that query time. Imagine we sort the schedules in ascending order of time: TIME1, TIME2, TIME3, and so on. This divides the entire timeline of a day 00:00 to 23:59 into intervals. If a query time is earlier than TIME1(I.E the earliest time in schedles) this function should return the default temperature value . if the quert time is after the last time it should returnthe temperature set at that last time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!