Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions scala/.gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions scala/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# kata-fizz-buzz

http://codingdojo.org/cgi-bin/index.pl?KataFizzBuzz
7 changes: 7 additions & 0 deletions scala/build.sbt
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions scala/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 0.13.7
1 change: 1 addition & 0 deletions scala/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logLevel := Level.Warn
13 changes: 13 additions & 0 deletions scala/src/main/scala/FizzBuzz.scala
Original file line number Diff line number Diff line change
@@ -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
}

}
11 changes: 11 additions & 0 deletions scala/src/main/scala/FizzBuzz2.scala
Original file line number Diff line number Diff line change
@@ -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()
}
}
33 changes: 33 additions & 0 deletions scala/src/test/scala/FizzBuzz2Spec.scala
Original file line number Diff line number Diff line change
@@ -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")
}
}
35 changes: 35 additions & 0 deletions scala/src/test/scala/FizzBuzzSpec.scala
Original file line number Diff line number Diff line change
@@ -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")
}
}
7 changes: 7 additions & 0 deletions scala/src/test/scala/UnitSpec.scala
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions scala/src/test/scala/test.scala
Original file line number Diff line number Diff line change
@@ -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")
}
}