summaryrefslogtreecommitdiff
path: root/4-kyu/human-readable-duration-format.hs
diff options
context:
space:
mode:
Diffstat (limited to '4-kyu/human-readable-duration-format.hs')
-rw-r--r--4-kyu/human-readable-duration-format.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/4-kyu/human-readable-duration-format.hs b/4-kyu/human-readable-duration-format.hs
new file mode 100644
index 0000000..d506d2a
--- /dev/null
+++ b/4-kyu/human-readable-duration-format.hs
@@ -0,0 +1,26 @@
+-- https://www.codewars.com/kata/52742f58faf5485cae000b9a
+module FormatDuration where
+
+import Data.List
+
+units = ["second", "minute", "hour", "day", "year"]
+
+unitsLen :: (Integral i) => [i]
+unitsLen = [60, 60, 24, 365]
+
+seris [] cur ret = reverse (cur:ret)
+seris (x:xs) cur ret = seris xs (cur `div` x) ((cur `mod` x):ret)
+
+zipUnit nums = filter (/="") $ zipWith fn nums units
+ where
+ fn 0 unit = ""
+ fn 1 unit = "1 " ++ unit
+ fn n unit = (show n) ++ " " ++ unit ++ "s"
+
+joinUnits :: [String] -> String
+joinUnits [x] = x
+joinUnits (x:xs) = (intercalate ", " $ reverse xs) ++ " and " ++ x
+
+formatDuration :: (Integral i, Show i) => i -> String
+formatDuration n = if toInteger n == 0 then "now"
+ else joinUnits $ zipUnit $ seris unitsLen n []