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

【EXCERCISE】Programming Styles

Name: Anonymous 2010-06-08 13:49

People who code come from all walks of life and programming isn't always part of their profession. Some people receive proper training at a school, yet others learn on their own, and sometimes even a little bit of both. This sort of variety allows people to approach the same problem in a myriad different ways.

So then, let us all try this popular FizzBuzz exercise:

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

The purpose of this exercise is not to get the problem right or wrong (it was not chosen for its difficulty), but to demonstrate our different approaches to the problem.

## When posting your solution please tell us a little about yourself and your background in programming. ##

Name: Anonymous 2010-06-08 13:57

Oh, look.  It's THIS thread again.
>>1209141432
>>1259905156

Name: Anonymous 2010-06-08 14:01

>>2
Reading comprehension not your strong suit, then?

I don't really think FizzBuzz is complex enough to get huge difference in style, but alright.

I started out with PHP on my own, then spent a few years at a Java school, and finally ended up learning languages on my own because it became apparent I couldn't count on my professors.
C is one of my favorites, and I initially picked it up from K&R. At first I used Java indentation style because that's what I was used to, but eventually I moved to full K&R, because it's just more beautiful.
There are fancier ways to write FizzBuzz, but I like clarity.

#include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i <= 100; ++i) {
        if (i % 15 == 0)
            printf("FizzBuzz\n");
        else if (i % 3 == 0)
            printf("Fizz\n");
        else if (i % 5 == 0)
            printf("Buzz\n");
        else
            printf("%d\n", i);
    }

    return 0;
}

Name: Anonymous 2010-06-08 14:05

>>2
This is not a FizzBuzz thread. This is a thread to compare and contrast our different approaches to a problem. That problem just happens to be FizzBuzz.

Name: Anonymous 2010-06-08 14:06


showar x | m3 && m5  = "FizzBuzz"
         | m3        = "Fizz"
         | m5        = "Buzz"
         | otherwise = show x
  where [m3,m5] = map ((==0).mod x) [3,5]

fizzbuzz = do
   mapM_ (putStrLn.showar) [1..100]


I am a HASKELL NOMAD

Name: Anonymous 2010-06-08 14:19

>>2
How did you link those both to my post?

Name: Anonymous 2010-06-08 14:25

i am a 24yo year old psychology bachelor's working in retail. i am self taught in programming.


#!/usr/bin/python3000

for j in range(1,100):
        if j%5==0 and j%3!=0:
                print "Bizz"
        elif j%3==0 and j%5!=0:
                print "Fizz"
        elif j%3==0 and j%5==0:
                print "FizzBizz"
        else:
                print str(j)

Name: Anonymous 2010-06-08 14:25

>>5
boop foop gee goop       = foop goop (gee goop)
ping goop zoom           = goop
oot foop gee goop        = foop (gee goop)
pop foop gee goop        = foop goop gee
zoom foop                = foop (zoom foop)
cond bloop foop gee goop = if bloop goop then foop goop else gee goop
floop  = zoom (oot (cond ((==) 0) (ping 1)) (oot (boop (*)) (pop oot pred)))

Name: Anonymous 2010-06-08 14:36

I am not a classically trained programmer. I started out by editing CGI web scripts. At first I only made small changes, then bigger changes, and eventually I started writing my own web scripts. My personal rule of programming: Write the least amount of code as possible, so long as it is consistent in style and is readable.

As for a little bit about myself, I was a psych major at first (wanted to be an industrial psychologist), but then I decided I wanted to be a pharmacist instead, so now I'm biochem major. Not sure how that influences my coding.


#!/usr/bin/perl

for (my $count = 1 ; $count <= 100 ; $count++) {
    my $res;
    $res  = 'Fizz' if $count % 3 eq 0;
    $res .= 'Buzz' if $count % 5 eq 0;
    $res  = $count unless $res;
    print $res . "\n";
}

Name: Anonymous 2010-06-08 14:37

My natural solution in HASKAL.

fizzbuzz = mapM_ fizzprint [0..100]
           where fizzprint x | x `mod` 15 == 0 = putStrLn "fizzBuzz"
                             | x `mod` 3 == 0  = putStrLn "fizz"
                             | x `mod` 5 == 0  = putStrLn "buzz"
                             | otherwise       = putStrLn $ show x

