Skip to content

Instantly share code, notes, and snippets.

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 erezLieberman/420de856b4c94f803d72dd472aa968a2 to your computer and use it in GitHub Desktop.
Save erezLieberman/420de856b4c94f803d72dd472aa968a2 to your computer and use it in GitHub Desktop.
Udacity.com-Intro-to-Computer-Science-Course-Answers - Converting Seconds
# Write a procedure, convert_seconds, which takes as input a non-negative
# number of seconds and returns a string of the form
# '<integer> hours, <integer> minutes, <number> seconds' but
# where if <integer> is 1 for the number of hours or minutes,
# then it should be hour/minute. Further, <number> may be an integer
# or decimal, and if it is 1, then it should be followed by second.
# You might need to use int() to turn a decimal into a float depending
# on how you code this. int(3.0) gives 3
#
# Note that English uses the plural when talking about 0 items, so
# it should be "0 minutes".
#
def convert_seconds(number):
hours = int(number) / 3600
minutes = ((int(number) - (hours *3600)) / 60)
seconds = round(((number - (hours *3600) - (minutes *60))),2)
hoursString = "hour"
if(hours != 1):
hoursString = "hours"
minutesString = "minute"
if(minutes != 1):
minutesString = "minutes"
secondsString = "second"
if(seconds != 1):
secondsString = "seconds"
return repr(hours)+' ' + hoursString + ', '+repr(minutes) +" "+ minutesString +", "+repr(seconds) +" " + secondsString
print convert_seconds(3600)
#>>> 1 hour, 0 minutes, 1 seconds
print convert_seconds(3661)
#>>> 1 hour, 1 minute, 1 second
print convert_seconds(7325)
#>>> 2 hours, 2 minutes, 5 seconds
print convert_seconds(7261.7)
#>>> 2 hours, 1 minute, 1.7 seconds
# Write a procedure download_time which takes as inputs a file size, the
# units that file size is given in, bandwidth and the units for
# bandwidth (excluding per second) and returns the time taken to download
# the file.
# Your answer should be a string in the form
# "<number> hours, <number> minutes, <number> seconds"
# Some information you might find useful is the number of bits
# in kilobits (kb), kilobytes (kB), megabits (Mb), megabytes (MB),
# gigabits (Gb), gigabytes (GB) and terabits (Tb), terabytes (TB).
#print 2 ** 10 # one kilobit, kb
#print 2 ** 10 * 8 # one kilobyte, kB
#print 2 ** 20 # one megabit, Mb
#print 2 ** 20 * 8 # one megabyte, MB
#print 2 ** 30 # one gigabit, Gb
#print 2 ** 30 * 8 # one gigabyte, GB
#print 2 ** 40 # one terabit, Tb
#print 2 ** 40 * 8 # one terabyte, TB
# Often bandwidth is given in megabits (Mb) per second whereas file size
# is given in megabytes (MB).
def download_time(file_size,size_unit,bandwidth,bandwidth_unit):
if(size_unit == "kb"):
file_size = file_size * 2 ** 10
if(size_unit == "kB"):
file_size = file_size * 2 ** 10 * 8
if(size_unit == "Mb"):
file_size = file_size * 2 ** 20
if(size_unit == "MB"):
file_size = file_size * 2 ** 20 * 8
if(size_unit == "Gb"):
file_size = file_size * 2 ** 30
if(size_unit == "GB"):
file_size = file_size * 2 ** 30 * 8
if(size_unit == "Tb"):
file_size = file_size * 2 ** 40
if(size_unit == "TB"):
file_size = file_size * 2 ** 40 * 8
if(bandwidth_unit == "kb"):
bandwidth = bandwidth * 2 ** 10
if(bandwidth_unit == "kB"):
bandwidth = bandwidth * 2 ** 10 * 8
if(bandwidth_unit == "Mb"):
bandwidth = bandwidth * 2 ** 20
if(bandwidth_unit == "MB"):
bandwidth = bandwidth * 2 ** 20 * 8
if(bandwidth_unit == "Gb"):
bandwidth = bandwidth * 2 ** 30
if(bandwidth_unit == "GB"):
bandwidth = bandwidth * 2 ** 30 * 8
if(bandwidth_unit == "Tb"):
bandwidth = bandwidth * 2 ** 40
if(bandwidth_unit == "TB"):
bandwidth = bandwidth * 2 ** 40 * 8
number = float(file_size)/bandwidth
hours = int(number) / 3600
minutes = ((int(number) - (hours *3600)) / 60)
seconds = round(((number - (hours *3600) - (minutes *60))),10)
hoursString = "hour"
if(hours != 1):
hoursString = "hours"
minutesString = "minute"
if(minutes != 1):
minutesString = "minutes"
secondsString = "second"
if(seconds != 1):
secondsString = "seconds"
return repr(hours)+' ' + hoursString + ', '+repr(minutes) +" "+ minutesString +", "+repr(seconds) +" " + secondsString
print download_time(1,'TB', 100, 'Mb')
#>>> 23 hours, 18 minutes, 6.08 seconds
print download_time(5.56,'GB',1,'GB')
#>>> 0 hours, 0 minutes, 5.56 seconds
print download_time(1024,'kB', 1, 'MB')
#>>> 0 hours, 0 minutes, 1 second
print download_time(1024,'kB', 1, 'Mb')
#>>> 0 hours, 0 minutes, 8 seconds # 8.0 seconds is also acceptable
print download_time(13,'GB', 5.6, 'MB')
#>>> 0 hours, 39 minutes, 37.1428571429 seconds
print download_time(13,'GB', 5.6, 'Mb')
#>>> 5 hours, 16 minutes, 57.1428571429 seconds
print download_time(10,'MB', 2, 'kB')
#>>> 1 hour, 25 minutes, 20 seconds # 20.0 seconds is also acceptable
print download_time(10,'MB', 2, 'kb')
#>>> 11 hours, 22 minutes, 40 seconds # 40.0 seconds is also acceptable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment