From 94c5af72aa1a802ca191144faacc206be9699dc2 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Fri, 29 Nov 2024 16:16:15 +0100 Subject: [PATCH 1/5] solution2.5.2.8.scala provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.8.scala | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.8.scala diff --git a/chapter02/worksheets/solution2.5.2.8.scala b/chapter02/worksheets/solution2.5.2.8.scala new file mode 100644 index 0000000..8b1369a --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.8.scala @@ -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 From 7e063ff8711881c47353c61cfe38f798dc835f6d Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sat, 30 Nov 2024 21:55:37 +0100 Subject: [PATCH 2/5] solution2.5.2.8_stream_iterate.scala provided Signed-off-by: Andreas Roehler --- .../solution2.5.2.8_stream_iterate.scala | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.8_stream_iterate.scala diff --git a/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala b/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala new file mode 100644 index 0000000..316f177 --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala @@ -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 +} + +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 From 2169e5f77c23d14e764a8200becdd765c3dd18c6 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 1 Dec 2024 18:31:56 +0100 Subject: [PATCH 3/5] Use revDigits in isPalindrome Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.8_stream_iterate.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala b/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala index 316f177..cc68544 100644 --- a/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala +++ b/chapter02/worksheets/solution2.5.2.8_stream_iterate.scala @@ -22,7 +22,7 @@ val expected: Int = 54321 assert(result == expected) def isPalindrome(n: Int): Boolean = { - n.toString.reverse.toInt == n + revDigits(n) == n } val iPexpected: Boolean = false From 216a701d53a443c134481741fbebcecfe850fc28 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 1 Dec 2024 19:02:09 +0100 Subject: [PATCH 4/5] Use revDigits in isPalindrome again Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.8.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.8.scala b/chapter02/worksheets/solution2.5.2.8.scala index 8b1369a..161a29b 100644 --- a/chapter02/worksheets/solution2.5.2.8.scala +++ b/chapter02/worksheets/solution2.5.2.8.scala @@ -22,7 +22,7 @@ val expected: Int = 54321 assert(result == expected) def isPalindrome(n: Int): Boolean = { - n.toString.reverse.toInt == n + revDigits(n) == n } val iPexpected: Boolean = false @@ -31,12 +31,12 @@ assert(iPresult == iPexpected) val iPexpected2: Boolean = true val iPresult2 = isPalindrome(123321) -assert(iPresult2 == iPexpected2) +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 result: Int / 54321 // val expected: Int = 54321 // def isPalindrome(n: Int): Boolean // val iPexpected: Boolean = false From 5e886904dfa6b999979974382321936aa0c56f85 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 1 Dec 2024 19:13:31 +0100 Subject: [PATCH 5/5] typo Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.8.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.5.2.8.scala b/chapter02/worksheets/solution2.5.2.8.scala index 161a29b..1e28ed7 100644 --- a/chapter02/worksheets/solution2.5.2.8.scala +++ b/chapter02/worksheets/solution2.5.2.8.scala @@ -31,7 +31,7 @@ assert(iPresult == iPexpected) val iPexpected2: Boolean = true val iPresult2 = isPalindrome(123321) -assert(iPresult2 =/ iPexpected2) +assert(iPresult2 == iPexpected2) // scala> :load solution2.5.2.8.scala // :load solution2.5.2.8.scala