Click here to Skip to main content
15,888,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Python script is nearly there, so here it is:

Python
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:

Python
'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!
Posted
Updated 4-Oct-16 12:22pm
Comments
Richard MacCutchan 5-Oct-16 5:33am    
Your month value is 28 days which only applies to February in 3 out of every 4 years. Months are more likely to be 30 or 31 days long.

The value you calculate in days etc equates to 6.5 months by your (incorrect) values.

1 solution

Are you sure this is your code and that you can't make it reply in number of days ?
the change is really easy, you should be able to do it yourself.

Bug report:
You should check the number of seconds per years and per months, there is a surprise :)

Quote:
'6 months, 2 weeks, 1 hour, 44 minutes and 40 seconds' should equal '182 days, 1 hour, 44 minutes and 40 seconds'
If you crank your brain a little, you should discover that 182 days is about half a year or 6 months, not 6 months and 2 weeks!

[Update]
Quote:
The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

So, Where have you seen that you needed months and weeks ?
 
Share this answer
 
v2
Comments
Member 12776322 5-Oct-16 12:18pm    
I've changed my durations, and now test output is '26 weeks, 1 hour, 44 minutes and 40 seconds' should equal '182 days, 1 hour, 44 minutes and 40 seconds'

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 = 4):
intervals = (('years', 31104000), ('months', 31449600), ('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)
Patrice T 5-Oct-16 12:33pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Question: how do you know that you must answer in days.
Member 12776322 5-Oct-16 12:37pm    
Well, this is where I'm confused, because the instructions state the following:

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900