Skip to content

Commit

Permalink
solution2.5.2.7.scala added
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Nov 29, 2024
1 parent 9a4a6f2 commit 8c8cc93
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions chapter02/worksheets/solution2.5.2.7.scala
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c8cc93

Please sign in to comment.