summaryrefslogtreecommitdiffstats
path: root/Scala/example
diff options
context:
space:
mode:
Diffstat (limited to 'Scala/example')
-rw-r--r--Scala/example/src/main/scala/example/Lists.scala15
-rw-r--r--Scala/example/src/test/scala/example/ListsSuite.scala46
2 files changed, 53 insertions, 8 deletions
diff --git a/Scala/example/src/main/scala/example/Lists.scala b/Scala/example/src/main/scala/example/Lists.scala
index 4e5feee..628486c 100644
--- a/Scala/example/src/main/scala/example/Lists.scala
+++ b/Scala/example/src/main/scala/example/Lists.scala
@@ -23,7 +23,11 @@ object Lists {
* @param xs A list of natural numbers
* @return The sum of all elements in `xs`
*/
- def sum(xs: List[Int]): Int = ???
+ def sum(xs: List[Int]): Int = {
+ def sumIter(acc: Int, xs: List[Int]): Int =
+ if (xs.isEmpty) acc else sumIter(acc + xs.head, xs.tail)
+ sumIter(0, xs)
+ }
/**
* This method returns the largest element in a list of integers. If the
@@ -38,5 +42,12 @@ object Lists {
* @return The largest element in `xs`
* @throws java.util.NoSuchElementException if `xs` is an empty list
*/
- def max(xs: List[Int]): Int = ???
+ def max(xs: List[Int]): Int = {
+ def maxInt(a: Int, b: Int) = if (a>b) a else b
+ def maxIter(max: Int, xs: List[Int]): Int =
+ if (xs.isEmpty) max else maxIter(maxInt(max,xs.head), xs.tail)
+ if (xs.isEmpty) throw new NoSuchElementException
+ else maxIter(Integer.MIN_VALUE, xs)
+ }
+
}
diff --git a/Scala/example/src/test/scala/example/ListsSuite.scala b/Scala/example/src/test/scala/example/ListsSuite.scala
index 4a52667..5973369 100644
--- a/Scala/example/src/test/scala/example/ListsSuite.scala
+++ b/Scala/example/src/test/scala/example/ListsSuite.scala
@@ -19,7 +19,7 @@ import org.scalatest.junit.JUnitRunner
* inside eclipse using the built-in JUnit test runner.
*
* You have two options for running this test suite:
- *
+ *
* - Start the sbt console and run the "test" command
* - Right-click this file in eclipse and chose "Run As" - "JUnit Test"
*/
@@ -47,7 +47,7 @@ class ListsSuite extends FunSuite {
* This allows tests to be written in a more readable manner:
*/
test("one plus one is three?") {
- assert(1 + 1 == 3) // This assertion fails! Go ahead and fix it.
+ assert(1 + 1 == 2) // This assertion fails! Go ahead and fix it.
}
@@ -72,7 +72,7 @@ class ListsSuite extends FunSuite {
* We recommend to always use the `===` equality operator when writing tests.
*/
test("details why one plus one is not three") {
- assert(1 + 1 === 3) // Fix me, please!
+ assert(1 + 1 === 2) // Fix me, please!
}
@@ -99,9 +99,9 @@ class ListsSuite extends FunSuite {
* Now we finally write some tests for the list functions that have to be
* implemented for this assignment. We fist import all members of the
* `List` object.
- */
+ */
import Lists._
-
+
/**
* We only provide two very basic tests for you. Write more tests to make
@@ -114,11 +114,45 @@ class ListsSuite extends FunSuite {
* however it is recommended to write an individual `test` statement for
* every tested aspect of a method.
*/
+ test("sum of a 1 number list") {
+ assert(sum(List(666)) === 666)
+ }
+
test("sum of a few numbers") {
assert(sum(List(1,2,0)) === 3)
}
-
+
+ test("sum of an empty list") {
+ assert(sum(List()) === 0)
+ }
+
+ test("sum of a same numbers") {
+ assert(sum(List(2,2,2,2,2)) === 10)
+ }
+
+ test("sum of a few positives and negatives numbers") {
+ assert(sum(List(1,2,0,-3,-8)) === -8)
+ }
+
+ test("max of a 1 number list") {
+ assert(max(List(666)) === 666)
+ }
+
+ test("max of an empty list throws NoSuchElementException") {
+ intercept[NoSuchElementException] {
+ max(List())
+ }
+ }
+
test("max of a few numbers") {
assert(max(List(3, 7, 2)) === 7)
}
+
+ test("max of negatives numbers") {
+ assert(max(List(-3, -7, -2, -5)) === -2)
+ }
+
+ test("max of a few positives and negatives numbers") {
+ assert(max(List(3, 7, 0, -5)) === 7)
+ }
}