3 ms·
Breaks the spec: '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
by GeorgeTirebiter 14y ago
Breaks the spec: '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”.'
Actually, this spec is ambiguous, too. For multiples of five, do we print the number AND "Buzz" or just "Buzz" ? Similarly for multiples of 15.
I thought the OP was a joke, BTW. It had to be, doesn't it? If somebody thought of this problem and solution the way the job candidate did, why on earth would one want to hire somebody who loves complexity for its own sake?
To me, the following is the clearest way to express this (in C anyway). I suppose it says a lot about me! lol
#include <stdio.h>
#define FALSE (0)
#define TRUE (!FALSE)
int
main(){
for(int i=1; i <= 100; i++){
int printed_something_already=FALSE;
if(i%3 == 0){
printf("Fizz"); printed_something_already=TRUE;
}
if(i%5 == 0){
printf("Buzz"); printed_something_already=TRUE;
}
if(!printed_something_already) printf("%d",i);
printf("\n");
}
return 0;
}
- KirinDave 14y ago> I thought the OP was a joke, BTW. Well balls to you too, I guess? Or not. We're all just trying to have a good time here. And from the look of this thread, people are having fun. I think golfing fizzbuzz into an unreadable mess sorta misses the point, but most people here are software engineers because they love to code and build stuff. So let's enjoy the moment. > why on earth would one want to hire somebody who loves complexity for its own sake? You remind me of: http://wondermark.com/333/ http://wondermark.com/333/ It's not complexity for its own stake. I daresay the functional patterns tend to be simpler. Certainly moreso than things like Visitor or AbstractFactory. > To me, the following is the clearest way to express this (in C anyway). I suppose it says a lot about me! Your version is almost identical to the Ruby version I listed. And it's an appropriate solution for C.
- GeorgeTirebiter 14y agoI don't know what balls to me too means. Yes, if we're all trying to have a good time, we'd shoot heroin and be done with it. ;-) IMHO, we are group sharing to learn better ways to build startups, small businesses, and to better ourselves, co-workers, and companies with best practices - and to do it with fun and a twinkle in our eyes. I did look at that wordmark comic -- I believe I ask a credible question: If complexity buys you nothing, then why make something complex? And, believe me, if you are thinking "Visitor" or "AbstractFactory" then I have already lost you. ;-) Thank you for your comment about my C version. I wanted to make sure it was extensible and quite obvious -- because I write code to be read by humans as my 2nd goal (my first is that it works & meets the spec!). Knuth's Literate Programming taught me that. The other thing about that C solution is that it's simple and many people can understand it. One does not need to know the difference between a monad and a gonad to "get it". My overall comment: Complexity: when required; otherwise, choose simple. Believe me, I have learned this lesson over and over again during my 45 years of programming.
- KirinDave 14y ago> If complexity buys you nothing, then why make something complex? And, believe me, if you are thinking "Visitor" or "AbstractFactory" then I have already lost you. ;-) I think maybe the disconnect here is that I work on products with a much larger scale than you may be accustomed to. My idea of complexity begins to blossom at scales past many boxes. For me, 25 interconnected machines is barely enough to register as a "big system". So perhaps I am just desensitized? Abstraction is generally trying to find ways to mitigate complexity, not embrace it. At some point we need to have sympathy for the machines we run on to have efficient code, but at the same time you and I need to find good perspectives to write code at. A more concrete example of this is my advice that people building distributed systems take a cue from Boundary and have systems rendezvous in Zookeeper rather than just using something like hardwired ports and /etc/hosts. You may think that "sounds complex" but it's actually a very simple solutin from our perspective as users of Zookeeper. It ends up being pretty nice the next time a giant storm takes out half of us-east-1 and you're frantically re-provisioning boxes to keep your service up. Monoids are that kind of choice at the programming level. > Complexity: when required; otherwise, choose simple. Believe me, I have learned this lesson over and over again during my 45 years of programming. I think c_wraith's monoid example made the program simpler, cleaner, and less bug prone. If you think monoids are a "complex" idea, then my blog post has failed profoundly and I apologize for wasting your time.
- koide 14y agoI believe that efforts using trivial examples to show how complexity can be managed via certain abstractions are doomed to fail. And that's a pity, because it makes abstractions harder to explain in a proper context. You need an example where the complexity at least rears its ugly head before the abstraction starts to shine. For many people, especially those not familiar with it at the time of the reading, the simple example using a manage huge complexity abstraction reeks of "complexity for its own sake" or "killing flies with a cannon" or "premature optimization" and it's natural because the example is not showing the strengths of the abstraction, rather its weaknesses.
- 14y ago