Skip to content

Commit 186b426

Browse files
committed
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
1 parent b31c9aa commit 186b426

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import org.scalatest.{FlatSpec, Matchers}
1+
object ComputingSquareRoot{
22

3-
object ComputingSquareRoot extends FlatSpec with Matchers {
3+
def sqrt(x: Double) = sqrtIter(1.0, x)
44

5-
def sqrtExample(): Unit = {
6-
def sqrt(x: Double) = sqrtIter(1.0, x)
5+
def sqrtIter (guess: Double, x: Double): Double =
6+
if (isGoodEnough(guess, x)) guess
7+
else sqrtIter(improve(guess, x), x)
78

8-
def sqrtIter (guess: Double, x: Double): Double =
9-
if (isGoodEnough(guess, x)) guess
10-
else sqrtIter(improve(guess, x), x)
9+
def improve(guess: Double, x: Double) = (guess + x/ guess) / 2
1110

12-
def improve(guess: Double, x: Double) = (guess + x/ guess) / 2
11+
def isGoodEnough(guess: Double, x: Double) = math.abs(guess * guess - x) < 0.001
1312

14-
def isGoodEnough(guess: Double, x: Double) = math.abs(guess * guess - x) < 0.001
15-
16-
sqrt(4).round shouldBe 2
17-
sqrt(36).round shouldBe 6
13+
def main(args: Array[String]): Unit = {
14+
println(sqrt(36))
15+
println(sqrt(255))
1816
}
19-
2017
}

Functional Loops/Computing the Square Root of a Value/task-info.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ files:
55
- name: src/ComputingSquareRoot.scala
66
visible: true
77
placeholders:
8-
- offset: 266
8+
- offset: 155
99
length: 5
10-
placeholder_text: //TODO
11-
- offset: 359
10+
placeholder_text: //you can just return the guess here
11+
- offset: 244
1212
length: 22
13-
placeholder_text: //TODO
13+
placeholder_text: //here we need to put an expression for evaluating a better
14+
guess
1415
- name: test/TestSpec.scala
1516
visible: false
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import org.scalatest.Spec
1+
import ComputingSquareRoot.sqrt
2+
import org.scalatest.Matchers
3+
import org.scalatest.refspec.RefSpec
24

3-
class Test extends Spec {
5+
class Test extends RefSpec with Matchers{
46

57
def `check factorial`(): Unit = {
6-
ComputingSquareRoot.sqrtExample()
8+
sqrt(4).round shouldBe 2
9+
sqrt(36).round shouldBe 6
710
}
811

912
}

0 commit comments

Comments
 (0)