[1+1=2]

OneAndOneIs2

« OuchThe BoyGirl paradox »

Thu, Oct 20, 2011

[Icon][Icon]FizzBuzz

• Post categories: Omni, My Life, Programming

I was reading this article about self-proclaimed programmers who can't actually program - something I've had some experience of myself.

I quite liked the idea of the FizzBuzz challenge:

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".

It's dead easy to do this sloppily: Test if it divides by three and five, else test if it divides by three, else test if it divides by five, else print the number. But it's slightly more difficult to do it neatly/elegantly.

So I gave myself five minutes to try and do it 'properly', and I came up with this bit of perl:
for my $i (1..100){
my $print;
$print .= "Fizz" unless $i % 3;
$print .= "Buzz" unless $i % 5;
$print ||= $i;
print "$print\n";
}

Anyone know of any other interesting ways to do it? :)

11 comments

codeskraps
Comment from: codeskraps [Visitor]
Last semester in college while studying procedural programming with C the teacher gave us an assignment on who could come up with the most efficient bit of code to do what you just suggested.

Because we used C to do the programming I thought using pre-compilers would be the most efficient. So, I came up with this:

#define isMultipleOf(x) ((x)%15 == 0 ? 15 : \
((x)%5 == 0 ? 5 : \
((x)%3 == 0 ? 3 : 1)))

I then had a loop from 0 to 99 and switch statement printing "fizz", "buzz" or "fizzbuzz" depending on the numbers... You get the point.

I was looking forward to see who come up with the fatest bit of code between my classmates but to my disappointment it was only a trick to make us do the assignment.

I have to say it worked because I never spend so much time doing something so simple
20/10/11 @ 11:33
pdh
Comment from: pdh [Visitor]
The sexiest one liner I could find is.

print 'Fizz'x!($_ % 3) . 'Buzz'x!($_ % 5) || $_ , "\n" for 1 .. 100;

I hope somebody asks me that in an interview in the future now :).
20/10/11 @ 12:19
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Oh, I like that one :)
20/10/11 @ 12:34
pdh
Comment from: pdh [Visitor]
Fibbuzz:
Print 100 fibonacci numbers, but replace multiples of 3 with "Fizz" and multiples of 5 with "Buzz" and multiples of both with "FizzBuzz"

My solution.

use bignum;
@p=(0,1);until($#p>101){print'Fizz'x!($p[-2] % 3) . 'Buzz'x!($p[-2] % 5) || $p[-2] , "\n";push @p,$p[-2]+$p[-1]}
20/10/11 @ 16:36
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
This is what happens when OOP is emphasized to such an extent that it overshadows every other programming concept. Everything has become an "object" and it's ridiculous how some programming languages insist on atomic data types being "objects".

I think people have forgotten the art of writing simple algorithms.

Love live Procedural Programming (and its derivatives).
21/10/11 @ 04:01
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
I came up with this in Python. Not too clever, but it looks readable and nice.

for i in range (1, 100):
  if (i % 3) <> 0 and (i % 5) <> 0: print i,
   if (i % 3) == 0: print "Fizz",
   if (i % 5) == 0: print "Buzz",
   print ", ",
24/10/11 @ 15:39
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
Forgive the indentation problem in the above. There should be an extra space after the first line.
24/10/11 @ 15:41
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
Oh, yes, to reproduce the same without spaces, I used sys.stdout.write

import sys

for i in range (1, 100):
   if (i % 3) <> 0 and (i % 5) <> 0: sys.stdout.write ("%d" % i)
   if (i % 3) == 0: sys.stdout.write ("Fizz")
   if (i % 5) == 0: sys.stdout.write ("Buzz")
   sys.stdout.write ("\n")
24/10/11 @ 15:52
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
BTW, did I mention that I hate breaking my head thinking up of "elegant" solutions for trivial problems?

OK, this is absolutely last post to spam your blog.
24/10/11 @ 15:53
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
OK, damn, I got the range wrong. It should be range (1, 101)

*smacks head* I hate trivial problems. :P
24/10/11 @ 16:11
Hari
Comment from: Hari [Member] · http://harishankar.org/blog/
So I'm just learning Haskell and it immediately occured that it might be a good exercise to program it in Haskell as well. Here's what I came up with:

fizzbuzz n = if n `mod` 15 == 0 then "FizzBuzz" else if n `mod` 5 == 0 then "Buzz" else if n `mod` 3 == 0 then "Fizz" else show n
fizzbuzzseq range = [ fizzbuzz x | x <- [1..range]]
main = print (fizzbuzzseq 100)
25/10/11 @ 17:30

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] I'm in the Perl newsletter again. I should try and write about some other language...
21/05/12

[Icon][Icon] Facebook Syndication Error
22/05/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
js.js

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


May 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  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      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution