The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:
"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"."
Post your solutions!
Wrap them in a code tag wrapped in a spoiler tag.
Critique others' solutions!
Example:
Code:
Code:
for number in range(1, 101):
output = str()
if not number % 3:
output += 'Fizz'
if not number % 5:
output += 'Buzz'
if not output:
output = str(number)
print(output)
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:
"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"."
Post your solutions!
Wrap them in a code tag wrapped in a spoiler tag.
Critique others' solutions!
Example:
Code:
Code:
for number in range(1, 101):
output = str()
if not number % 3:
output += 'Fizz'
if not number % 5:
output += 'Buzz'
if not output:
output = str(number)
print(output)
I just realized this a minute ago. That's a result of me coding Python 2 at work and Python 3 at home. I tend to mix them up. It was totally valid Python 2 code.
Enumerable.Range(1, 100).Select(i => i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" : i % 3 == 0 ? "Fizz" : i % 5 == 0 ? "Buzz" : i.ToString()).ToList().ForEach(s => Console.WriteLine(s));
Here's a solution that accomplishes this using only one loop and two If statements, and no Else clauses.
It's not one line though. Also not my best code, it's kinda (very) hacky. But it gives the right output. (Editted to be less hacky) (Edit reverted, because I realized I broke my own rule. Null Coalescing counts as an If)
Code:
Code:
List<string> list = Enumerable.Range(1, 100).Select(i => " " + i.ToString() + " ").ToList();
for (int i = 0; i < 100; i++)
{
if ((i + 1) % 3 == 0)
list[i] = "Fizz ";
if ((i + 1) % 5 == 0)
list[i] = list[i].Insert(list[i].IndexOf(" "), "Buzz ");
Console.WriteLine(list[i].TrimStart().Substring(0, 8));
}
public class Main {
public static void main(String[] args) {
for (int num = 1; num < 101; num++) {
int start = num * num % 3 * 4;
int end = 8 - (((int)-Math.pow(num, 4) % 5) + 5) % 5;
System.out.println(start == end ? num + "" : "FizzBuzz".substring(start, end));
}
}
}
Code:
Code:
print ("\n".join(["FizzBuzz"[number * number % 3 * 4:8 - -number ** 4 % 5] or str(number) for number in range(1, 101)]))
HAI
I HAS A LIMIT ITZ 101
I HAS A FIZZ ITZ "Fizz"
I HAS A BUZZ ITZ "Buzz"
I HAS A NOTHING ITZ ""
I HAS A LOOPZ ITZ 1
IM IN YR LOOP UPPIN YR LOOPZ TIL BOTH SAEM LOOPZ AN LIMIT
I HAS A THREEMOD ITZ MOD OF LOOPZ AN 3
I HAS A FIVEMOD ITZ MOD OF LOOPZ AN 5
I HAS A THING ITZ NOTHING
BOTH SAEM THREEMOD AN 0, O RLY?
YA RLY, THING R SMOOSH THING AN FIZZ MKAY
OIC
BOTH SAEM FIVEMOD AN 0, O RLY?
YA RLY, THING R SMOOSH THING AN BUZZ MKAY
OIC
BOTH SAEM THING AN NOTHING, O RLY?
YA RLY, VISIBLE LOOPZ
NO WAI, VISIBLE THING
OIC
IM OUTTA YR LOOP
KTHXBYE
HAI
I HAS A LIMIT ITZ 101
I HAS A FIZZ ITZ "Fizz"
I HAS A BUZZ ITZ "Buzz"
I HAS A NOTHING ITZ ""
I HAS A LOOPZ ITZ 0
IM IN YR LOOP UPPIN YR LOOPZ TIL BOTH SAEM LOOPZ AN LIMIT
I HAS A THREEMOD ITZ MOD OF LOOPZ AN 3
I HAS A FIVEMOD ITZ MOD OF LOOPZ AN 5
I HAS A THING ITZ NOTHING
BOTH SAEM THREEMOD AN 0, O RLY?
YA RLY, THING R SMOOSH THING AN FIZZ MKAY
OIC
BOTH SAEM FIVEMOD AN 0, O RLY?
YA RLY, THING R SMOOSH THING AN BUZZ MKAY
OIC
BOTH SAEM THING AN NOTHING, O RLY?
YA RLY, VISIBLE LOOPZ
NO WAI, VISIBLE THING
OIC
IM OUTTA YR LOOP
KTHXBYE
I'm so sorry, I had to be "That Guy", but I would totally turn this in for a job application.
Code:
Code:
def main():
for i in range(1, 101):
try:
print(isFizzBuzz(i, isFizz(i), isBuzz(i)))
except ValueError:
"Your fizz can no longer buzz!"
"""isFizz calcuates if the integer n is divisble by 3"""
def isFizz(n):
FizzList = []
if (n % 3 == 0):
FizzList.extend((True, 'Fizz'))
else:
FizzList.append(False)
return (FizzList)
"""isBuzz calculates if the integer n is divisble by 5"""
def isBuzz(n):
BuzzList = []
if (n % 5 == 0):
BuzzList.extend((True, 'Buzz'))
else:
BuzzList.append(False)
return (BuzzList)
"""isFizzBuzz calculates if the integer n is divisble by 3 and 5"""
def isFizzBuzz(n, myFizzList, myBuzzList):
myFizzList = isFizz(n)
myBuzzList = isBuzz(n)
if (myFizzList[0] == True and myBuzzList[0] == True):
return (myFizzList[1] + myBuzzList[1])
elif (myFizzList[0] == True and myBuzzList[0] == False):
return (myFizzList[1])
elif (myFizzList[0] == False and myBuzzList[0] == True):
return (myBuzzList[1])
else:
return n
main()
No, it isn't a hard thing to program. However a lot of people claim that they can program in a job interview. This leads to issues of companies hiring people who have to be spoon fed on how to do things when it comes time to work.
It is really just a simple thing to prove that you aren't lying on a job application. As I have known many math/actuarial majors to do in job interviews.
I'm so sorry, I had to be "That Guy", but I would totally turn this in for a job application.
Code:
Code:
def main():
for i in range(1, 101):
try:
print(isFizzBuzz(i, isFizz(i), isBuzz(i)))
except ValueError:
"Your fizz can no longer buzz!"
"""isFizz calcuates if the integer n is divisble by 3"""
def isFizz(n):
FizzList = []
if (n % 3 == 0):
FizzList.extend((True, 'Fizz'))
else:
FizzList.append(False)
return (FizzList)
"""isBuzz calculates if the integer n is divisble by 5"""
def isBuzz(n):
BuzzList = []
if (n % 5 == 0):
BuzzList.extend((True, 'Buzz'))
else:
BuzzList.append(False)
return (BuzzList)
"""isFizzBuzz calculates if the integer n is divisble by 3 and 5"""
def isFizzBuzz(n, myFizzList, myBuzzList):
myFizzList = isFizz(n)
myBuzzList = isBuzz(n)
if (myFizzList[0] == True and myBuzzList[0] == True):
return (myFizzList[1] + myBuzzList[1])
elif (myFizzList[0] == True and myBuzzList[0] == False):
return (myFizzList[1])
elif (myFizzList[0] == False and myBuzzList[0] == True):
return (myBuzzList[1])
else:
return n
main()
It just goes against certain languages with a strict entry point. But in the long run I believe the compiler is smart enough to reorganize the code and be able to float the methods to the top like how Javascript reorders stuff. In C it will, if you try to use -ansi flag will throw a huge error right at compile because the method calls in main cannot be found/referenced before declaration of the function signature and the method body itself.
Really in a FizzBuzz test, they aren't looking for a OOP solution it is an algorithm question of how do you deal with looping and control flow. Another quick question you get in an interview is looping through a singly linked list, using a loop is what they want to see and how you deal with copy. Another one is writing a factorial algorithm that does tail recursion or fibonacci sequence showing dynamic programming.
So to make it even better control on the Entry Point try this:
Code:
Code:
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print() # Just a newline
sys.exit()
"""import statements would go here if any"""
"""isFizz calcuates if the integer n is divisble by 3"""
def isFizz(n):
FizzList = []
if (n % 3 == 0):
FizzList.extend((True, 'Fizz'))
else:
FizzList.append(False)
return (FizzList)
"""isBuzz calculates if the integer n is divisble by 5"""
def isBuzz(n):
BuzzList = []
if (n % 5 == 0):
BuzzList.extend((True, 'Buzz'))
else:
BuzzList.append(False)
return (BuzzList)
"""isFizzBuzz calculates if the integer n is divisble by 3 and 5"""
def isFizzBuzz(n, myFizzList, myBuzzList):
myFizzList = isFizz(n)
myBuzzList = isBuzz(n)
if (myFizzList[0] == True and myBuzzList[0] == True):
return (myFizzList[1] + myBuzzList[1])
elif (myFizzList[0] == True and myBuzzList[0] == False):
return (myFizzList[1])
elif (myFizzList[0] == False and myBuzzList[0] == True):
return (myBuzzList[1])
else:
return n
def main():
for i in range(1, 101):
try:
print(isFizzBuzz(i, isFizz(i), isBuzz(i)))
except ValueError:
"Your fizz can no longer buzz!"
It just goes against certain languages with a strict entry point. But in the long run I believe the compiler is smart enough to reorganize the code and be able to float the methods to the top like how Javascript reorders stuff. In C it will, if you try to use -ansi flag will throw a huge error right at compile because the method calls in main cannot be found/referenced before declaration of the function signature and the method body itself.
Really in a FizzBuzz test, they aren't looking for a OOP solution it is an algorithm question of how do you deal with looping and control flow. Another quick question you get in an interview is looping through a singly linked list, using a loop is what they want to see and how you deal with copy. Another one is writing a factorial algorithm that does tail recursion or fibonacci sequence showing dynamic programming.
So to make it even better control on the Entry Point try this:
Code:
Code:
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print() # Just a newline
sys.exit()
"""import statements would go here if any"""
"""isFizz calcuates if the integer n is divisble by 3"""
def isFizz(n):
FizzList = []
if (n % 3 == 0):
FizzList.extend((True, 'Fizz'))
else:
FizzList.append(False)
return (FizzList)
"""isBuzz calculates if the integer n is divisble by 5"""
def isBuzz(n):
BuzzList = []
if (n % 5 == 0):
BuzzList.extend((True, 'Buzz'))
else:
BuzzList.append(False)
return (BuzzList)
"""isFizzBuzz calculates if the integer n is divisble by 3 and 5"""
def isFizzBuzz(n, myFizzList, myBuzzList):
myFizzList = isFizz(n)
myBuzzList = isBuzz(n)
if (myFizzList[0] == True and myBuzzList[0] == True):
return (myFizzList[1] + myBuzzList[1])
elif (myFizzList[0] == True and myBuzzList[0] == False):
return (myFizzList[1])
elif (myFizzList[0] == False and myBuzzList[0] == True):
return (myBuzzList[1])
else:
return n
def main():
for i in range(1, 101):
try:
print(isFizzBuzz(i, isFizz(i), isBuzz(i)))
except ValueError:
"Your fizz can no longer buzz!"
The premier forum for overclocking experts and enthusiasts. Discuss hardware optimization, custom builds, benchmarking, cooling solutions, and pushing the boundaries of computing performance. From beginner guides to extreme overclocking, join our technical community to master system tuning.