Skip to content

Commit 35bc4f8

Browse files
authored
Merge pull request #20 from jetbrains-academy/igor/conditional-loops-tests-rework
Igor/conditional loops tests rework
2 parents 9179526 + 186b426 commit 35bc4f8

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
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
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import org.scalatest.{FlatSpec, Matchers}
22

33
object FunctionalLoops extends FlatSpec with Matchers {
4+
def factorial(n: Int): Int =
5+
if (n == 0) 1
6+
else factorial(n - 1) * n
47

5-
def factorialExercise(): Unit = {
6-
def factorial(n: Int): Int =
7-
if (n == 0) 1
8-
else factorial(n - 1) * n
9-
10-
factorial(3) shouldBe 6
11-
factorial(4) shouldBe 24
8+
def main(args: Array[String]): Unit = {
9+
println(factorial(5))
1210
}
1311
}

Functional Loops/Conditional Expressions/task-info.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ files:
33
- name: src/FunctionalLoops.scala
44
visible: true
55
placeholders:
6-
- offset: 179
6+
- offset: 135
77
length: 6
8-
placeholder_text: // TODO
9-
- offset: 200
8+
placeholder_text: /*insert the corner case condition for the factorial evaluation*/
9+
- offset: 152
1010
length: 20
11-
placeholder_text: // TODO
11+
placeholder_text: //insert the common case factorial expression here
1212
- name: test/Test.scala
1313
visible: false
1414
- name: build.sbt
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import org.scalatest.Spec
1+
import FunctionalLoops.factorial
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-
FunctionalLoops.factorialExercise()
8+
factorial(0) shouldBe 1
9+
factorial(3) shouldBe 6
10+
factorial(4) shouldBe 24
11+
factorial(10) shouldBe 3628800
712
}
813

914
}

Functional Loops/lesson-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
content:
22
- Conditional Expressions
3+
- Computing the Square Root of a Value

0 commit comments

Comments
 (0)