Skip to content

Commit

Permalink
Add armstrong-numbers exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ricemery committed Jun 5, 2018
1 parent 7d13ba5 commit 656637b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@
"Mathematics"
]
},
{
"uuid": "2bd9ab9e-ecc4-4331-984d-8e5f15f775b4",
"slug": "armstrong-numbers",
"core": false,
"unlocked_by": "leap",
"difficulty": 2,
"topics": [
"Integers",
"Mathematics"
]
},
{
"uuid": "5a31217f-02fd-4be4-b64f-7a93f36f5140",
"slug": "hamming",
Expand Down
3 changes: 3 additions & 0 deletions exercises/armstrong-numbers/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scalaVersion := "2.12.1"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"
7 changes: 7 additions & 0 deletions exercises/armstrong-numbers/example.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object ArmstrongNumbers {
def isArmstrongNumber(i: Int): Boolean = {
val s = i.toString
val power = s.length
i == s.toStream.foldLeft(0.0)((acc, c) => acc + math.pow(c.asDigit, power))
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
class ArmstrongNumbersTest extends FunSuite with Matchers {

test("Single digit numbers are Armstrong numbers") {
ArmstrongNumbers.isArmstrongNumber(5) should be (true)
}

test("There are no 2 digit Armstrong numbers") {
pending
ArmstrongNumbers.isArmstrongNumber(10) should be (false)
}

test("Three digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(153) should be (true)
}

test("Three digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(100) should be (false)
}

test("Four digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9474) should be (true)
}

test("Four digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9475) should be (false)
}

test("Seven digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9926315) should be (true)
}

test("Seven digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9926314) should be (false)
}
}
33 changes: 33 additions & 0 deletions testgen/src/main/scala/ArmstrongNumbersTestGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.File

import testgen.TestSuiteBuilder._
import testgen._

object ArmstrongNumbersTestGenerator {
def main(args: Array[String]): Unit = {
val file = new File("src/main/resources/armstrong-numbers.json")

def toString(expected: CanonicalDataParser.Expected): String = {
expected match {
case Right(b: Boolean) => b.toString
case _ => throw new IllegalStateException()
}
}

def fromLabeledTestFromInput(argNames: String*): ToTestCaseData =
withLabeledTest { sut =>
labeledTest =>
val args = sutArgsFromInput(labeledTest.result, argNames: _*)
val property = labeledTest.property
val sutCall =
s"""$sut.$property($args)"""
val expected = toString(labeledTest.expected)
TestCaseData(labeledTest.description, sutCall, expected)
}

val code = TestSuiteBuilder.build(file, fromLabeledTestFromInput("number"))
println(s"-------------")
println(code)
println(s"-------------")
}
}

0 comments on commit 656637b

Please sign in to comment.