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

Pages: 1-4041-

【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

Name: O !!MaeNOMadEhM2YD1 2010-06-09 4:06

putStr . unlines $ do{ n <- [1..100]
                       case liftM (mod n) [3, 5] of
                            [0, 0] -> return "FizzBuzz"
                            [0, _] -> return "Fizz"
                            [_, 0] -> return "Buzz"
                            _ -> return $ show n } }

Name: Anonymous 2010-06-09 8:05

for i in (1..100)
  if i%3 == 0 && i%5 == 0
    puts "FizzBuzz"
    next i
    elsif i%3 == 0
    puts "Fizz"
        elsif i%5 == 0
    puts "Buzz"
    else
      puts i
  end
 end

Name: Anonymous 2010-06-09 8:38

>>17
I like this expert implementation

Name: Anonymous 2010-06-09 9:56

#!/usr/bin/python
for s in ('FizzBuzz' if n%15 == 0 else (
          'Buzz' if n%3 == 0 else (
          'Fizz' if n%5 == 0 else str(n)))
          for n in xrange(100)):
    print s

I think I'm doing python too much functional. I'm still not on PY3000, so I can't map print to it.

Name: Anonymous 2010-06-09 10:17

>>44
Protip: define _print(s): print s
Also note my use of [code] tags.

Name: Anonymous 2010-06-09 11:22

I am a C# asp.NET web developer. I hate web development, but I fucking love C# so much


using System;
using System.Linq;
using C = System.Console;

class Program
{
    public delegate bool FizzBuzz(int q, int p);
    static void Main()
    {
        FizzBuzz fb = (q, p) => { return ((q % p) == 0) ? true : false; };
        Enumerable.Range(1, 100).ToList().ForEach(z =>
                    {
                        if (fb(z,3)) { C.Write("Fizz"); }
                        if (fb(z,5)) { C.Write("Buzz"); }
                        if (!fb(z,3) && !fb(z,5)) C.Write(z);
                        C.Write("\r\n");
                    });
        C.ReadLine();
    }
}

Name: Anonymous 2010-06-09 11:51

>>45
I'm aware I can have my print function, it's just usually too bothersome to do.

Also, my background: VFP, UnrealScript (early age), C, C# (high school), C++, Python, Java(university)

When I have choice, I do either python or C(++)

Name: Anonymous 2010-06-09 12:23

REPOSING MY SOLUTION FROM http://dis.4chan.org/read/prog/1259905156/93


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define f(x) for(i=(x)-1;i<n;i+=(x)) a[i]+=(x);
int main() {
  int i,b=3,a[100],n=sizeof(a)/sizeof(int);
  char *s="fizzbuzz ",*p,*m;
  memset(a,0,sizeof(a));
  f(b);
  b+=2;
  f(b);
  b--;
  for(i=0;i<n;i++)
    if(a[i]) {
      m = p = strdup(s);
      if(a[i]&8)
        goto pr;
      p+=(a[i]&4);
      p[b]=(!(a[i]&2))<<(b+1);
pr:
      printf("%s\n",p);
      free(m);
    }
    else
      printf("%d\n",i+1);
  return 0;
}

Name: Anonymous 2010-06-09 12:58

Oh well.
The first programming language I learned was TrueBasic, in high school; because the compiler was owned by the school and at the time we didn't have access to things like that as handouts, there was a bit of a gap between that and my first C/C++ course in college.  I say C/C++ because we could never be sure of which of the two we were being taught when we were being taught it.  The only way to find out was to change file names and compilers and see what the computer spit out.  It was that kind of disoriented course.  As can be expected from that, my C and C++ skills are not exactly up to par with the number of years I have been programming.

At the moment, I perform work on a number of Java-based applications as part-time employment.  In my spare time I am reteaching myself assembly (x86) and Python and do website consultation.  I'll return to C at a future date.

public class FizzBuzz
{
   public static void main(String[] args)
   {
      int i = 1;
      for(; i <= 100; ++i)
      {
         if(i%15 == 0)
         {
            System.out.println("FizzBuzz");
         }
         else if(i%3 == 0)
         {
            System.out.println("Fizz");
         }
         else if(i%5 == 0)
         {
            System.out.println("Buzz");
         }
         else
         {
            System.out.println(i);
         }
      }
   }
}

Name: Anonymous 2010-06-09 13:08

Fairly crappy... Been a long time since I've used printf.

#include <stdio.h>

int main(void) {
    for (int c = 1; c < 100;++c)
        printf("%d%n%s%s\n" + ((c % 3) && (c % 5)) ? 0 : 2), c, ((c % 3) ? "" : "Fizz"), ((c % 5) ? "" : "Buzz"), "");
   
    return 0;
}

Name: Anonymous 2010-06-09 13:28

>>50
%n is not what you want.

Name: Anonymous 2010-06-09 13:33

