I have always heard about FizzBuzz but was not really sure what it was. After some research it seems that FizzBuzz is pretty common interview question for Software Developers even though it is a children’s game.
The question goes like this:
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz.
I am in Dev/Ops week in the AWS loft NY (2/22/2017) and I challenged myself to see in how many programing languages I was able to solve FizzBuzz before the first speaker finish. BTW he was talking about CHEF and AWS Opsworks and I am pretty familiar with those two technologies so no loss there.
So, it took me a little less than 30 minutes to write the solution in Go, Python and PowerShell. These are the solutions
GO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "math" | |
import "time" | |
func main() { | |
for i := 1.0; i < 101; i++ { | |
switch { | |
case math.Mod(i,3) == 0 && math.Mod(i,5) == 0: | |
fmt.Println("Fizz Buzz") | |
case math.Mod(i,3) == 0: | |
fmt.Println("Fizz") | |
case math.Mod(i,5) == 0: | |
fmt.Println("Buzz") | |
default: | |
fmt.Println(i) | |
} | |
} | |
} |
Python3
for number in range(1,101): if number % 3 == 0 and number % 5 == 0: print('FizzBuzz') elif number % 3 == 0: print('Fizz') elif number % 5 ==0: print('Buzz') else: print(number)
PowerShell
ForEach ($number in (1..100)) { if($number % 3 -eq 0 -and $number % 5 -eq 0){ Write-Output('FizzBuzz') } elseif ($number % 3 -eq 0) { Write-Output('Fizz') } elseif ($number % 5 -eq 0){ Write-Output('Buzz') } else{ Write-Output($number) } }
Finally, if you are reading this now you can see how to solve FizzBuzz in different languages and try to solve on you prefer language. The key to solve the problem is by using the modulus operator
Wow you’ve been programming a lot since we first met! Kudos to you sir glad to see you’re liking it, keep up the good work.
LikeLike
Thanks how is life
LikeLike