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: 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

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