Write a Boolean function between that takes two
MyTime
objects,t1
andt2
, as arguments, and returns True if the invoking object falls between the two times. Assumet1 <= t2
, and make the test closed at the lower bound and open at the upper bound, i.e. return True ift1 <= obj < t2
.
Now from the wording of this question, it seems like there should only be two arguments in the function, but I cannot see a way to make such a function by only using two arguments. I mean I guess you could make another function that creates a variable that is a MyTime
object, but I was only going to keep it to one function and not make two. The wording of the question makes it seem like you should have Object(Function(t1,t2))
but I dont think that is possible. Is it possible to make the 'between' function with only two arguments? Here is my code
class MyTime:
""" Create some time """
def __init__(self,hrs = 0,mins = 0,sec = 0):
"""Splits up whole time into only seconds"""
totalsecs = hrs*3600 + mins*60 + sec
self.hours = totalsecs // 3600
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def __str__(self):
return '{0}:{1}:
{2}'.format(self.hours,self.minutes,self.seconds)
def to_seconds(self):
# converts to only seconds
return (self.hours * 3600) + (self.minutes * 60) + self.seconds
def between(t1,t2,x):
t1seconds = t1.to_seconds()
t2seconds = t2.to_seconds()
xseconds = x.to_seconds()
if t1seconds <= xseconds < t2seconds:
return True
return False
currentTime = MyTime(0,0,0)
doneTime = MyTime(10,3,4)
x = MyTime(2,0,0)
print(between(currentTime,doneTime,x))