Skip to content

Commit

Permalink
Merge pull request #75 from andreas-roehler/master
Browse files Browse the repository at this point in the history
solution2.5.2.7.scala added
  • Loading branch information
winitzki authored Nov 29, 2024
2 parents 9a4a6f2 + 8c8cc93 commit 98cdc1f
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 98cdc1f

Please sign in to comment.