-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#4_kyu_Human_readable_duration_format.py
114 lines (101 loc) · 4.85 KB
/
#4_kyu_Human_readable_duration_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Description:
"""
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.
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.
It is much easier to understand with an example:
formatDuration(62) // returns "1 minute and 2 seconds"
formatDuration(3662) // returns "1 hour, 1 minute and 2 seconds"
format_duration(62) # returns "1 minute and 2 seconds"
format_duration(3662) # returns "1 hour, 1 minute and 2 seconds"
format_duration(62) # returns "1 minute and 2 seconds"
format_duration(3662) # returns "1 hour, 1 minute and 2 seconds"
format_duration(62) # returns "1 minute and 2 seconds"
format_duration(3662) # returns "1 hour, 1 minute and 2 seconds"
format_duration(62) # returns "1 minute and 2 seconds"
format_duration(3662) # returns "1 hour, 1 minute and 2 seconds"
formatDuration 62 -- returns "1 minute and 2 seconds"
formatDuration 3662 -- returns "1 hour, 1 minute and 2 seconds"
TimeFormatter.formatDuration(62) //returns "1 minute and 2 seconds"
TimeFormatter.formatDuration(3662) //returns "1 hour, 1 minute and 2 seconds"
Kata.formatDuration(62) //returns "1 minute and 2 seconds"
Kata.formatDuration(3662) //returns "1 hour, 1 minute and 2 seconds"
duration 62 # echos "1 minute and 2 seconds"
duration 3662 # echos "1 hour, 1 minute and 2 seconds"
For the purpose of this Kata, a year is 365 days and a day is 24 hours.
Note that spaces are important.
Detailed rules
The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. 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.
A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.
Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.
A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.
A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.
"""
My codes:
def format_duration(second):
if second == 0:
return "now"
seconds = second%60
minutes= int((second%3600)/60)
hours = int((second%86400)/3600)
days = int((second%31536000)/86400)
years = int(second/31536000)
tmp = sum([1 for x in [seconds,minutes,hours,days,years] if x > 0])
ans = [x for x in [["years",years],["days",days],["hours",hours],["minutes",minutes],["seconds",seconds]] if x[1]>0]
if tmp == 1:
if ans[0][1] == 1:
return str(ans[0][1])+ " " + ans[0][0][:-1]
else:
return str(ans[0][1])+ " " + ans[0][0]
else:
new = ""
for x in ans[:-2]:
if x[1] == 1:
new += str(x[1])+ " " + x[0][:-1] + ", "
else:
new += str(x[1])+ " " + x[0] + ", "
if ans[-2][1] == 1:
new += str(ans[-2][1])+ " " + ans[-2][0][:-1] + " and "
else:
new += str(ans[-2][1])+ " " + ans[-2][0] + " and "
if ans[-1][1] == 1:
new += str(ans[-1][1])+ " " + ans[-1][0][:-1]
else:
new += str(ans[-1][1])+ " " + ans[-1][0]
return new
Others codes:
times = [("year", 365 * 24 * 60 * 60),
("day", 24 * 60 * 60),
("hour", 60 * 60),
("minute", 60),
("second", 1)]
def format_duration(seconds):
if not seconds:
return "now"
chunks = []
for name, secs in times:
qty = seconds // secs
if qty:
if qty > 1:
name += "s"
chunks.append(str(qty) + " " + name)
seconds = seconds % secs
return ', '.join(chunks[:-1]) + ' and ' + chunks[-1] if len(chunks) > 1 else chunks[0]
times=[('year',365*24*60*60),
('day',24*60*60),
('hour',60*60),
('minute',60),
('second',1)]
def format_duration(seconds):
#your code here
if not seconds:
return 'now'
chunks=[]
for name,secs in times:
qty=seconds//secs
if qty:
if qty>1:
name+='s'
chunks.append(str(qty)+' '+name)
seconds = seconds %secs
return (', '.join(chunks[:-1]) + ' and ' + chunks[-1] if len(chunks) > 1 else chunks[0])