Name: >>10 2010-06-08 14:39

As for the background; I've been programming for a year and a half, started with SICP, then learned Haskell and C to write fun programs for myself and C# to code for money.

Name: >>10 2010-06-08 14:40

Now I also see that I botched the spacing in the guards :(

Name: Anonymous 2010-06-08 14:43

>>6
Textboard magic and failure at thread linking.

Name: Anonymous 2010-06-08 14:45

My job involves programming Perl apps for UNIX. Here's my version.

#!/usr/bin/perl

for (1..100) {
    my $res;
    $res = "Fizz" unless ($_ % 3);
    $res = $res . "Buzz" unless ($_ % 5);
    $res = $_ unless ($res);
    print "$res\n";
}

Name: Anonymous 2010-06-08 15:10

fizzbuzz = mapM_ (\x -> putStrLn $ ["FizzBuzz", "Fizz", "Buzz", show x] !! (fromMaybe 3 . elemIndex 0 . ((:) =<< sum) $ map (mod x) [3, 5])) [1..100]

Name: Anonymous 2010-06-08 15:28

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int main(void)
{ for(uint_fast8_t i = 1; i <= 100; ++i)
    switch(i % 15)
    { case 0:
        puts("FizzBuzz"); break;
      case 3: case 6: case 9: case 12:
        puts("Fizz"); break;
      case 5: case 10:
        puts("Buzz"); break;
      default:
        printf(PRIuFAST8 "\n", i); }
  return 0; }

Name: Anonymous 2010-06-08 15:30

int i, t, f;
for (i = t = f = 0; i < 100; i++, t++ %= 3, f++ %= 5) {
 if (!t)
  printf ("Fizz");
 if (!f)
  printf ("Buzz");
 if (t && f)
  printf ("%d", i);
 printf ("\n");
}

Background: I am an EXPERT since birth

Name: Anonymous 2010-06-08 17:12

>>17
I think I just came a little inside.

Name: Anonymous 2010-06-08 17:20

>>1
People say im insane, but they just say that because im smarter then them. I am constantly drunk and like to trip on acid. I make money by making obscure paintings of anuses.

and here is my solution to your problem:

#!/usr/bin/perl
my $dick = '=';
sub disks {
  $dick x= 2; #I wish it could do better
  print '8'. $dick. 'D'; #i hope you have much memory
  fork(); #Prints Fizz
  disks(); #Prints Buzz
}

Name: Anonymous 2010-06-08 17:22

>>19
People say im insane, but they just say that because im smarter then them.
Oh my.

Name: Anonymous 2010-06-08 18:01

>>20
What about your?

Name: Anonymous 2010-06-08 18:05

>>21
It's oh.

Name: Anonymous 2010-06-08 18:05

irnoob just learned some Scheme yesterday...lets give this a go
i know this is wrong, idk how to do loops yet

(define(Increment x)(x+1))

(define(FizzBuzz x)
        (if (% x 15==0)buzzfizz
        (else (if(%x 5 == 0)buzz
                fizz)))
         (FizzBuzz(Increment x))

k nao java

for(int z=1; z<=100; z++){
   if(z%15==0)
     System.out.println(fizzbuzz);
   else{
     if(z%5==0)
       System.out.println(buzz);
     else
       System.out.println(fizz);

Name: Anonymous 2010-06-08 18:13

>>23
IHBT

Name: Anonymous 2010-06-08 18:58

I am a young male striper by night, and script kiddie by day.
<?php
for ($i = 1; $i <= 100; $i++) {
    if ($i % 15 == 0) {
        echo "FizzBuzz\n";
    } elseif ($i % 3 == 0) {
        echo "Fizz\n";
    } elseif ($i % 5 == 0) {
        echo "Buzz\n";
    } else {
        echo "{$i}\n";
    }
}
?>

Name: Anonymous 2010-06-08 19:12

>>19
I make money by making obscure paintings of anuses.
What is this madness?

Name: Anonymous 2010-06-08 19:18

>>26
Basically Anal Cunt's logo. He got about 78 cents.

Name: Anonymous 2010-06-08 19:55

They don't have to printed in order, right?
Junior undergrad CS fag, start programming so I could bot on runescape, then realized it was pretty interesting.

#include <pthread.h>
#include <stdio.h>

void* multiples_of_three(void* pause)
{
    int* p =  pause; /* Don't want to deal with TWO mutexes */
    for(int i=0; i <= 100; i += 3)
    {
        if(i % 5 == 0)
        {
            while(*p == 0);
            printf("Fizz", i);
            *p = 2;
            while(*p == 2);
        }
        else           
            printf("Fizz\n", i);
       
    }
}

void* multiples_of_five(void* pause)
{
    int* p =  pause;
    for(int i=0; i <= 100; i += 5)
    {
        if(i % 3 == 0)
        {
            *p = 1;
            while(*p == 1);
            printf("Buzz\n", i);
            *p = 0;
        }
        else
            printf("Buzz\n", i);
    }
   
}

void* everything_else(void* useless)
{
    for(int i=0; i < 100; i++)
        if((i%3) && (i%5))
            printf("%d\n", i);
}

int main()
{
    pthread_t three, five, others;
    int shared = 0;
   
   
    pthread_create(&three, NULL, multiples_of_three, &shared);
    pthread_create(&five, NULL, multiples_of_five, &shared);
    pthread_create(&others, NULL, everything_else, NULL);
   
    pthread_join(others, NULL);
    pthread_join(three, NULL);
    pthread_join(five, NULL);
}

Name: Anonymous 2010-06-08 20:00

>>28
The plural of ``mutex'' is ``mutices''.

Name: Anonymous 2010-06-08 20:02

>>29
I've been doing it wrong for six months. Thanks for the correction.

Name: Anonymous 2010-06-08 20:19

Web developer.


require 'rubygems'
require 'sinatra'

get '/' do
    <<-EOS
<!DOCTYPE html>
<html>
  <head>
    <title>FizzBuzz</title>
    <script src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js" type="text/javascript"></script>
    <script type="text/javascript">
      window.addEvent('domready', function(){
        var FizzBuzz = new Class({
          Implements: Options,
          initialize:function(options){
            this.position = 0;
            this.limit = 100;
            this.setOptions(options);
            this.element = this.options.element;
            this.request = new Request(this.options.request);
            this.request.addEvent('success', function(response){
              new Element('div', {
                text:response
              }).inject(this.element);
              if(this.position < this.limit) this.run();
            }.bind(this));
          },
          run:function(){
            this.position++;
            this.request.send({
              data:{
                x:this.position
              }
            });
            return this;
          },
        });
     
        var fizzBuzz = new FizzBuzz({
          element:$('fizzbuzz'),
          request:{
            url:'/',
            method:'post'
          }
        }).run();
      });
    </script>
  </head>
  <body>
    <div id="fizzbuzz"></div>
  </body>
</html>
EOS
end

post '/' do
  x = params[:x].to_i
  if x % 15 == 0
    "FizzBuzz"
  elsif x % 5 == 0
    "Buzz"
  elsif x % 3 == 0
    "Fizz"
  else
    x.to_s
  end
end

Name: Anonymous 2010-06-08 20:26

>>29
You should really be calling it GNU/Mutex.

Name: Anonymous 2010-06-08 21:22

>>24
I dont know why everyone always thinks im trolling.
Experience: took an intro programming class and 1 higher lvl programming class. Trying to independently study this summer, hence the poor scheme coding i learned from reading a few pages of SICP (Recommended to be by /g/)

Name: Anonymous 2010-06-08 21:35

>>33
And you should go back there, please

Name: Anonymous 2010-06-08 22:12

>>34
=/
because u asked kindly i wont post anything. I'll just lurk starting now

Name: Anonymous 2010-06-08 22:46

>>33
/g/ is recommending SICP to people?

Name: Anonymous 2010-06-08 22:47

>>36
Imitation is the sincerest form of flattery.

Name: Anonymous 2010-06-08 23:45

>>36
Yeah. It's like this is the freaking Twilight Zone or something.

Name: Anonymous 2010-06-09 0:00

>>35
Work on your spelling before you return.

Name: Anonymous 2010-06-09 3:16

>>8


Prelude> take 20 $ flip fmap [0..] floop
[1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000]

U MENA FLOOPTORIAL

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