>>15
This is vile. Each of those little fucking IO()s is like a shit covered PHALLUS going IN and OUT of your eye socket with vicious lust.

What's the point of creating an [IO()] if you are simply going to sequence_ it? Might as well execute each IO() as it is created; [1..100] is already in order, FUCK.

Name: Anonymous 2010-06-09 20:57

>>52
that's not how haskell works.

Name: Anonymous 2010-06-09 21:21

python 3:
map(print(("Fizz" if x%3==0 else "")+("Buzz" if x%5==0 else "") or str(x) for x in range(1,101)))

perl 5:
print map {(($_%3?'':'Fizz').($_%5?'':'Buzz') or $_)."\n"}1..100;

Name: Anonymous 2010-06-09 21:35

>>54
map(print(("Fizz" if x%3==0 else "")+("Buzz" if x%5==0 else "") or str(x) for x in range(1,101)))
... IHBT

Name: Anonymous 2010-06-09 21:36

>>54
Way to fail miserably.

Name: Anonymous 2010-06-09 22:39


(defun divisiblep (x n)
  (zerop (mod x n)))

(loop
   for i from 1 to 100 do
   (let ((mod3 (divisiblep i 3))
         (mod5 (divisiblep i 5)))
     (cond
       ((and mod3 mod5) (format t "~&FizzBuzz~&"))
       (mod3 (format t "~&Fizz~&"))
       (mod5 (format t "~&Buzz~&"))
       (t (format t "~&~A~&" i)))))

(loop
  for i from 1 to 100 do
  (let (result)
    (format t "~&~{~A~}~&"
            (progn
              (when (divisiblep i 5) (push "Buzz" result))
              (when (divisiblep i 3) (push "Fizz" result))
              (unless result (push i result))
              result))))


Reposting from the other thread.
Background: A bit of CS education, but even before and after that I was self-thought. Learned C, Pascal, x86 asm, C#, O'Caml, Common Lisp approximatively in that order (other languages which I don't use too often, like PHP, were learned too along the way).

The first FizzBuzz implementation is supposed to be rather standard and efficient, while the second being a more 'clever', but wasteful implementation.

Name: Anonymous 2010-06-10 2:58

>>50
Even if the syntax was messed, I understand what you were trying for (though, I think the %d and %s%s were backwards), it won't work that way. And most compact I could get it was:

#include <stdio.h>

void main(void) {
    int i,useless;
    for (i=1;i<=100;useless=!(printf("%s%s",(i%3?"":"Fizz"),(i%5?(i%3?"":"\n"):"Buzz\n")))?printf("%d\n",i):0,++i);
}

Name: Anonymous 2010-06-10 3:20

Name: Anonymous 2010-06-10 4:43

>>58
I was trying to do it in a single call, not the shortest possible code. It compiled and ran on my machine?

Name: Anonymous 2010-06-10 6:56

>>60
You tried to emulate vocal tone by using a textual question mark? You look like a penis because of it?

Name: Anonymous 2010-06-10 9:34

I am a Second Year Electric and Electronics Engineering student.

import Control.Applicative

main::IO()
main = buzzprint.fizzle $  [1..100]

data FizzBuzzer =None Integer|Fizz|Buzz|FizzBuzz

instance Show FizzBuzzer where
    show (None x) = show x
    show Fizz = "Fizz"
    show Buzz = "Buzz"
    show FizzBuzz = "FizzBuzz"

instance Num Bool where
    negate = not
    (+) = (||)
    (*) = (&&)
    fromInteger x
        |x<=0      = False
        |otherwise = True
    abs x = x
    signum False = -1
    signum _ = 1

buzzprint::[FizzBuzzer]->IO ()
buzzprint= mapM_ print

fizzle::[Integer]->[FizzBuzzer]
fizzle [] = []
fizzle (fb:fbs)
    | fizzbuzz  = FizzBuzz:fizzle fbs
    | fizz      = Fizz:fizzle fbs
    | buzz      = Buzz:fizzle fbs
    | otherwise = None fb:fizzle fbs
        where [fizzbuzz,fizz,buzz]=((:)=<<product) $ map ((==0).(mod fb)) [3,5]

Name: Anonymous 2010-06-10 11:31

I am an EXPERT FIOC PROGRAMMER.

from itertools import cycle
print('\n'.join(map(lambda x: x[1] or x[0], zip(map(str, range(1, 101)), map(''.join, zip(cycle(['', '', 'Fizz']), cycle([''] * 4 + ['Buzz'])))))))

Name: Anonymous 2010-06-10 15:14

>>59
You posted the same thread twice. Please learn to read.

Please see >>2,4

Name: Anonymous 2010-06-10 16:48

>>64
HWBT?

Name: Anonymous 2010-06-10 17:23

FIZZBUZZ MY ANUS

Name: Anonymous 2011-02-04 15:34

Name: Anonymous 2011-02-04 16:17

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