Skip to content

Instantly share code, notes, and snippets.

@gunnarig
Created October 23, 2018 18:32
Show Gist options
  • Save gunnarig/96384b2c5f8daed6ada1181f0fdd4244 to your computer and use it in GitHub Desktop.
Save gunnarig/96384b2c5f8daed6ada1181f0fdd4244 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.hours = var1
self.minutes = var2
self.seconds = var3
#hours = hours
#minutes = minutes
#seconds = seconds
correct_Hours = var1*(60*60)
correct_minutes = var2*(60)
correct_seconds = var3
integer_hour = int((correct_seconds + correct_minutes + correct_Hours)/60/60)
correct_hour = float((correct_seconds + correct_minutes + correct_Hours)/60/60)
correct_minute = float(correct_hour - integer_hour)*60
integer_minute = int(correct_minute)
calculated_minute = float(correct_minute)
seconds_left = float(correct_minute - integer_minute)
seconds_corr = int(((seconds_left+(0.00001))*60))
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