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!!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]