In: Computer Science
Python: I am not sure where to begin with this question, and I hope I can get an input on it. This is in regards to Downey's program, and we are asked to make two changes.
(I am asked to identify my changes with #)
A) Rewrite the dunder str method used to print the time. It currently prints Time(17, 30, 0) as
17:30:00
Modify it to return
5:30 PM
Hours are numbers between 1 and 12 inclusive, seconds are suppressed, and times end with AM or PM. For purposes of this problem, midnight is AM, while noon is PM.
B) Time2.py currently allows you to create times with hours greater than 23. Identify the routines that Downey provides that would have to change to keep hours less than 24.
C) Make the changes required to keep hours less than 24.
D) Include the tests you have used to verify your changes.
Run the unit tests: all times should be within 24 hours
Here's the code below:
""
Code example from Think Python, by Allen B. Downey.
Available from http://thinkpython.com
Copyright 2012 Allen B. Downey.
Distributed under the GNU General Public License at
gnu.org/licenses/gpl.html.
"""
class Time(object):
"""Represents the time of day.
attributes: hour, minute, second
"""
def __init__(self, hour=0, minute=0, second=0):
self.hour = hour
self.minute = minute
self.second = second
# Modify this routine
def __str__(self):
return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
def print_time(self):
print(str(self))
def time_to_int(self):
"""Computes the number of seconds since midnight."""
minutes = self.hour * 60 + self.minute
seconds = minutes * 60 + self.second
return seconds
def is_after(self, other):
"""Returns True if t1 is after t2; false otherwise."""
return self.time_to_int() > other.time_to_int()
def __add__(self, other):
"""Adds two Time objects or a Time object and a number.
other: Time object or number of seconds
"""
if isinstance(other, Time):
return self.add_time(other)
else:
return self.increment(other)
def __radd__(self, other):
"""Adds two Time objects or a Time object and a number."""
return self.__add__(other)
def add_time(self, other):
"""Adds two time objects."""
assert self.is_valid() and other.is_valid()
seconds = self.time_to_int() + other.time_to_int()
return int_to_time(seconds)
def increment(self, seconds):
"""Returns a new Time that is the sum of this time and
seconds."""
seconds += self.time_to_int()
return int_to_time(seconds)
def is_valid(self):
"""Checks whether a Time object satisfies the invariants."""
if self.hour < 0 or self.minute < 0 or self.second <
0:
return False
if self.minute >= 60 or self.second >= 60:
return False
return True
def int_to_time(seconds):
"""Makes a new Time object.
seconds: int seconds since midnight.
"""
minutes, second = divmod(seconds, 60)
hour, minute = divmod(minutes, 60)
time = Time(hour, minute, second)
return time
# TESTING features of Class Time
def main():
start = Time(9, 45, 00)
start.print_time()
end = start.increment(1337)
end.print_time()
print('Is end after start?', end=" ")
print(end.is_after(start))
# Testing __str__
print(f'Using __str__: {start} {end}')
# Testing addition
start = Time(9, 45)
duration = Time(1, 35)
print(start + duration)
print(start + 1337)
print(1337 + start)
print('Example of polymorphism')
t1 = Time(7, 43)
t2 = Time(7, 41)
t3 = Time(7, 37)
total = sum([t1, t2, t3])
print(total)
# A time that is invalid
t1 = Time(50)
print(t1)
def convert24To12(str):
sss=str[6]+str[7] #get seconds
se = (int)(sss) #convert seconds to integer
h1 = ord(str[0]) - ord('0') #get first string
h2 = ord(str[1]) - ord('0') #get second string
hour = h1 * 10 + h2 #for example 12:55:43 here h1=1, h2=2,hour=1*10+2=>12
meridien="";
if (((int)(hour/12))%2 == 0): # find the meridien i.e AM or PM
meridien = "AM"
else:
meridien = "PM"
if (hour == 0): #handle if hour is 00
print("00", end = "") # it will print hour
if se!=0:
for i in range(2, 6): #It will Print minutes
print(str[i], end = "")
print(sss,end="") #it will print seconds
else:
for i in range(2, 5): #It will Print minutes if second is 00
print(str[i], end = "")
elif hour==12: #handle if hour is 12
print("12",end = "") # it will print hour
if se!=0:
for i in range(2, 6): #It will Print minutes
print(str[i], end = "")
print(sss,end="") #it will print seconds
else:
for i in range(2, 5): #It will Print minutes if seconds is 00
print(str[i], end = "")
else: #handle if hour is not 00 or 12
hour %= 12; #convert hour from 24 to 12 hour formate
if hour<10:
print("0",end = "")
print(hour,end="") # it will print hour
if se!=0:
for i in range(2, 6): #It will Print minutes
print(str[i], end = "")
print(sss,end="") #it will print seconds
else:
for i in range(2, 5): #It will Print minutes if second is 00
print(str[i], end = "")
print(" " + meridien); #after time print meridien
if __name__ == '__main__': # Driver code
str = input("Enter time in hh:mm:ss formate: ") #input
convert24To12(str); #calling the function