My Python script is nearly there, so here it is:
def prettyList(human_time):
if len(human_time) > 1:
return ' '.join([', '.join(human_time[:-1]), "and", human_time[-1]])
elif len(human_time) == 1:
return human_time[0]
else:
return ""
def format_duration(seconds, granularity = 5):
intervals = (('years', 29030400), ('months', 2419200), ('weeks', 604800),('days', 86400),('hours', 3600), ('minutes', 60), ('seconds', 1))
human_time = []
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
name = name.rstrip('s')
human_time.append("{} {}".format(value, name))
if not human_time:
return "now"
human_time = human_time[:granularity]
return prettyList(human_time)
The code should take an input of seconds, which then reads with proper grammar a list of durations (years, months, weeks, days, hours, minutes, seconds). My tests are passing but for one of the larger tests, it's reading the following:
'6 months, 2 weeks, 1 hour, 44 minutes and 40 seconds' should equal '182 days, 1 hour, 44 minutes and 40 seconds'
Essentially it's requiring that I translate what's now 6 months and 2 weeks into 182 days. Please advise!
What I have tried:
I've attempted to translate seconds as durations, then join in accordance with the proper commas and "and"s to make the output statement grammatically sound. My grammar is on point, it's the durations which are givin me trouble now!