From bd4b8e5e08c61a2b993d57bed4f7752bb1726054 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 29 Sep 2024 15:36:27 +0200 Subject: [PATCH 1/4] solution2.5.2.3.scala provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.3.scala | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.3.scala diff --git a/chapter02/worksheets/solution2.5.2.3.scala b/chapter02/worksheets/solution2.5.2.3.scala new file mode 100644 index 0000000..8e86800 --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.3.scala @@ -0,0 +1,69 @@ +/** + Exercise 2.5.2.3 + + For a given integer 𝑛, compute the sum of cubed digits, then the sum + of cubed digits of the result, etc.; + + stop the resulting sequence when it repeats itself, and so determine + whether it ever reaches 1. + + (Use Exercise 2.5.2.1.) + + def cubes(n: Int): Stream[Int] = ??? + + scala> cubes(123).take(10).toList + res0: List[Int] = List(123, 36, 243, 99, 1458, 702, 351, 153, 153, 153) + + scala> cubes(2).take(10).toList + res1: List[Int] = List(2, 8, 512, 134, 92, 737, 713, 371, 371, 371) + + scala> cubes(4).take(10).toList + res2: List[Int] = List(4, 64, 280, 520, 133, 55, 250, 133, 55, 250) + + def cubesReach1(n: Int): Boolean = ??? + + scala> cubesReach1(10) + res3: Boolean = true + + scala> cubesReach1(4) + res4: Boolean = false + */ + +def digitsOf(n: Int): Seq[Int] = { + Stream.iterate(n) { nk => nk / 10 } + .takeWhile { nk => nk != 0 } + .map { nk => nk % 10 } + .toList.reverse +} + +def digitsFSum(x: Int)(f: Int => Int): Int = { + // digitsOf(x).map{ x => f(x)}.foldLeft(0)(_ + _) + digitsOf(x).map{ x => f(x)}.sum +} + +def digitsListSum(x: Int, xs: Seq[Int] = Seq(), f: (Int => Int)): Seq[Int] = { + // digitsOf(x).map{ x => f(x)}.foldLeft(0)(_ + _) + // var xs: Seq[Int] = Seq() + val a: Int = digitsOf(x).map{ x => f(x)}.sum + println(s"a: ${a}") + if (xs.contains(a)) xs.reverse + else digitsListSum(a, a +: xs, f) +} + +def cubes(n: Int): Seq[Int] = { + digitsListSum(n, xs = Seq(), { k => k * k * k }) +} + +def cubesReach1(x: Int): Boolean = { + val a = Cubes.cubes(x) + a.length == 1 +} + +val result = cubesReach1(10) +val expected = true +assert(result == expected) + +val result2 = cubesReach1(4) +val expected2 = false +assert(result2 == expected2) + From f850ae51c6ddc04226665d4b5783bfecc7925fff Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 29 Sep 2024 15:44:38 +0200 Subject: [PATCH 2/4] solution2.5.2.3.scala fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.3.scala | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.3.scala b/chapter02/worksheets/solution2.5.2.3.scala index 8e86800..25cfbd5 100644 --- a/chapter02/worksheets/solution2.5.2.3.scala +++ b/chapter02/worksheets/solution2.5.2.3.scala @@ -11,15 +11,6 @@ def cubes(n: Int): Stream[Int] = ??? - scala> cubes(123).take(10).toList - res0: List[Int] = List(123, 36, 243, 99, 1458, 702, 351, 153, 153, 153) - - scala> cubes(2).take(10).toList - res1: List[Int] = List(2, 8, 512, 134, 92, 737, 713, 371, 371, 371) - - scala> cubes(4).take(10).toList - res2: List[Int] = List(4, 64, 280, 520, 133, 55, 250, 133, 55, 250) - def cubesReach1(n: Int): Boolean = ??? scala> cubesReach1(10) @@ -36,16 +27,8 @@ def digitsOf(n: Int): Seq[Int] = { .toList.reverse } -def digitsFSum(x: Int)(f: Int => Int): Int = { - // digitsOf(x).map{ x => f(x)}.foldLeft(0)(_ + _) - digitsOf(x).map{ x => f(x)}.sum -} - def digitsListSum(x: Int, xs: Seq[Int] = Seq(), f: (Int => Int)): Seq[Int] = { - // digitsOf(x).map{ x => f(x)}.foldLeft(0)(_ + _) - // var xs: Seq[Int] = Seq() val a: Int = digitsOf(x).map{ x => f(x)}.sum - println(s"a: ${a}") if (xs.contains(a)) xs.reverse else digitsListSum(a, a +: xs, f) } @@ -59,6 +42,11 @@ def cubesReach1(x: Int): Boolean = { a.length == 1 } + +cubes(123) +cubes(10) +cubes(4) + val result = cubesReach1(10) val expected = true assert(result == expected) From 7dbff1eaedb1ab39678abb075fe9544382b741bb Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 30 Sep 2024 10:02:19 +0200 Subject: [PATCH 3/4] solution2.5.2.3.scala fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.3.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.3.scala b/chapter02/worksheets/solution2.5.2.3.scala index 25cfbd5..dba5c10 100644 --- a/chapter02/worksheets/solution2.5.2.3.scala +++ b/chapter02/worksheets/solution2.5.2.3.scala @@ -38,8 +38,8 @@ def cubes(n: Int): Seq[Int] = { } def cubesReach1(x: Int): Boolean = { - val a = Cubes.cubes(x) - a.length == 1 + val a = cubes(x) + a.length == 1 && a(0) == 1 } From 6957163d78a1d6171e4e7952d6e8efa165de4096 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 30 Sep 2024 12:47:02 +0200 Subject: [PATCH 4/4] Fix, tests added Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.3.scala | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.3.scala b/chapter02/worksheets/solution2.5.2.3.scala index dba5c10..9e36eb7 100644 --- a/chapter02/worksheets/solution2.5.2.3.scala +++ b/chapter02/worksheets/solution2.5.2.3.scala @@ -29,7 +29,9 @@ def digitsOf(n: Int): Seq[Int] = { def digitsListSum(x: Int, xs: Seq[Int] = Seq(), f: (Int => Int)): Seq[Int] = { val a: Int = digitsOf(x).map{ x => f(x)}.sum - if (xs.contains(a)) xs.reverse + if (xs.contains(a)) + if (a == 1) xs + else xs.reverse else digitsListSum(a, a +: xs, f) } @@ -39,7 +41,7 @@ def cubes(n: Int): Seq[Int] = { def cubesReach1(x: Int): Boolean = { val a = cubes(x) - a.length == 1 && a(0) == 1 + a(0) == 1 } @@ -55,3 +57,10 @@ val result2 = cubesReach1(4) val expected2 = false assert(result2 == expected2) +assert ( cubesReach1(20) == false ) +assert ( cubesReach1(50) == false ) +assert ( cubesReach1(100) == true ) + +assert ( cubesReach1(112) == true ) +assert ( cubesReach1(114) == false ) +assert ( cubesReach1(778) == true )