diff --git a/scala/.gitignore b/scala/.gitignore new file mode 100644 index 0000000..96035b7 --- /dev/null +++ b/scala/.gitignore @@ -0,0 +1,13 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen + +target + +project/project/target \ No newline at end of file diff --git a/scala/README.md b/scala/README.md new file mode 100644 index 0000000..db2d91b --- /dev/null +++ b/scala/README.md @@ -0,0 +1,3 @@ +# kata-fizz-buzz + +http://codingdojo.org/cgi-bin/index.pl?KataFizzBuzz diff --git a/scala/build.sbt b/scala/build.sbt new file mode 100644 index 0000000..af10484 --- /dev/null +++ b/scala/build.sbt @@ -0,0 +1,7 @@ +name := "kata-fizz-buzz" + +version := "1.0" + +scalaVersion := "2.11.6" + +libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test" \ No newline at end of file diff --git a/scala/project/build.properties b/scala/project/build.properties new file mode 100644 index 0000000..5728c48 --- /dev/null +++ b/scala/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.7 \ No newline at end of file diff --git a/scala/project/plugins.sbt b/scala/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/scala/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/scala/src/main/scala/FizzBuzz.scala b/scala/src/main/scala/FizzBuzz.scala new file mode 100644 index 0000000..69548da --- /dev/null +++ b/scala/src/main/scala/FizzBuzz.scala @@ -0,0 +1,13 @@ +/** + * Created by arincon on 4/03/15. + */ +object FizzBuzz { + def getResult(i: Int): String ={ + var t="" + if (i%3==0) t += "fizz" + if (i%5==0) t.+=("buzz") + if (t=="") t=i.toString + t + } + +} diff --git a/scala/src/main/scala/FizzBuzz2.scala b/scala/src/main/scala/FizzBuzz2.scala new file mode 100644 index 0000000..3e1629b --- /dev/null +++ b/scala/src/main/scala/FizzBuzz2.scala @@ -0,0 +1,11 @@ +/** + * Created by arincon on 4/03/15. + */ +object FizzBuzz2 { + def getResult(number: Int): String = (number % 3, number % 5) match { + case (0 , 0) => "fizzbuzz" + case (0 , _) => "fizz" + case (_ , 0) => "buzz" + case _ => number.toString() + } +} diff --git a/scala/src/test/scala/FizzBuzz2Spec.scala b/scala/src/test/scala/FizzBuzz2Spec.scala new file mode 100644 index 0000000..250d661 --- /dev/null +++ b/scala/src/test/scala/FizzBuzz2Spec.scala @@ -0,0 +1,33 @@ +import org.scalatest.FlatSpec +import org.scalatest.Matchers + +class FizzBuzz2Spec extends FlatSpec with Matchers { + + val dictionary: Map[Int, String] = Map(3 -> "fizz", 5 -> "buzz", 7 -> "wizz") + + def processor(i: Int) = { + lazy val toReturn = dictionary.keys.flatMap(module(i, _)) + if (toReturn.isEmpty) i + else toReturn.mkString("") + } + + private def module(i: Int, divisor: Int): Option[String] = { + if (i % divisor == 0) dictionary.get(divisor) else None + } + + "processor" should "return 1 if input is 1" in { + processor(1) should be(1) + } + it should "return fizz if input is 6" in { + processor(6) should be("fizz") + } + it should "return buzz if input is 10" in { + processor(10) should be("buzz") + } + it should "return fizzbuzz if input is 15" in { + processor(15) should be("fizzbuzz") + } + it should "return wizz if input is 7" in { + processor(7) should be("wizz") + } +} diff --git a/scala/src/test/scala/FizzBuzzSpec.scala b/scala/src/test/scala/FizzBuzzSpec.scala new file mode 100644 index 0000000..75bf41f --- /dev/null +++ b/scala/src/test/scala/FizzBuzzSpec.scala @@ -0,0 +1,35 @@ +import org.scalatest.FlatSpec +import org.scalatest.Matchers + +class FizzBuzzSpec extends FlatSpec with Matchers { + + def module(i: Int, d: Int): Boolean = i % d == 0 + + def FizzBuzz(input:String)={ + input.toInt match{ + case x if module(x, 15) => "fizzbuzz" + case x if module(x, 3) => "fizz" + case x if module(x, 5) => "buzz" + case _ => input + } + } + + "FizzBuzz" should "return 1 if input is 1" in { + FizzBuzz("1") should be("1") + } + it should "return 2 if input is 2" in { + FizzBuzz("2") should be("2") + } + it should "return fizz if input is 3" in { + FizzBuzz("3") should be("fizz") + } + it should "return fizz if input is 4" in { + FizzBuzz("4") should be("4") + } + it should "return buzz if input is 5" in { + FizzBuzz("5") should be("buzz") + } + it should "return fizzbuzz if input is 15" in { + FizzBuzz("15") should be("fizzbuzz") + } +} diff --git a/scala/src/test/scala/UnitSpec.scala b/scala/src/test/scala/UnitSpec.scala new file mode 100644 index 0000000..525ce9f --- /dev/null +++ b/scala/src/test/scala/UnitSpec.scala @@ -0,0 +1,7 @@ +/** + * Created by arincon on 4/03/15. + */ +import org.scalatest._ + +abstract class UnitSpec extends FlatSpec with Matchers with +OptionValues with Inside with Inspectors diff --git a/scala/src/test/scala/test.scala b/scala/src/test/scala/test.scala new file mode 100644 index 0000000..4894516 --- /dev/null +++ b/scala/src/test/scala/test.scala @@ -0,0 +1,32 @@ +class FizzBuzzTest extends UnitSpec { + + "FizzBuzz" should "return fizz if the number is dividable by 3" in { + FizzBuzz.getResult(3) should be("fizz") + FizzBuzz.getResult(6) should be("fizz") + } + it should "return buzz if the number is dividable by 5" in { + FizzBuzz.getResult(5) should be("buzz") + } + it should "return fizzbuzz if the number is dividable by 15" in { + FizzBuzz.getResult(15) should be ("fizzbuzz") + } + it should "return number if input number is not dividable by 3, 5 or 15" in { + FizzBuzz.getResult(1) should be ("1") + } + + + + "FizzBuzz2" should "return fizz if the number is dividable by 3" in { + FizzBuzz2.getResult(3) should be("fizz") + FizzBuzz2.getResult(6) should be("fizz") + } + it should "return buzz if the number is dividable by 5" in { + FizzBuzz2.getResult(5) should be("buzz") + } + it should "return fizzbuzz if the number is dividable by 15" in { + FizzBuzz2.getResult(15) should be ("fizzbuzz") + } + it should "return number if input number is not dividable by 3, 5 or 15" in { + FizzBuzz2.getResult(1) should be ("1") + } +} \ No newline at end of file