Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

I Propose a challenge. (FIZZ BUZZ)

Name: Anonymous 2007-09-01 20:19 ID:Ei0Nz+V2

The fizz buzz challenge

Make a program(in the language of your choice), that goes like this:

1)Prints all numbers from one to one hundred
2)For every number that can be divided with 3, it writes "fizz" next to the number.
3)For every number that can be divided with 5, it writes "buzz".

Should look like this:

1
2
3 Buzz
4
5 Fizz
6 Buzz
7
8
9 Buzz
10 Fizz
11
12 Buzz
13
14
15 FizzBuzz
16
17
18 Buzz

This a common problem employers give to seperate the Expert programers from the Enterprise Qulity Programmers. Surprisingly, not many programmers can. I made one in BASIC in 5 minutes.

Lets see how /PROG/ stands up to this.

Name: Anonymous 2007-09-02 8:46 ID:Heaven

I propose you GTFO

Name: Anonymous 2007-09-02 8:59 ID:WvkUH96t

This is way way too easy a problem to given by any employer to seperate the crap from the crapper.

Name: Anonymous 2007-09-02 9:09 ID:Heaven

>>42
True. It will probably work as a first screening test to weed out the ENTERPRISE fuckwits who have an impressive list of certificates and no programming skills whatsoever, though.

Name: Anonymous 2007-09-02 9:34 ID:Heaven

Now propose a new challenge: a program that actually does something useful.

Name: Anonymous 2007-09-02 9:38 ID:Heaven

>>42
Well, if your employer gives you this problem, he's probably crap.

Name: Anonymous 2007-09-02 9:53 ID:yXa5pFup

>>45
or he reads digg all day to check out WHATZ NEW IN DA BLOGOSFEAR

Name: Anonymous 2007-09-02 10:02 ID:dQk8FlwG


;FIZZBUZZ - DOES STUFF ABOVE
CHROUT EQU $FFD2 ;COMMODORE 64 ROUTINE THAT PRINTS CHARACTER IN .A TO STDOUT; CHANGE ACCORDINGLY FOR OTHER ARCHITECTURES
WHICH  .DB 0
POINT3 .DB 0
POINT5 .DB 0
FZBZ   LDA #$00
       STA WHICH
NEXT   INC WHICH
       LDA WHICH
       JSR PRINTA
       LDA #32 ;PRINT A SPACE
       JSR CHROUT
       LDA WHICH
       CMP #101
       BEQ GTFO
       INC POINT3
       LDA POINT3
       CMP #3
       BNE SKIP1
       JSR BUZZ
       LDA #0
       STA POINT3
SKIP1  INC POINT5
       LDA POINT5
       CMP #5
       BNE SKIP2
       JSR FIZZ
       LDA #0
       STA POINT5
SKIP2  LDA #13 ;PRINT A CR
       JSR CHROUT
       JMP NEXT
       ;I'M TOO TIRED RIGHT NOW TO ACTUALLY WRITE OUT THESE ROUTINES
       ;REST ASSURED THAT DUE TO MY EXPERT PROGRAMMER ABILITY THAT I CAN
FIZZ   ;PRINTS FIZZ
BUZZ   ;PRINTS BUZZ
PRINTA ;CONVERT NUMBER IN .A TO INTEGER AND PRINT IT, NO NEWLINE

Name: Anonymous 2007-09-02 10:30 ID:G85jbkOo

Python, 1.5 minutes:

def fizzbuzz():
    for num in range(1, 101):
        out = ""
        if num % 3 == 0: out = "Fizz"
        if num % 5 == 0: out += "Buzz"
        print num, out

Factor, roughly 5 minutes:

: check ( str div num -- ) swap mod 0 = [ write ] [ drop ] if ;
: output ( num -- num ) dup number>string write " " write ;
: fizz ( num -- num ) dup "Fizz" 3 rot check ;
: buzz ( num -- num ) dup "Buzz" 5 rot check ;
: fizzbuzz 1 100 [ output fizz buzz "" print 1 + ] times drop ;

Name: Anonymous 2007-09-02 10:34 ID:Heaven

>>48
You do realize that listing the time wasted on this worthless shit makes you look proud of it, right?

Name: Anonymous 2007-09-02 10:35 ID:G85jbkOo

>> 49
Whatever

Name: Anonymous 2007-09-02 10:35 ID:G85jbkOo

>>50
I fail

Name: Anonymous 2007-09-02 11:08 ID:dWC7VqBB

