>>> for x in range(1,20+1):
... if x % 3 == 0: print "Fizz"
... elif x % 5 == 0: print "Buzz"
... else: print x
(map
(lambda (x)
(format "~a~a," x
(case x
((3 6 9 12 18 21 24 27 33 36 39 42 48 51 54 57 63 66 69 72 78 81 84 87 93 96 99) " Fizz")
((5 10 20 25 35 40 50 55 65 70 80 85 95) " Buzz")
((15 30 45 60 75 90) " Fizz Buzz")
(else ""))))
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100))
Bah. That's not only over-engineered, it's unreadable. You want some nice clean functional code.Code: [Select](map
(lambda (x)
(format "~a~a," x
(case x
((3 6 9 12 18 21 24 27 33 36 39 42 48 51 54 57 63 66 69 72 78 81 84 87 93 96 99) " Fizz")
((5 10 20 25 35 40 50 55 65 70 80 85 95) " Buzz")
((15 30 45 60 75 90) " Fizz Buzz")
(else ""))))
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100))MoreI actually did it properly in order to get the lists of numbers :)
what does ~a~a do?It's a format string, a bit like C's printf() uses. And it's not necessary, as I copied my code from where I'd posted it elsewhere - it doesn't actually meet the requirements. This version does it more strictly.
(map
(lambda (x)
(case (x)
((3 6 9 12 18) 'fizz)
((5 10 20) 'buzz)
((15) '(fizz buzz))
(else x)))
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))
when someone explains how my version works, i will put up anther version ;)Puts a function into a namespace object (interviewTestAnswers) under the window, and then calls it with the data. That function is horrible, in that it mutates the data passed to it, and fails on fizz buzz. I can't see why you're testing type and value equality against undefined twice, either.
Puts a function into a namespace object (interviewTestAnswers) under the window, and then calls it with the data. That function is horrible, in that it mutates the data passed to it, and fails on fizz buzz. I can't see why you're testing type and value equality against undefined twice, either.
has anyone ever been asked this in an interview?
It actually doesn't say anything about outputting fizz or buzz either - you are to, and I quote, "output this array of numbers" :)
The assignment said "output", so there (also in Python):Code: [Select]>>> for x in range(1,20+1):
... if x % 3 == 0: print "Fizz"
... elif x % 5 == 0: print "Buzz"
... else: print x
Is the point of the interview question that the subject should ask what to do at number 15?
has anyone ever been asked this in an interview?
I've had two software engineering jobs (including my current job). The first of which required me to write programs to solve three problems/tasks. All three we significantly harder than a fizzbuzz though, which would make sense because producing a fizzbuzz program should hopefully be trivial for most professionals. But I can see the use of it if interviewer wanted to purely assess coding style.
That being said, my current programming job interviews (for a fortune 100 company) didn't involve any code at all...
has anyone ever been asked this in an interview?
I've had two software engineering jobs (including my current job). The first of which required me to write programs to solve three problems/tasks. All three we significantly harder than a fizzbuzz though, which would make sense because producing a fizzbuzz program should hopefully be trivial for most professionals. But I can see the use of it if interviewer wanted to purely assess coding style.
That being said, my current programming job interviews (for a fortune 100 company) didn't involve any code at all...
(define (fizzbuzz x)
(or
(and
(vector? x)
(begin
(display x)
(for-each
(lambda (y)
(cond
((zero? (mod (vector-ref x y) 3)) (vector-set! x y 'fizz))
((zero? (mod (vector-ref x y) 5)) (vector-set! x y 'buzz))))
(iota (vector-length x)))))
(error 'fizzbuzz "not a vector")))
;; Testing we get the expected result
> (fizzbuzz '#(1 2 3 4 5 6 7 8 9 10 1 12 13 14 15 16 17 18 19 20))
#(1 2 3 4 5 6 7 8 9 10 1 12 13 14 15 16 17 18 19 20)
;; Yep, looks good.
I am going to give you an array of numbers: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
You need to write a script that will output this array of numbers either sequentially, or all at once in an array. For each number in the array that is divisible by 3, replace the number with "Fizz" and for each number that is divisible by 5, replace the number with "Buzz"
def fizzbuzz(input):
for n in input:
if n % 3 == 0:
yield "Fizz"
elif n % 5 == 0:
yield "Buzz"
else:
yield n
Could you post your 3 challenges? I'm about 1 year into my first stint with programming and I'm hungry for bran twisters!
sheer curiosity, is it CS algo questions that you got tested on or more common problem solving?
com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.interfaces.visitors.OutputGenerationContextVisitor
has anyone ever been asked this in an interview? i don't understand how someone could be qualified and fail at the same time :/
Common guys that FizzBuzz thing is years old...
That said there's one FizzBuzz solution which I really like: it's a satire of Java called "FizzBuzz Enterprise Edition"... It's on GitHub:
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition (https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition)
It's quite complete with classes like:Code: [Select]com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.interfaces.visitors.OutputGenerationContextVisitor
to be sure to adhere to high-quality enterprise Java apps standards ;)
I'm a Java dev and honestly I find that one very funny ^ ^
#include <iostream>
int main() {
for (int number{1}; number <= 20; ++number) {
const bool fizz{number % 3 == 0};
const bool buzz{number % 5 == 0};
if (fizz || buzz) {
if (fizz) {
std::cout << "Fizz";
}
if (buzz) {
std::cout << "Buzz";
}
} else {
std::cout << number;
}
std::cout << std::endl;
}
}
*{
position:relative;
}
*:nth-child(3n):after{
content:"Fizz";
display:block;
position:absolute;
top:0;
bottom:0;
background-color:white;
}
*:nth-child(5n):before{
content:"Buzz";
background-color:white;
display:block;
position:absolute;
padding-left:3em;
top:0;
bottom:0;
}
I wrote one in CSS ;D
See it working: http://jsfiddle.net/4387o329/3/Code: [Select]*{
position:relative;
}
*:nth-child(3n):after{
content:"Fizz";
display:block;
position:absolute;
top:0;
bottom:0;
background-color:white;
}
*:nth-child(5n):before{
content:"Buzz";
background-color:white;
display:block;
position:absolute;
padding-left:3em;
top:0;
bottom:0;
}
This is closest to what I did and it is short, sweet, and to the point.It is also incorrect. If x is divisible by both 3 and 5 then the output should be "FizzBuzz". That is the catch!
>>> def fizzbuzz(seq):
... for x in seq:
... s = ""
... if x % 3 == 0: s += "Fizz"
... if x % 5 == 0: s += "Buzz"
... if s=="": s=str(x)
... yield s
...
>>> print ", ".join(fizzbuzz(range(1,20+1)))
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz
I wrote one in CSS ;D
See it working: http://jsfiddle.net/4387o329/3/Code: [Select]*{
position:relative;
}
*:nth-child(3n):after{
content:"Fizz";
display:block;
position:absolute;
top:0;
bottom:0;
background-color:white;
}
*:nth-child(5n):before{
content:"Buzz";
background-color:white;
display:block;
position:absolute;
padding-left:3em;
top:0;
bottom:0;
}
has anyone ever been asked this in an interview? i don't understand how someone could be qualified and fail at the same time :/
I have asked this question to people in interviews, even recently. You'd be amazed at how many people get it wrong.
And the original post forgot to mention 15... (just saying...)
I have asked this question to people in interviews, even recently. You'd be amazed at how many people get it wrong.
And the original post forgot to mention 15... (just saying...)
Yeah I actually realized that my program is wrong. I've fixed this on my own functions but haven't updated this post.
You are supposed to output 'FizzBuzz' if the number is divisible by both 3 and 5 :p