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. ##
>>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:
Anonymous2010-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:
Anonymous2010-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]
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)
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.
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.
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:
Anonymous2010-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
>>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
}
>>26
Basically Anal Cunt's logo. He got about 78 cents.
Name:
Anonymous2010-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);
>>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/)
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!!MaeNOMadEhM2YD12010-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:
Anonymous2010-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
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:
Anonymous2010-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"), "");
>>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.
(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:
Anonymous2010-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);
}
>>60
You tried to emulate vocal tone by using a textual question mark? You look like a penis because of it?
Name:
Anonymous2010-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]