Time Class

Your task is to create a Time class with three attributes - hours, minutes, and seconds.
You are required to implement two magic methods, __add__ and __sub__, that will allow for the addition and subtraction of two instances of the Time class respectively. The output of these operations should be a new Time instance.
You should also implement the __str__ magic method that would return a string of hours, minutes, and seconds separated by a space.
You have to handle cases where minutes or seconds go above 59 or below 0, as well as cases where hours go above 24 or below 0.
In the __add__ method, if seconds, minutes, or hours exceed the threshold, they should roll over to 0 and increment the next higher unit (minutes or hours) by 1. Hours should always stay in the range of 0 to 23.
Similarly, in the __sub__ method, if seconds or minutes fall below 0, they should roll over to 59 and decrement the next higher unit (minutes or hours) by 1. If hours fall below 0, they should roll over to 23.
t1 = Time(23, 15, 45); t2 = Time(1, 48, 30); print(t1 + t2); print(t1 - t2);
1 4 15 21 27 15
Note: The hours are represented in a 24-hour format.


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue