Today's challenge is from davazp on CodeWars.
The function will accept an input of non-negative integers. If it is zero, it just returns
"now"
. Otherwise, the duration is expressed as a combination ofyears
,days
,hours
,minutes
, andseconds
, in that order.The resulting expression is made of components like
4 seconds
,1 year
, etc. The unit of time is used in plural if the integer is greater than 1. The components are separated by a comma and a space (", "), except the last component which is separated by" and "
, just like it would be written in English. For the purposes of this challenge, a year is 365 days and a day is 24 hours. Note that spaces are important.The challenge is much easier to understand through example:
format_duration(62) # returns "1 minute and 2 seconds"
format_duration(3662) # returns "1 hour, 1 minute and 2 seconds"
Definitely a useful bit of code to have. It's much easier to work in seconds sometimes while coding, but easier to read human time in actual use.
Good luck!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (19)
JavaScript
With a little bit of cheating: instead of replacing the last comma with an "and", I just add the "and" to the last element and claim that everyone should be using Oxford comma to avoid misunderstandings.
Live demo on CodePen.
This feels overcomplicated. It probably can be done easier with a
map
or areduce
. I'll check later with more time.Hello Alvaro. Did u get a chance to work on this?🤓
LOL. I didn't 😓😬
Thats fine Alvaro. Are u on discord??
Not really. I have only used once or twice.
Cool.i like the way you explain things man. Just want to be in touch with you to ask some basic doubta as im a beginner. Is there any other way i can be in touch. Do u have wssap? I wont bother u much.dont be scared😅
I'm not going to lie: I've had bad experiences with this in the past. If you have questions and you post them here or in StackOverflow, I'll be happy to look at them and answer if I know the answer.
Easy to muck up, that's for sure. For awhile, I had this thing trying to say that
3600
was equal to several days (instead of an hour).Quite enjoyable. Turns out that Wolfram Alpha stops being terribly useful if you give it a large number of seconds, so had to go "sure, that looks right" a couple times.
Gist: gist.github.com/kerrishotts/e655d6...
Javascript (no idea whether this works or not :D )
I may change the formatter function to be more elegant and less crappy using slice and arrays and idontknow.
(Late) Rust Solution
Python3
Solution
Output
The
pretty_output
method was more challenging than I first thought, I did get a bit lazy with the if/elif/else statementsRuby
4500 seconds: 1 hour and 15 minutes
2000487 seconds: 23 days, 3 hours, 41 minutes and 27 seconds
42000487 seconds: 1 year, 121 days, 2 hours, 48 minutes and 7 seconds
1563739200 seconds: 49 years, 213 days and 20 hours
Elixir:
Ahh ya I didn't notice that on my first quick read through. I did get the commas right, I believe, so I got that going for me!
The part of this that I really like is just defining the conversion functions, it reads much better than the solution that I came up with in terms of how the conversions all work!
A functional style one in JS
that was fun 😋