Questions tagged [fizzbuzz]

a game, algorithm and a common programming task of replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz"

Fizzbuzz is a game used to teach children division. Implementing an algorithm for that game is sometimes used as an interview task.

https://en.wikipedia.org/wiki/Fizz_buzz

290 questions
25
votes
11 answers

How to code Fizzbuzz in F#

I am currently learning F# and have tried (an extremely) simple example of FizzBuzz. This is my initial attempt: for x in 1..100 do if x % 3 = 0 && x % 5 = 0 then printfn "FizzBuzz" elif x % 3 = 0 then printfn "Fizz" elif x % 5 = 0…
Russell
  • 17,481
  • 23
  • 81
  • 125
25
votes
9 answers

How to reduce if statements

The program below functions as necessary but how do I reduce the amount of if statements. I have been told that if your function contains 2 or more if statements then your doing it wrong. Any suggestions? I've tried using switch statements but that…
Calgar99
  • 1,670
  • 5
  • 26
  • 42
25
votes
47 answers

Writing FizzBuzz

Reading the coding horror, I just came across the FizzBuzz another time. The original post is here: Coding Horror: Why Can't Programmers.. Program? For those who do not know: FizzBuzz is a quite popular children's game. Counting from 1 to 100, and…
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
14
votes
9 answers

From 1 to 100, print "ping" if multiple of 3, "pong" if multiple of 5, or else print the number

I just came home from a job interview, and the interviewer asked me to write a program: It should, count from 1 to 100, and print... If it was multiple of 3, "ping" If it was multiple of 5, "pong" Else, print the number. If it was multiple of 3 AND…
BernaMariano
  • 846
  • 2
  • 9
  • 27
12
votes
4 answers

Swift 2: expression pattern of type 'Bool' cannot match values of type 'Int'

I'm doing this problem set "FizzBuzz", and my switch statement is giving me some problems, here's my code: func fizzBuzz(n: Int) -> String { switch n { case n % 3 == 0: print("Fizz") case n % 5 == 0: print("Buzz") case n % 15 ==…
user3724487
  • 123
  • 1
  • 6
11
votes
1 answer

Haskell -- problem with pretty-printing a list

I'm new to haskell, and i read through and digested Learn You A Haskell For Great Good, trying out a couple of things along the way. For my first project i wanted to try the classic: FizzBuzz. So i came up with the following code: import…
RCIX
  • 38,647
  • 50
  • 150
  • 207
10
votes
1 answer

Why is this Fizz Buzz generator significantly faster than this Fizz Buzz Iterator class?

After learning about iterator class methods and generators, I tested the performance characteristics of simple Fizz Buzz solutions utilizing each idiom: >>> from timeit import timeit >>> timeit('tuple(fizzbuzz.FizzBuzzIterator(10))', 'import…
Winny
  • 1,189
  • 1
  • 16
  • 29
9
votes
11 answers

C programming. The FizzBuzz program

I had a quiz and I wrote this code: Print Fizz if it is divisible by 3 and it prints Buzz if it is divisible by 5. It prints FizzBuss if it is divisible by both. Otherwise, it will print the numbers between 1 and 100. But after I arrived home, I…
leocod
  • 173
  • 1
  • 2
  • 7
9
votes
5 answers

What makes an application or a software development process "Enterprise"?

After reading Wolfbyte's answer on Enterprise FizzBuzz I have thought about what constitutes a program as "Enterprise". What makes an application or a software development process as Enterprise? EDIT: It seems like there is a lot of negativity…
dance2die
  • 35,807
  • 39
  • 131
  • 194
9
votes
8 answers

javascript fizzbuzz switch statement

I'm currently taking the code academy course on Javascript and I'm stuck on the FizzBuzz task. I need to count from 1-20 and if the number is divisible by 3 print fizz, by 5 print buzz, by both print fizzbuzz, else just print the number. I was able…
user3133810
8
votes
3 answers

Ways to improve my FizzBuzz solution for a TDD role?

I had an interview recently where I was asked to produce the traditional FizzBuzz solution: Output a list of numbers from 1 to 100. For all multiples of 3 and 5, the number is replaced with "FizzBuzz" For all remaining multiples of 3, the number…
seanhodges
  • 17,426
  • 15
  • 71
  • 93
8
votes
6 answers

FizzBuzz using ternary conditional operator

I've been reading up on conditional-style expressions in ruby. However I came across one I couldn't quite understand to define the classic FizzBuzz problem. I understand the FizzBuzz problem and even wrote my own before finding the following quick…
Damian
  • 663
  • 1
  • 9
  • 12
8
votes
9 answers

Why isn't my FizzBuzz code processing both if statements when they both match?

For those who don't know, FizzBuzz is the following problem: 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…
bpromas
  • 684
  • 1
  • 11
  • 25
7
votes
8 answers

FizzBuzz cleanup

I'm still learning Haskell, and I was wondering if there is a less verbose way to express the below statement using 1 line of code: map (\x -> (x, (if mod x 3 == 0 then "fizz" else "") ++ if mod x 5 == 0 then "buzz" else ""))…
Jonathan Dunlap
  • 2,581
  • 3
  • 19
  • 22
7
votes
3 answers

FizzBuzz Ruby one-liner

Rosettacode.org has this excellent one-line FizzBuzz solution in Ruby. 1.upto(100){|n|puts'FizzBuzz '[i=n**4%-15,i+13]||n} The trouble is, I don’t understand it. The part that puzzles me is the ”n to the power of 4 modulo -15”. Does anyone have an…
Fred
  • 564
  • 10
  • 21
1
2 3
19 20