From b58916029a92c20e593d8628c4d9dc248081a853 Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Tue, 16 Feb 2021 11:06:46 +0100 Subject: to24H --- challenge-100/frankivo/scala/FunTime.scala | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-100/frankivo/scala/FunTime.scala diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala new file mode 100644 index 0000000000..2b06d901cb --- /dev/null +++ b/challenge-100/frankivo/scala/FunTime.scala @@ -0,0 +1,35 @@ +object FunTime { + val examples = Seq[String]( + "08:24pm", + "08:24am", + "09:39 am", + "08:24", + "20:24", + "13:37", + "12:34" + ) + + + def to12H(time: String): Option[String] = { + + None + } + + def to24H(time: String): Option[String] = { + if (!("(am|pm)$".r findFirstIn time).isDefined) + return None + + val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq + val hour = if (time.contains("pm")) h.toInt + 12 else h + + Some(s"$hour:$m") + } + + def main(args: Array[String]): Unit = { + examples + .map(e => Seq(to12H(e), to24H(e))) + .flatten + .flatten + .foreach(println) + } +} \ No newline at end of file -- cgit From 65ea62571cc749356e227dc27f7dc5116620e360 Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Tue, 16 Feb 2021 11:20:21 +0100 Subject: cleanup --- challenge-100/frankivo/scala/FunTime.scala | 37 +++++++++++++++++++----------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala index 2b06d901cb..6aa4d83229 100644 --- a/challenge-100/frankivo/scala/FunTime.scala +++ b/challenge-100/frankivo/scala/FunTime.scala @@ -1,35 +1,44 @@ object FunTime { val examples = Seq[String]( + "07:04am", "08:24pm", "08:24am", "09:39 am", "08:24", "20:24", "13:37", - "12:34" + "12:34", + "18:04" ) + def getTime(time: String) : (Int, Int) = { + val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq + (h.toInt, m.toInt) + } - def to12H(time: String): Option[String] = { - - None + def makeTime(hour: Int, minute: Int, suffix: String = "") = { + "%02d:%02d%s".format(hour, minute, " " + suffix) } - def to24H(time: String): Option[String] = { - if (!("(am|pm)$".r findFirstIn time).isDefined) - return None - - val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq - val hour = if (time.contains("pm")) h.toInt + 12 else h + def to12H(time: String): String = { + time + } + + def to24H(time: String): String = { + val parsed = getTime(time) + val hour = if (time.contains("pm")) parsed._1 + 12 else parsed._1 - Some(s"$hour:$m") + makeTime(hour, parsed._2) } def main(args: Array[String]): Unit = { examples - .map(e => Seq(to12H(e), to24H(e))) - .flatten - .flatten + .map(e => { + if (("(am|pm)$".r findFirstIn e).isDefined) + to24H(e) + else + to12H(e) + }) .foreach(println) } } \ No newline at end of file -- cgit From 5e9f6a7f1a0a9da2ac250a53206aca5bc2eb2323 Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Tue, 16 Feb 2021 11:34:42 +0100 Subject: to12H --- challenge-100/frankivo/scala/FunTime.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala index 6aa4d83229..0578ea6551 100644 --- a/challenge-100/frankivo/scala/FunTime.scala +++ b/challenge-100/frankivo/scala/FunTime.scala @@ -5,9 +5,12 @@ object FunTime { "08:24am", "09:39 am", "08:24", - "20:24", - "13:37", + "11:59", + "12:00", + "12:01", "12:34", + "13:37", + "20:24", "18:04" ) @@ -21,7 +24,12 @@ object FunTime { } def to12H(time: String): String = { - time + val parsed = getTime(time) + + val suffix = if (parsed._1 < 12) "am" else "pm" + val hour = if (parsed._1 > 12) parsed._1 - 12 else parsed._1 + + makeTime(hour, parsed._2, suffix) } def to24H(time: String): String = { -- cgit From db3d3298e10b3dcf63bf1e0f940e2f69af95eb38 Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Tue, 16 Feb 2021 11:37:47 +0100 Subject: convert --- challenge-100/frankivo/scala/FunTime.scala | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala index 0578ea6551..97ae8f09a5 100644 --- a/challenge-100/frankivo/scala/FunTime.scala +++ b/challenge-100/frankivo/scala/FunTime.scala @@ -39,14 +39,16 @@ object FunTime { makeTime(hour, parsed._2) } + def convert(time: String) : String = { + if (("(am|pm)$".r findFirstIn time).isDefined) + to24H(time) + else + to12H(time) + } + def main(args: Array[String]): Unit = { examples - .map(e => { - if (("(am|pm)$".r findFirstIn e).isDefined) - to24H(e) - else - to12H(e) - }) + .map(convert) .foreach(println) } } \ No newline at end of file -- cgit