From b31c9aa31b2a7c215c1831a0cbe1a0267dd34756 Mon Sep 17 00:00:00 2001 From: Geravant Date: Tue, 31 Mar 2020 12:08:19 +0300 Subject: [PATCH 1/2] fix(Functional loops - Conditional Expressions ): Tests reworked Asswertions moved from the task file to the test file, placeholder texts updated, main function added to the task file to provide the ability to lunch the code Closes EDC-214 --- .../src/FunctionalLoops.scala | 12 +++++------- .../Conditional Expressions/task-info.yaml | 8 ++++---- .../Conditional Expressions/test/Test.scala | 11 ++++++++--- Functional Loops/lesson-info.yaml | 1 + 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Functional Loops/Conditional Expressions/src/FunctionalLoops.scala b/Functional Loops/Conditional Expressions/src/FunctionalLoops.scala index 090147d..368029e 100644 --- a/Functional Loops/Conditional Expressions/src/FunctionalLoops.scala +++ b/Functional Loops/Conditional Expressions/src/FunctionalLoops.scala @@ -1,13 +1,11 @@ import org.scalatest.{FlatSpec, Matchers} object FunctionalLoops extends FlatSpec with Matchers { + def factorial(n: Int): Int = + if (n == 0) 1 + else factorial(n - 1) * n - def factorialExercise(): Unit = { - def factorial(n: Int): Int = - if (n == 0) 1 - else factorial(n - 1) * n - - factorial(3) shouldBe 6 - factorial(4) shouldBe 24 + def main(args: Array[String]): Unit = { + println(factorial(5)) } } \ No newline at end of file diff --git a/Functional Loops/Conditional Expressions/task-info.yaml b/Functional Loops/Conditional Expressions/task-info.yaml index 594c775..098db5e 100644 --- a/Functional Loops/Conditional Expressions/task-info.yaml +++ b/Functional Loops/Conditional Expressions/task-info.yaml @@ -3,12 +3,12 @@ files: - name: src/FunctionalLoops.scala visible: true placeholders: - - offset: 179 + - offset: 135 length: 6 - placeholder_text: // TODO - - offset: 200 + placeholder_text: /*insert the corner case condition for the factorial evaluation*/ + - offset: 152 length: 20 - placeholder_text: // TODO + placeholder_text: //insert the common case factorial expression here - name: test/Test.scala visible: false - name: build.sbt diff --git a/Functional Loops/Conditional Expressions/test/Test.scala b/Functional Loops/Conditional Expressions/test/Test.scala index 2051bd7..408dc7d 100644 --- a/Functional Loops/Conditional Expressions/test/Test.scala +++ b/Functional Loops/Conditional Expressions/test/Test.scala @@ -1,9 +1,14 @@ -import org.scalatest.Spec +import FunctionalLoops.factorial +import org.scalatest.Matchers +import org.scalatest.refspec.RefSpec -class Test extends Spec { +class Test extends RefSpec with Matchers{ def `check factorial`(): Unit = { - FunctionalLoops.factorialExercise() + factorial(0) shouldBe 1 + factorial(3) shouldBe 6 + factorial(4) shouldBe 24 + factorial(10) shouldBe 3628800 } } diff --git a/Functional Loops/lesson-info.yaml b/Functional Loops/lesson-info.yaml index 7147d3d..ede5702 100644 --- a/Functional Loops/lesson-info.yaml +++ b/Functional Loops/lesson-info.yaml @@ -1,2 +1,3 @@ content: - Conditional Expressions +- Computing the Square Root of a Value From 186b4261845ad2ec64a1865ce67b0abb3ac4c1ec Mon Sep 17 00:00:00 2001 From: Geravant Date: Tue, 31 Mar 2020 13:37:35 +0300 Subject: [PATCH 2/2] fix(Functional Loops - Computing Square Root): Tests reworked Assertions moved from the task to the test file, placeholders updated, main method added Closes EDC-214 --- .../src/ComputingSquareRoot.scala | 23 ++++++++----------- .../task-info.yaml | 9 ++++---- .../test/TestSpec.scala | 9 +++++--- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Functional Loops/Computing the Square Root of a Value/src/ComputingSquareRoot.scala b/Functional Loops/Computing the Square Root of a Value/src/ComputingSquareRoot.scala index ff78070..efee7bc 100644 --- a/Functional Loops/Computing the Square Root of a Value/src/ComputingSquareRoot.scala +++ b/Functional Loops/Computing the Square Root of a Value/src/ComputingSquareRoot.scala @@ -1,20 +1,17 @@ -import org.scalatest.{FlatSpec, Matchers} +object ComputingSquareRoot{ -object ComputingSquareRoot extends FlatSpec with Matchers { + def sqrt(x: Double) = sqrtIter(1.0, x) - def sqrtExample(): Unit = { - def sqrt(x: Double) = sqrtIter(1.0, x) + def sqrtIter (guess: Double, x: Double): Double = + if (isGoodEnough(guess, x)) guess + else sqrtIter(improve(guess, x), x) - def sqrtIter (guess: Double, x: Double): Double = - if (isGoodEnough(guess, x)) guess - else sqrtIter(improve(guess, x), x) + def improve(guess: Double, x: Double) = (guess + x/ guess) / 2 - def improve(guess: Double, x: Double) = (guess + x/ guess) / 2 + def isGoodEnough(guess: Double, x: Double) = math.abs(guess * guess - x) < 0.001 - def isGoodEnough(guess: Double, x: Double) = math.abs(guess * guess - x) < 0.001 - - sqrt(4).round shouldBe 2 - sqrt(36).round shouldBe 6 + def main(args: Array[String]): Unit = { + println(sqrt(36)) + println(sqrt(255)) } - } \ No newline at end of file diff --git a/Functional Loops/Computing the Square Root of a Value/task-info.yaml b/Functional Loops/Computing the Square Root of a Value/task-info.yaml index c8ea26e..13468e7 100644 --- a/Functional Loops/Computing the Square Root of a Value/task-info.yaml +++ b/Functional Loops/Computing the Square Root of a Value/task-info.yaml @@ -5,11 +5,12 @@ files: - name: src/ComputingSquareRoot.scala visible: true placeholders: - - offset: 266 + - offset: 155 length: 5 - placeholder_text: //TODO - - offset: 359 + placeholder_text: //you can just return the guess here + - offset: 244 length: 22 - placeholder_text: //TODO + placeholder_text: //here we need to put an expression for evaluating a better + guess - name: test/TestSpec.scala visible: false diff --git a/Functional Loops/Computing the Square Root of a Value/test/TestSpec.scala b/Functional Loops/Computing the Square Root of a Value/test/TestSpec.scala index 9ad0214..475874b 100644 --- a/Functional Loops/Computing the Square Root of a Value/test/TestSpec.scala +++ b/Functional Loops/Computing the Square Root of a Value/test/TestSpec.scala @@ -1,9 +1,12 @@ -import org.scalatest.Spec +import ComputingSquareRoot.sqrt +import org.scalatest.Matchers +import org.scalatest.refspec.RefSpec -class Test extends Spec { +class Test extends RefSpec with Matchers{ def `check factorial`(): Unit = { - ComputingSquareRoot.sqrtExample() + sqrt(4).round shouldBe 2 + sqrt(36).round shouldBe 6 } }