From af102d67147ba0f12776f63a9bf0400a090ad42f Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Mon, 19 Oct 2020 13:01:24 +0200 Subject: Challenge083 - Task #1 --- challenge-083/frankivo/WordsLength.scala | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 challenge-083/frankivo/WordsLength.scala diff --git a/challenge-083/frankivo/WordsLength.scala b/challenge-083/frankivo/WordsLength.scala new file mode 100644 index 0000000000..eec2dee30c --- /dev/null +++ b/challenge-083/frankivo/WordsLength.scala @@ -0,0 +1,22 @@ +object WordsLength { + val examples: Seq[String] = Seq( + "The Weekly Challenge", + "The purpose of our lives is to be happy" + ) + + def main(args: Array[String]): Unit = { + examples + .map(e => (e, count(e))) + .foreach(println) + } + + def count(line: String) : Int = { + val words = line.split(" ") + + words + .drop(1) + .take(words.length - 2) + .mkString + .length + } +} \ No newline at end of file -- cgit From 6093e73147f482425e8449824d4fcc0809171587 Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Mon, 19 Oct 2020 13:46:48 +0200 Subject: Use slice --- challenge-083/frankivo/WordsLength.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-083/frankivo/WordsLength.scala b/challenge-083/frankivo/WordsLength.scala index eec2dee30c..55fb871c2e 100644 --- a/challenge-083/frankivo/WordsLength.scala +++ b/challenge-083/frankivo/WordsLength.scala @@ -14,8 +14,7 @@ object WordsLength { val words = line.split(" ") words - .drop(1) - .take(words.length - 2) + .slice(1, words.length - 1) .mkString .length } -- cgit From 016c41bd4147ccda30131b0243197d10e6a50f5a Mon Sep 17 00:00:00 2001 From: Frank Oosterhuis Date: Mon, 19 Oct 2020 13:48:44 +0200 Subject: TIL about dropRight --- challenge-083/frankivo/WordsLength.scala | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/challenge-083/frankivo/WordsLength.scala b/challenge-083/frankivo/WordsLength.scala index 55fb871c2e..4888ca1439 100644 --- a/challenge-083/frankivo/WordsLength.scala +++ b/challenge-083/frankivo/WordsLength.scala @@ -11,10 +11,9 @@ object WordsLength { } def count(line: String) : Int = { - val words = line.split(" ") - - words - .slice(1, words.length - 1) + line.split(" ") + .drop(1) + .dropRight(1) .mkString .length } -- cgit