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