From 258d6b285992d14778011c90e9916be734d5db50 Mon Sep 17 00:00:00 2001 From: Ahmed Allam Date: Wed, 14 Feb 2024 23:51:19 +0200 Subject: [PATCH] Human readable duration format --- Python/Kyu4/Human readable duration format.py | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Python/Kyu4/Human readable duration format.py diff --git a/Python/Kyu4/Human readable duration format.py b/Python/Kyu4/Human readable duration format.py new file mode 100644 index 0000000..c4f72c0 --- /dev/null +++ b/Python/Kyu4/Human readable duration format.py @@ -0,0 +1,80 @@ +""" +|_______________________________________________________________________________ +| @Solver : GziXnine +| @Link : https://www.codewars.com/kata/52742f58faf5485cae000b9a +| @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: +| +| * For seconds = 62, your function should return +| "1 minute and 2 seconds" +| * For seconds = 3662, your function should return +| "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. +|_______________________________________________________________________________ +""" +def format_duration(seconds): + words = ["year", "day", "hour", "minute", "second"] + + if not seconds: + return "now" + else: + m, s = divmod(seconds, 60) + h, m = divmod(m, 60) + d, h = divmod(h, 24) + y, d = divmod(d, 365) + + time = [y, d, h, m, s] + + duration = [] + + for x, i in enumerate(time): + if i == 1: + duration.append(f"{i} {words[x]}") + elif i > 1: + duration.append(f"{i} {words[x]}s") + + if len(duration) == 1: + return duration[0] + elif len(duration) == 2: + return f"{duration[0]} and {duration[1]}" + else: + return ", ".join(duration[:-1]) + " and " + duration[-1]