Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution2.5.2.8.scala provided #76

Merged
merged 5 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {
n.toString.reverse.toInt == 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 = {
n.toString.reverse.toInt == n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I would expect you to call revDigits to get the reversed digits, rather than toString.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'm on it.

}

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
Loading