Skip to content

Instantly share code, notes, and snippets.

@beltiras
Forked from gunnarig/clock.py
Last active October 23, 2018 18:45
Show Gist options
  • Save beltiras/f0cef90080a3e3b239acf0ad88fcbccf to your computer and use it in GitHub Desktop.
Save beltiras/f0cef90080a3e3b239acf0ad88fcbccf to your computer and use it in GitHub Desktop.
class Clock:
def __init__(self,hours = 0,minutes = 0,seconds = 0):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
def add_clocks(self,var1,var2,var3):
self.seconds = self.seconds + var3
if self.seconds > 59:
self.seconds -= 60
self.minutes += 1
self.minutes += var2
if self.minutes > 59:
self.minutes -=60
self.hours += 1
print(integer_hour,integer_minute,seconds_corr)
return integer_hour,calculated_minute,seconds_corr
def str_update(self,a_string):
split_list = a_string.split(":")
split_list = [int(x) for x in split_list]
def __str__(self):
return ("{} hours, {} minutes and {} seconds".format(self.hours,self.minutes,self.seconds))
# 1440m m í degi
#3600s í klst
clock1 = Clock()
clock2 = Clock()
print(clock1)
print(clock2)
assert str(clock1) == "0 hours, 0 minutes and 0 seconds"
clock1.str_update("03:21:34")
clock2.str_update("05:45:52")
print(clock1)
print(clock2)
assert str(clock2) == "5 hours, 45 minutes and 52 seconds"
clock3 = clock1.add_clocks(clock2)
print(clock3)
assert str(clock3) == "9 hours, 7 minutes and 26 seconds"
# Constructor with three parameters:
# hours, minutes, seconds with default values 0.
# Three instance variables: hours, minutes, seconds.
# A method called str_update().
# It takes as an argument a string of the form hh:mm:ss and updates
# the three instances variables.
# A __str__() method for responding to the print() method.
# It should write out: "{} hours, {} minutes and {} seconds"
# A method called add_clocks().
# It takes another clock object as a parameter,
# adds the two clocks and returns a new clock instance.
# In this method, you need to add the respective values of the two clocks together
# and remember the resulting hours, minutes and seconds.
# Remember that the sum of seconds cannot exceed 60,
# in which case there is a carry over to the minutes values.
# Same for minutes, it cannot exceed 60 and carries over to hours.
# For hours, the summed values cannot exceed
# 23. If hours is exceeded, we ignore it.
# Use the divmod() built-in Python function in your implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment