DEV Community

Cover image for Get Your (Fizz)Buzz On

Get Your (Fizz)Buzz On

Yechiel Kalmenson on October 02, 2017

FizzBuzz In 3 Acts Learning to code was full of learning moments. One that I look back upon fondly was when I did the Flatiron lesson on...
Collapse
 
stanleynguyen profile image
Stanley Nguyen

Another one of the scalability aspects that we can explore is that what if the game wont stop at just 2 denominators (e.g. If I add 7 to the game, so for numbers multiple of 3 print Fizz, multiple of 5 print Buzz, multiple of 15 (3 and 5) print FizzBuzz, multiple of 35 (5 and 7) print BuzzBazz, multiple of 21 (3 and 7) print FizzBazz, and multiple of 105 (3 and 5 and 7) print FizzBuzzBazz). How we would go about scale it up on this aspect is to make use of hashmap, iterating through the map to determine the output. Here is the implementation in Go:

package main

import (
    "fmt"
    "sort"
    "strconv"
)

func scalableFizzBuzz(num int, denominatorsMap map[int]string) {
    // make a list of keys and sort it in order from small -> large
    // this is to make sure that it prints out "FizzBuzzBazz"
    // instead of random order like "BuzzFizzBazz" due to the
    // random nature of map iterator
    keys := make([]int, 0)
    for k := range denominatorsMap {
        keys = append(keys, k)
    }
    sort.Ints(keys)

    for i := 1; i <= num; i++ {
        output := ""
        for _, k := range keys {
            if i%k == 0 {
                output += denominatorsMap[k]
            }
        }
        if output == "" {
            output += strconv.Itoa(i)
        }
        output += ", "
        fmt.Print(output)
    }
}

func main() {
    scalableFizzBuzz(300, map[int]string{3: "Fizz", 5: "Buzz", 7: "Bazz"})
}
Collapse
 
stanleynguyen profile image
Stanley Nguyen

Here is the solution in js

function scalableFizzBuzz(num, denominatorsMap) {
  for (let i = 1; i <= num; i++) {
    output = "";
    Object.keys(denominatorsMap).forEach((k) => {
      if (i%k === 0) {
        output += denominatorsMap[k];
      }
    });
    if (output === "") {
      output += i;
    }
    console.log(output);
  }
}

scalableFizzBuzz(300, { 3: "Fizz", 5: "Buzz", 7: "Bazz" });
Collapse
 
yechielk profile image
Yechiel Kalmenson • Edited

Nice, I never thought of scaling it in that direction.

I like the idea of using a Hash.

Collapse
 
danieljsummers profile image
Daniel J. Summers • Edited

From what I gleaned from people who had used this test "in the wild", the single most neglected step was "or else, print the number." Pretenders (or not-paying-attention-to-details developers) would get so wrapped up in all of the exceptions that they'd forget the "else" condition at the end.

Just for fun, here's one you probably didn't expect... COBOL 85 or after. (imagine column 1 starts at column 8 if your compiler cares about that...)

edit: Holy cow, this code formatter knows COBOL! Disregard the note above...

       identification division.
         program-id. fizzbuzz.
       environment division.
       data division.
         working-storage section.
       77  nbr pic 9(3) value zero.
       77  quotient pic 9(3) value zero.
       77  rem pic 9(2) value zero.
       procedure division.
       begin.
           perform varying nbr from 1 by 1 until nbr > 100
               divide nbr by 15 giving quotient remainder rem
               evaluate rem
                   when 0
                       display "FizzBuzz"
                   when 3
                   when 6
                   when 9
                   when 12
                       display "Fizz"
                   when 5
                   when 10
                       display "Buzz"
                   when other
                       display nbr
               end-evaluate
           end-perform.
           stop run.

This outputs 002 for 2, but I'm not posting a completely correct FizzBuzz solution on the public Internet. Interviewers don't want memorizers, they want programmers. :)

Collapse
 
yechielk profile image
Yechiel Kalmenson

Oh wow! I was NOT expecting a COBOL solution ;)

Guess that last part is left as an exercise for the reader (from my understanding, COBOL programmers don't have silly riddles in their interviews these days... :)

Collapse
 
danieljsummers profile image
Daniel J. Summers • Edited

I'm less than 4 years from the end of my current employment, and need to decide what to do next. Helping prop up aging COBOL is on my list to consider. :)

To bring it into this century, here's an F# version...

let words =
  function
  | _ when 0 = nbr % 3 && 0 = nbr % 5 -> "FizzBuzz"
  | _ when 0 = nbr % 3 -> "Fizz"
  | _ when 0 = nbr % 5 -> "Buzz"
  | x -> string x
let fizzBuzz i =
  [1 .. i]
  |> List.map words
  |> String.concat ", "
fizzBuzz 100
|> System.Console.WriteLine

...although, technically, this separates them by commas rather than listing one per line.