-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Following up Issue #16. Thanks @etorreborre!
- Loading branch information
Showing
2 changed files
with
24 additions
and
7 deletions.
There are no files selected for viewing
12 changes: 9 additions & 3 deletions
12
exercises/src/main/scala/pl/japila/scalania/euler/Euler_P01.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package pl.japila.scalania.euler | ||
|
||
object Euler_P01 { | ||
|
||
val solutions = List[(Int, Int, Int) => Int]( | ||
findSumOfMultiplies, | ||
findSumOfMultiplies_jl | ||
findSumOfMultiplies_jl, | ||
findSumOfMultiplies_eric | ||
) | ||
|
||
def findSumOfMultiplies(a: Int = 3, b: Int = 5, limit: Int = 1000): Int = ??? | ||
def findSumOfMultiplies: (Int, Int, Int) => Int = (a: Int, b: Int, limit: Int) => ??? | ||
|
||
def findSumOfMultiplies_jl: (Int, Int, Int) => Int = (a: Int, b: Int, limit: Int) => ??? | ||
|
||
def findSumOfMultiplies_eric = (a: Int, b: Int, limit: Int) => | ||
(0 until limit).filter(n => n % a == 0 || n % b == 0).sum | ||
|
||
def findSumOfMultiplies_jl(a: Int = 3, b: Int = 5, limit: Int = 1000): Int = ??? | ||
} |
19 changes: 15 additions & 4 deletions
19
exercises/src/test/scala/pl/japila/scalania/euler/P01Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
package pl.japila.scalania.euler | ||
|
||
import org.specs2.mutable._ | ||
import Euler_P01.solutions | ||
import org.specs2.specification.Tables | ||
import Euler_P01._ | ||
|
||
class P01Spec extends Specification { | ||
class P01Spec extends Specification with Tables { | ||
"P01 solution" should { | ||
"Find the sum of all the multiples of 3 or 5 below 1000." in { | ||
solutions.forall { f => f(3, 5, 1000) === 233168 } | ||
"Find the sum of all the multiples of 3 or 5 below 1000." >> { | ||
|
||
val titles = List("name", "function") | ||
val dr2s = List[DataRow2[String, (Int, Int, Int) => Int]]() | ||
val rows = solutions.foldLeft(dr2s) { (l, f) => | ||
DataRow2(s"${f.getClass.getSimpleName} solution", f) :: l | ||
} | ||
|
||
val t2 = new Table2[String, (Int, Int, Int) => Int](titles, rows.reverse) | ||
t2.rows.foreach { row => | ||
row.t1 >> { row.t2(3, 5, 1000) must_== 233168 } | ||
}; br | ||
} | ||
} | ||
} |