Skip to content

Commit

Permalink
solution2.5.2.8.scala provided (#76)
Browse files Browse the repository at this point in the history
* solution2.5.2.8.scala provided

Signed-off-by: Andreas Roehler <[email protected]>

* solution2.5.2.8_stream_iterate.scala provided

Signed-off-by: Andreas Roehler <[email protected]>

* Use revDigits in isPalindrome

Signed-off-by: Andreas Roehler <[email protected]>

* Use revDigits in isPalindrome again

Signed-off-by: Andreas Roehler <[email protected]>

* typo

Signed-off-by: Andreas Roehler <[email protected]>

---------

Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler authored Dec 1, 2024
1 parent 98cdc1f commit 0ea97f1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
45 changes: 45 additions & 0 deletions chapter02/worksheets/solution2.5.2.8.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
Exercise 2.5.2.8
(a) Reverse an integer’s digits (see Example 2.5.1.6) as shown:
def revDigits(n: Int): Int = ???
scala> revDigits(12345)
res0: Int = 54321
(b) A palindrome integer is an integer number n such that revDigits(n) == n.
Write a predicate function of type Int => Boolean that checks
whether a given positive integer is a palindrome.
*/

def revDigits (n: Int): Int = {
n.toString.reverse.toInt
}

val result = revDigits(12345)
val expected: Int = 54321
assert(result == expected)

def isPalindrome(n: Int): Boolean = {
revDigits(n) == n
}

val iPexpected: Boolean = false
val iPresult = isPalindrome(123)
assert(iPresult == iPexpected)

val iPexpected2: Boolean = true
val iPresult2 = isPalindrome(123321)
assert(iPresult2 == iPexpected2)

// scala> :load solution2.5.2.8.scala
// :load solution2.5.2.8.scala
// def revDigits(n: Int): Int
// val result: Int / 54321
// val expected: Int = 54321
// def isPalindrome(n: Int): Boolean
// val iPexpected: Boolean = false
// val iPresult: Boolean = false
// val iPexpected2: Boolean = true
// val iPresult2: Boolean = true
47 changes: 47 additions & 0 deletions chapter02/worksheets/solution2.5.2.8_stream_iterate.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
Exercise 2.5.2.8
(a) Reverse an integer’s digits (see Example 2.5.1.6) as shown:
def revDigits(n: Int): Int = ???
scala> revDigits(12345)
res0: Int = 54321
(b) A palindrome integer is an integer number n such that revDigits(n) == n.
Write a predicate function of type Int => Boolean that checks
whether a given positive integer is a palindrome.
*/

def revDigits(n: Int): Int = {
Stream.iterate(n)(_ / 10).takeWhile(_ != 0).map(_ % 10).toList.reverse.foldRight(0)(_ + _ * 10)
}

val result = revDigits(12345)
val expected: Int = 54321
assert(result == expected)

def isPalindrome(n: Int): Boolean = {
revDigits(n) == n
}

val iPexpected: Boolean = false
val iPresult = isPalindrome(123)
assert(iPresult == iPexpected)

val iPexpected2: Boolean = true
val iPresult2 = isPalindrome(123321)
assert(iPresult2 == iPexpected2)

// scala> :load solution2.5.2.8_stream_iterate.scala
// :load solution2.5.2.8_stream_iterate.scala
// there was 1 deprecation warning; re-run with -deprecation for details
// 1 warning found
// def revDigits(n: Int): Int
// val result: Int = 54321
// val expected: Int = 54321
// def isPalindrome(n: Int): Boolean
// val iPexpected: Boolean = false
// val iPresult: Boolean = false
// val iPexpected2: Boolean = true
// val iPresult2: Boolean = true

0 comments on commit 0ea97f1

Please sign in to comment.