Isn't that harder than the actual question?
def is_prime(x):
if x%3 and x%2:
return True
else:
return False
print is_prime(4)
print is_prime(7)
EDIT: Oops, nevermind... you're all right and I'm wrong. I'm not fixing this code so everyone can see exactly how I screwed up.Am I missing something, or is that basically what you're looking for?
So... actually, I don't know if it's filtering anything really. It's just interesting to watch people do it and it at least establishes that the candidate isn't a complete faker.
for (int i = 1; i <= 100; ++i) {
boolean pureNumba = true;
if (i % 3 == 0) {
System.out.print("Fizz");
pureNumba = false;
}
if (i % 5 == 0) {
System.out.print("Buzz");
pureNumba = false;
}
if (pureNumba) {
System.out.print(i);
}
System.out.println();
} if (i % 3 != 0 && i % 5 != 0)
this extra boolean variable out?The beauty of FizzBuzz is that while it's obvious it can lead to interesting places. If a candidate just knows a way or two to implement it they'll quickly run out of things to say when you're talking about their solution. Discussing a solution and improving it is a skill you want in a developer. FizzBuzz can discover how competent they are at that. The way I use FizzBuzz isn't really about finding out if someone can code. It's about something bigger than that.
["Fizz" if isinstance(x,int) and x%3==0 else x for x in ["Buzz" if isinstance(x,int) and x%5==0 else x for x in ["FizzBuzz" if x%3==0 and x%5==0 else x for x in l]]]
I'm using a triple check, I'll find a better solution with just two check.
['FizzBuzz' if x % 15 == 0
else 'Fizz' if x % 3 == 0
else 'Buzz' if x % 5 == 0
else x for x in range(100) if type(x) is int]Do it yourself first, so that you have interesting questions to ask; and you can keep iterating on it too. Get them to write tests for it, documentation, what are the error conditions, how do they want to do error processing, and so on.
"[FizzBuzz helps to] filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag."
He's not saying 99.5% of candidates can't do it, but rather 99.5% of candidates who can't seem to program their way out of a wet paper bag can't do it. Presumably 1 in 200 of the candidates who can't program manage it by blind luck.
The title here on HN is wrong too.
when he said "print every number up to one hundred and if its the 3rd line print fizz and if its the fifth line print buzz"... honestly my train of thought was "why the fk would u print every line and print again if its the 3rd line and print again if its the fifth line...?? and why would you print 6-100?"... not the best explanation either, and i had no idea he meant print the string "Fizz" when he said print fizz... i thought maybe he meant some ruby gem rails.Fizz or what not...
of course the commenters code example above is clear as day, maybe i just read code better than i talk about code (this so much... for a lot of us). and i didnt get that job.
Best programming test I've seen was one that was done online, and automatically ran your test through unit tests.
Doing a test is fine, but do it, don't waste my time and move on accordingly to the result. Also, make me do it first, so the technical ability is off the table at an interview, don't make me 1) do it "on paper", (or like a quiz show) and in your location and 2) rate on objective measures (like passing/failing some test) and be transparent about scores (like they would be in a code review)
I agree with being transparent about the discussion around the result of any programming done during an interview but having the person do it before hand defeats the purpose of using it to catch out people who can't actually code because they will just Google the result.
Then don't use FizzBuzz. It's that simple
FizzBuzz irritates me because they haven't gone to the trouble of thinking something else.
Another thing I was given was "make me something interesting in 30min" or "try to solve this problem in 2 hours"
<?php
for ($i=1;$i<=100;$i++) {
if ( ($i%3===0) AND ($i%5===0) ) {
echo "FizzBuzz\n";
} else if ($i%3===0) {
echo "Fizz\n";
} else if ($i%5===0) {
echo "Buzz\n";
} else {
echo $i."\n";
}
}
?>Why the this story, just if you give anyone a choice of any language to solve fizzbuzz or other problem, be sure you are open to see code that looks strange to you.
https://www.goodreads.com/quotes/540355-he-walked-straight-o...
for (var i = 1 ; i <= 100; i++) {
console.log(
_.chain(["Fizz", "Buzz"])
.zip(_.map([3, 5], function(n) { return i%n;}))
.where({"1": 0})
.pluck(0)
.value().join("")
|| i);
} _.each(_.range(1, 100), function (i) {
console.log(
_.chain(["Fizz", "Buzz"])
.zip(_.map([3, 5], function(n) { return i%n;}))
.where({"1": 0})
.pluck(0)
.value().join("")
|| i);
});The job of the person that gets interviewed is to weed out the places where you do not do anything interesting intellectually.
Sure, ask this question, but you'll ask quite a lot from your applicant to still consider your company an interesting place to work for.