Convert seconds to compound duration: Difference between revisions

m
m (→‎Python: Functional: Updated output)
Line 2,791:
 
 
# compoundDurationFromUnits :: [Num] -> [String] -> Num -> [(Num, String)]
def compoundDurationFromUnits(qs):
'''A list of compound string representions of a number n of time units,
Line 2,798:
return lambda ks: lambda n: list(
chain.from_iterable(map(
lambda kv, vk: [str(v) + ' ' +, k)] if 0 < v else [],
ks,
mapAccumR(
lambda a, x: divmod(a, x) if 0 < x else (1, a)
)(n)(qs)[1],
ks,
))
)
Line 2,813:
particular set of units and labels.
'''
 
print(
fTable('Compound durations:\n')(lambda x:from str(x)numbers +of 'sseconds:\n')(str)(
lambda n: quoted("', '.join(")
)(
lambda n: ', '.join([
str(v) + ' ' + k for v, k in
compoundDurationFromUnits([0, 7, 24, 60, 60])(
['wk', 'd', 'hr', 'min', 'sec']
)(n)
])
)([7259, 86400, 6000000])
)
Line 2,855 ⟶ 2,859:
reduce(go, reversed(xs), (acc, []))
)
 
 
# quoted :: Char -> String -> String
def quoted(c):
'''A string flanked on both sides
by a specified quote character.
'''
return lambda s: c + s + c
 
 
Line 2,861 ⟶ 2,873:
main()</lang>
{{Out}}
<pre>Compound durations from numbers of seconds:
 
7259s7259 -> '2 hr, 59 sec'
86400s86400 -> '1 d'
6000000s6000000 -> '9 wk, 6 d, 10 hr, 40 min'</pre>
 
=={{header|Racket}}==
9,659

edits