>>51
You'd failed long before you posted >>50

Name: Anonymous 2007-09-02 11:22 ID:7mZAOyMA

mapM_ putStrLn $ filter (not . null) $ map (\ x -> foldr (\ (y, s) b -> if (x `mod` y) == 0 then s++b else b) [] [(3, "Fizz"), (5, "Buzz")]) [1..100]

Or, a bit more elegant through the wonders of pointlessness:

mapM_ putStrLn (filter (not . null) (map (flip (flip foldr [] . flip ap snd . (. fst) . flip flip id . ((flip . (ap .)) .) . flip flip (++) . (((.) . (.)) .) . (if' .) . flip flip 0 . ((==) .) . mod) [(3, "Fizz"), (5, "Buzz")]) [1..100]))

Name: Anonymous 2007-09-02 13:40 ID:zRiO+5EK

>>53
The first line looks kinda nasty, but the second implementation is pointless. Elegant m y ass, it looks like the worst from Lisp and Perl.

Name: Anonymous 2007-09-02 13:55 ID:7mZAOyMA

>>54
map = flip foldr [] . ((:) .)
(++) = flip (foldr (:))
concat = foldr (++) []

Name: Anonymous 2007-09-02 17:50 ID:Oi3oqhLt

It's ironic that >>12's solution is the cleanest one here. Take that suave lisp weenies / charming haskell puppies

Name: Anonymous 2007-09-02 17:57 ID:zRiO+5EK

I took about 50 seconds. Untested.


for i in xrange(1, 101):
    print i,
    if not i % 3: print 'fizz',
    if not i % 5: print 'buzz',
    print

Name: Anonymous 2007-09-02 17:58 ID:Oi3oqhLt

>>57
It looks to me you just plagiarised >>12

Name: Anonymous 2007-09-02 18:13 ID:zRiO+5EK

>>58
I didn't, honest. I just wrote the simplest crap I could think of.

Name: Anonymous 2007-09-02 18:29 ID:VZTHHFJ2

i give you ruby:

(1..100).each do |i|
  print i
  print "fizz" if i % 3 == 0
  print "buzz" if i % 5 == 0
  print "\n"
end


or, you could just get stupid..

(1..100).each do |i|
  text = [i]
  text << "fizz" if i % 3 == 0
  text << "buzz" if i % 5 == 0
  print text.join(" "), "\n"
end

Name: Anonymous 2007-09-02 18:44 ID:RCJevnca

or, you could just get way too smart..


%a = ('fizz', '$_ % 3', 'fuzz', '$_ % 3');
for (1..100) {
    print;
    eval $b and print $a
        while ($a, $b) = each %a;
    print "\n"
}

Name: Anonymous 2007-09-02 18:57 ID:VZTHHFJ2

or, you could just get way too pointless..



class Fixnum
  def divisible_by?(num)
    self % num == 0
  end
end

class FizzBuzzTest

  attr_accessor :num, :text

  def initialize(num)
    @num = num
    @text = num.to_s
  end

  def test_and_print
    self.add_fizz if self.add_fizz?
    self.add_buzz if self.add_buzz?
    self.print_text
  end

  private

  def print_text
    print self.text + "\n"
  end

  def add_buzz?
    self.num.divisible_by?(5)
  end

  def add_fizz?
    self.num.divisible_by?(3)
  end

  def add_buzz
    self.text += " buzz"
  end

  def add_fizz
    self.text += " fizz"
  end

end

(1..100).each do |i|
  t = FizzBuzzTest.new(i)
  t.test_and_print
end


not tested, at all.

Name: Anonymous 2007-09-02 19:09 ID:Heaven

>>61 has a bug, but fixed :)

%a = ('fizz', '$i % 3', 'buzz', '$i % 5');
for $i (1..100) {
    eval $b and $c .= $a while ($a, $b) = each %a;
    print "$i$c\n" and $c = q{}}


Name: Anonymous 2007-09-02 19:12 ID:RCJevnca

>>9
sweet
>>34
awesome!

Name: Anonymous 2007-09-02 19:25 ID:Heaven

>>56
there's nothing inherently clean about >>12, it's just that you've been educated stupid to believe in the imperative world

Name: Anonymous 2007-09-02 19:43 ID:soxI90AR

>>64
i spend about 4 hours on that perl one several months ago.
the factor one took about 20 seconds.

Name: Anonymous 2007-09-02 19:54 ID:Heaven

>>66
s/d /t /

Name: Anonymous 2007-09-02 21:06 ID:Qr86Y3zC

void fizzbuzz( int r = 100 ) {
    printf( "%d %s %s", r, r%3?" Buzz":"", r%5?" Fizz":"" );
    return 0 && fizzbuzz( r-- );
}

inb4 database-bound xml and web2.0

Name: Anonymous 2007-09-02 23:11 ID:tEMYy0hC

>>68
that does not work, for many reasons.

Name: Anonymous 2007-09-02 23:43 ID:gtnUobJ9

$ cat fb.cpp; ./fb
#include "stdio.h"

int fizzbuzz( int r = 1 )
{
    printf( "%d%s%s\n", r, !(r%3)?" Buzz":"", !(r%5)?" Fizz":"" );
    return r - 100 && fizzbuzz( ++r );
}

int main()
{
    return fizzbuzz();
}

1
2
3 Buzz
4
5 Fizz
6 Buzz
7
8
9 Buzz
10 Fizz
11
12 Buzz
13
14
15 Buzz Fizz
16
17
18 Buzz

Name: Anonymous 2007-09-02 23:44 ID:B9GT1MYj

for (i=1; i<=100; i++)
{
 System.out.print(i);
 if (i%3)
  System.out.print(" fizz");
 if (i%5)
  System.out.print(" buzz");
 System.out.println("");
}

Not tested. Should work. This was meant to be in Java. May have mixed up my languages.

Name: Anonymous 2007-09-03 1:00 ID:Heaven

>>69
Sorry, I was high when I wrote that. See >>70 who has graciously corrected my grievous mistakes.

Name: Anonymous 2007-09-03 3:17 ID:RldtIkzN

>>38

<?php
for($i=1;$i<=100;$i++)echo $i.($i%3?'':'Fizz').($i%5?'':'Buzz')."\n";
?>


much nicer version

Name: Anonymous 2007-09-03 4:27 ID:VKg4YkaS

>>15
Oh, is that right?

#include <stdio.h>

int main(void)
{
  int i;
  int r = 0;
  char buff[] = "FizzBuzz";

  for (i = 1; i <= 100; i++) {
    sprintf(buff, "%d", i);
    if (! (i % 3))
      r = sprintf(buff + r, "%s", "Fizz");
    if (! (i % 5))
      r = sprintf(buff + r, "%s", "Buzz");
    puts(buff);
    r = 0;
  }

  return 0;
}

How's that?

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
.
.
.

Name: Anonymous 2007-09-03 5:04 ID:sIzmUEwb

#include <stdio.h>
int main()
{
for(int i=1;i<=100;++i)
  printf("%u %s%s\n", i, i%5==0?"fizz":"", i%3==0?"buzz":"");
}

Name: Anonymous 2007-09-03 6:37 ID:KJvW0vyq

%% fizzbuzz.pl (prolog, grumble)
output( C, 3, BC ) :- write('fizz'), output( C, 0, BC ).
output( C, FC, 5 ) :- write('buzz\n'), step(C, FC, 0).
output( Count, FC,BC ) :- write('\n'), step( Count, FC, BC).
step(100,_,_).
step(Count, FC, BC) :-
        FC2 is (FC+1),
        BC2 is (BC+1),
        C is (Count + 1),
        write(C),write(' '),
        output(C, FC2, BC2).
fizzbuzz :- step(0,0,0).

%% Mmmmmmmm recursion.

Name: Anonymous 2007-09-03 6:41 ID:KJvW0vyq

>>75 Iterative
>>76 Recursive

learn when to use them.

Name: Anonymous 2007-09-03 6:55 ID:UR/BfnOP

>>77
Recursive, yet iterative:


(define (iter i)
(if (<= i 100)
    (begin (display i) (display " ")
           (if (= 0 (modulo i 3)) (display "fizz"))
           (if (= 0 (modulo i 5)) (display "buzz"))          
           (display "\n")
           (iter (+ i 1)))))
(iter 1)

Name: Anonymous 2007-09-03 6:59 ID:sIzmUEwb

>>77
A recursive solution is quite inappropriate for this problem.

Name: Anonymous 2007-09-03 8:15 ID:KJvW0vyq

>>76
>>77

Both my posts :)
I just really enjoy writing prolog.  It was previously said about another language, but I do think programs written in prolog take longer to think about than to type. 

Still you wouldnt want to implement an operating system with it.....
Or would you?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List