From a8a33315705a80c0b48f888763774f56d249ee31 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sat, 14 Dec 2024 20:40:35 +0100 Subject: [PATCH 1/2] solution2.5.2.11.scala provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.11.scala | 113 ++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.11.scala diff --git a/chapter02/worksheets/solution2.5.2.11.scala b/chapter02/worksheets/solution2.5.2.11.scala new file mode 100644 index 0000000..2fe86b8 --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.11.scala @@ -0,0 +1,113 @@ +/** + Exercise 2.5.2.11 + (a) For a given integer interval [𝑛1 , 𝑛2], find the largest integer + 𝑘 ∈ [𝑛1 , 𝑛2] such that the decimal representation of 𝑘 does + not contain any of the digits 3, 5, or 7. + + The required type signature and a sample test: + + def largestNotThreeFiveSeven(a: Long, b: Long): Long = ??? + + scala> largestNotThreeFiveSeven(19, 97) + res0: Long = 96 + + (b) For a given integer interval [𝑛1 , 𝑛2], find the integer 𝑘 ∈ [𝑛1 , 𝑛2] with the + largest sum of decimal digits. + + (c) A positive integer 𝑛 is called a perfect number if it is equal to the sum of + its divisors (integers 𝑘 such that 1 ≤ 𝑘 < 𝑛 and 𝑘 divides 𝑛). + For example, 6 is a perfect number because its divisors are 1, 2, + and 3, and 1 + 2 + 3 = 6, while 8 is not a perfect number because + its divisors are 1, 2, and 4, and 1 + 2 + 4 = 7 ≠ 8. + + Write a function that determines whether a given number 𝑛 is perfect. + Determine all perfect numbers up to one million. + */ + +def largestNotThreeFiveSeven(a: Long=19, b: Long = 97): Long = { + val xs = (a to b).toList + val ys = xs.map(x => x.toString).map(_.toString).filter(! _.contains("3")).filter(! _.contains("5")).filter(! _.contains("7")).last + ys.toLong +} + +def largestSumDezDigitsIntern(xs: List[Long], res: Long = 0, thisNumber: Long = 0): Long = { + if (xs.isEmpty) thisNumber + else { + val a = xs.head.toString.toList.map(_.asDigit).sum + if (res < a) { + largestSumDezDigitsIntern(xs.tail, a, xs.head) + } + else + { + largestSumDezDigitsIntern(xs.tail, res, thisNumber) + } + } +} + +def largestSumDezDigits(a: Long = 9, b: Long = 101): Long = { + val xs = (a to b).toList + largestSumDezDigitsIntern(xs) +} + +def allNeededDivisors(n: Int = 99): List[Int] = { + (1 to (n - 1)).toList.filter(n % _ == 0) +} + +def isPerfect(n: Int = 28): Boolean = { + val a: Int = allNeededDivisors(n).sum + allNeededDivisors(n).sum == n +} + +def perfectNumbersUpToN(n: Int): List[Int] = { + (1 to n).toList.filter(isPerfect(_)) +} + +val result = largestNotThreeFiveSeven() +val expected: Long = 96 +assert(result == expected) + +val result2 = largestSumDezDigits() +val expected2: Long = 99 +assert(result2 == expected2) + +val result3 = isPerfect(8) +val expected3 = false +assert(result3 == expected3) + +val result4 = isPerfect(6) +val expected4 = true +assert(result4 == expected4) + +val result5 = isPerfect(28) +val expected5 = true +assert(result5 == expected5) + +val result6 = isPerfect(33) +val expected6 = false +assert(result6 == expected6) + +val result7 = perfectNumbersUpToN(10000) +val expected7: List[Int] = List(6, 28, 496, 8128) +assert(result7 == expected7) + +// :load solution2.5.2.11.scala +// def largestNotThreeFiveSeven(a: Long, b: Long): Long +// def largestSumDezDigitsIntern(xs: List[Long], res: Long, thisNumber: Long): Long +// def largestSumDezDigits(a: Long, b: Long): Long +// def allNeededDivisors(n: Int): List[Int] +// def isPerfect(n: Int): Boolean +// def perfectNumbersUpToN(n: Int): List[Int] +// val result: Long = 96 +// val expected: Long = 96 +// val result2: Long = 99 +// val expected2: Long = 99 +// val result3: Boolean = false +// val expected3: Boolean = false +// val result4: Boolean = true +// val expected4: Boolean = true +// val result5: Boolean = true +// val expected5: Boolean = true +// val result6: Boolean = false +// val expected6: Boolean = false +// val result7: List[Int] = List(6, 28, 496, 8128) +// val expected7: List[Int] = List(6, 28, 496, 8128) From 2d34783a4151a43936fe4b35e9e4829c8cbd7c53 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 15 Dec 2024 19:35:08 +0100 Subject: [PATCH 2/2] solution2.5.2.12.scala provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.12.scala | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.12.scala diff --git a/chapter02/worksheets/solution2.5.2.12.scala b/chapter02/worksheets/solution2.5.2.12.scala new file mode 100644 index 0000000..c360c09 --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.12.scala @@ -0,0 +1,31 @@ +/** + Exercise 2.5.2.12 + + Transform a sequence by removing adjacent repeated elements when + they are repeated more than 𝑘 times. Repetitions up to 𝑘 times + should remain unchanged. + + The required type signature and a sample test: + + def removeDups[A](s: Seq[A], k: Int): Seq[A] = ??? + + scala> removeDups(Seq(1, 1, 1, 1, 5, 2, 2, 5, 5, 5, 5, 5, 1), 3) + res0: Seq[Int] = List(1, 1, 1, 5, 2, 2, 5, 5, 5, 1) + */ + +def removeDupsIntern[A](xs: Seq[A], k: Int, res: Seq[A] = Seq.empty): Seq[A] = { + if (xs.isEmpty) res.reverse + else { + val a: Seq[A] = xs.takeWhile(_ == xs.head).take(k) + val b = xs.dropWhile(_ == xs.head) + removeDupsIntern(b, k, a ++ res) + } +} + +def removeDups[A](s: Seq[A], k: Int): Seq[A] = { + removeDupsIntern(s, k) +} + +val result = removeDups(Seq(1, 1, 1, 1, 5, 2, 2, 5, 5, 5, 5, 5, 1), 3) +val expected: Seq[Int] = List(1, 1, 1, 5, 2, 2, 5, 5, 5, 1) +assert(result == expected)