Skip to content

Instantly share code, notes, and snippets.

@mikezink
Created September 15, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/d9eeae063b1084f6ca22c87b07504b7c to your computer and use it in GitHub Desktop.
Save mikezink/d9eeae063b1084f6ca22c87b07504b7c to your computer and use it in GitHub Desktop.
HW1 Q1 Fall 2020
class Hw1Q1:
@staticmethod
def timeConvert(time):
result = ''
day = time // 24 // 60 // 60 # compute days
if day > 1:
result += str(time // 24 // 60 // 60) + ' days'
if day == 1:
result += str(time // 24 // 60 // 60) + ' day'
time %= 24 * 60 * 60
hour = time // 60 // 60 # compute hours
if hour > 0 and result:
result += ', '
if hour > 1:
result += str(time // 60 // 60) + ' hours'
elif hour == 1:
result += str(time // 60 // 60) + ' hour'
time %= 60 * 60
minute = time // 60 # compute minutes
if minute > 0 and result:
result += ', '
if minute > 1:
result += str(time // 60) + ' minutes'
elif minute == 1:
result += str(time // 60) + ' minute'
time %= 60
if time > 0 and result:
result += ', '
if time > 1:
result += str(time) + ' seconds'
elif time == 1:
result += str(time) + ' second'
return result
@staticmethod
def time2str(time, result, name):
if time < 1:
return result
if result:
result += ', '
result += str(time) + ' ' + name
if time > 1:
result += 's'
return result
@staticmethod
def time2str2(time, result, name):
return result if time < 1 else (result + ', ' if result else '') + str(time) + ' ' + name + ('s' if time > 1 else '')
@staticmethod
def timeConvert_alternative(time):
result = Hw1Q1.time2str(time //24//60//60, '', 'day') # compute days
time %= 24*60*60
result = Hw1Q1.time2str(time //60//60, result, 'hour') # compute hours
time %= 60*60
result = Hw1Q1.time2str(time //60, result, 'minute') # compute minutes
time %= 60
return Hw1Q1.time2str(time, result, 'second')
if __name__ == "__main__":
# time = int(input('enter the number of seconds: '))
obj = Hw1Q1()
print(obj.timeConvert(100000))
print(obj.timeConvert(3600))
print(obj.timeConvert(3601))
print(obj.timeConvert(99999))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment