Scala Tutorial Through Katas: Fizz Buzz (Easy)

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]

Test code can be found in the GitHub FizzBuzz.scala.

[ONE POSSIBLE SOLUTION]

Better solution was provided by rayyildiz.

[BETTER POSSIBLE SOLUTION]

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.

10 thoughts on “Scala Tutorial Through Katas: Fizz Buzz (Easy)

  1. Nishruu

    Just a quick note. The inferred return type of the solution posted is Any instead of String. Integer should be converted to string (number.toString()) to infer the right return type.

    Reply
    1. Viktor Farcic

      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.

      Reply
  2. Ali

    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
    | }}

    Reply
  3. taorm

    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()
    }
    }

    Reply
  4. scalony

    How about using options

    [code lang=scala]
    def getResult(number:Int):String = {
    val fb = (i:Int, s:String) => {if (number%i==0) Some(s) else None}:Option[String]
    fb(15,"fizzbuzz") orElse fb(3,"fizz") orElse fb(5,"buzz") getOrElse(s"$number")
    }
    [/code]

    Reply

Leave a Reply