From 8c8cc93b23925eb98ad500ab98b2ea0e6ab1190d Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Fri, 29 Nov 2024 09:31:48 +0100 Subject: [PATCH] solution2.5.2.7.scala added Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.7.scala | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.7.scala diff --git a/chapter02/worksheets/solution2.5.2.7.scala b/chapter02/worksheets/solution2.5.2.7.scala new file mode 100644 index 0000000..f9f9f81 --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.7.scala @@ -0,0 +1,24 @@ +/** + Exercise 2.5.2.7 + Reverse a sentence’s word order, but keep the words unchanged: + + def revSentence(s: String): String = ??? + + scala> revSentence("A quick brown fox") // Words are separated by one space. + + res0: String = "fox brown quick A" + */ + +def revSentence(s: String): String = { + s.split(' ').reverse.mkString(" ") +} + +val result = revSentence("A quick brown fox") +val expected: String = "fox brown quick A" +assert(result == expected) + +// scala> :load solution2.5.2.7.scala +// :load solution2.5.2.7.scala +// def revSentence(s: String): String +// val result: String = fox brown quick A +// val expected: String = fox brown quick A