A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.
This article is part of the series “Scala Tutorial Through Katas”. Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.
For the complete list of Scala katas and solutions please visit the index page
The article assumes that the reader is familiar with the basic usage of ScalaTest and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).
Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.
One possible solution is provided below the tests. Try to solve the kata by yourself first.
Fizz Buzz Kata
Return “fizz”, “buzz” or “fizzbuzz”.
For a given natural number greater than zero return:
- “fizz” if the number is dividable by 3
- “buzz” if the number is dividable by 5
- “fizzbuzz” if the number is dividable by 15
- the same number if no other requirement is fulfilled
[TESTS]
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") FizzBuzz.getResult(10) should be ("buzz") } it should "return fizzbuzz if the number is dividable by 15" in { FizzBuzz.getResult(15) should be ("fizzbuzz") FizzBuzz.getResult(30) should be ("fizzbuzz") } it should "return the same number if no other requirement is fulfilled" in { FizzBuzz.getResult(1) should be ("1") FizzBuzz.getResult(2) should be ("2") FizzBuzz.getResult(4) should be ("4") } }
Test code can be found in the GitHub FizzBuzz.scala.
[ONE POSSIBLE SOLUTION]
object FizzBuzz { def getResult(number: Int) = { var result = "" if (number % 3 == 0) result = "fizz" if (number % 5 == 0) result += "buzz" if (result == "") number.toString else result } }
Better solution was provided by rayyildiz.
[BETTER POSSIBLE SOLUTION]
object FizzBuzz { def getResult(number: Int): String = (number % 3, number % 5) match { case (0 , 0) => "fizzbuzz" case (0 , _) => "fizz" case (_ , 0) => "buzz" case _ => number.toString() } }
The solution code can be found in FizzBuzz.scala solution or in rayyildiz’s repository.
What was your solution? Post it as a comment so that we can compare different ways to solve this kata.
Just a quick note. The inferred return type of the solution posted is
Any
instead ofString
. Integer should be converted to string (number.toString()
) to infer the right return type.You’re right. Return type is indeed Any. I modified the post.
You can use pattent matching such as:
def matchTest(i:Int): String = (i % 3, i % 5) match {
case (0 , 0) => “FizzBuzz”
case (0 , ) => “Fizz”
case ( , 0) => “Buzz”
case _ => i.toString()
}
Here is the code: https://github.com/rayyildiz/codekata-scala/blob/master/FizzBuzz/src/main/scala/org/scalatr/FizzBuzz.scala
Thanks rayyildiz.
Your solution is indeed better. I was trying to do it using minimum Scala knowledge as an easy exercize for those who never worked with Scala before. On the other hand, your solution shows the beauty of Scala.
def getResult(n: Int)=
if (n%15==0) “fizzbuzz”
else if (n%3==0) “fizz”
else if (n%5==0) “buzz”
else n.toString
Purely functional solution using Scalaz:
https://github.com/electricmonk/scala-katas/blob/master/src/main/scala/com/shaiyallin/FizzBuzz.scala
scala> (1 to 100) map { x => x match {
| case x if (x % 3 == 0 && x % 5 == 0) => “fizzbuzz”
| case x if (x % 5 == 0) => “buzz”
| case x if (x % 3 == 0) => “fizz”
| case _ => x.toString
| }}
I am happy to solve it without pattern matching at all 😉
object FizzBuzz extends App{
def getResult (z: Int) = {
List( (“fizzbuzz”, 15),(“fizz”, 3), (“buzz”, 5)).find(z % _._2 == 0).map {case (a,b) => a}.getOrElse(z).toString()
}
}
How about using options
@Taorm great solution, but isn’t. “case (a,b) ” a pattern match? Not that it’s a bad thing, pattern matching is awesome