Make a program(in the language of your choice), that goes like this:
1)Prints all numbers from one to one hundred
2)For every number that can be divided with 3, it writes "fizz" next to the number.
3)For every number that can be divided with 5, it writes "buzz".
This a common problem employers give to seperate the Expert programers from the Enterprise Qulity Programmers. Surprisingly, not many programmers can. I made one in BASIC in 5 minutes.
Not tested, but should be good. Written in 7 minutes.
Name:
Anonymous2007-09-01 20:34 ID:qPfamYiZ
####
# This is according to the problem set, but the example
# had the words "Fizz" and "Buzz" switched.
# Less than 3 minutes.
def fizzbuzz():
for num in range(1, 101):
word = " "
if not num % 3:
word += "Fizz"
if not num % 5:
word += "Buzz"
int main ()
{
for (int i = 1; i < 101; i++)
{
cout << i << " ";
if (i %3 == 0)
cout << "Fizz";
if (i%5 == 0)
cout << "Bizz";
cout << endl;
}
return 1;
}
[pre]let f x = (if x `mod` 3 == 0 then "Fizz" else []) ++ (if x `mod` 5 == 0 then "Buzz" else []) in mapM_ (\x -> putStrLn (show x ++ " " ++ f x)) [1 .. 100][/pre]
Name:
Anonymous2007-09-02 0:17 ID:FAq2f+pY
It seems /prog/ is doing fairly well... Another challenge will come soon...
factor: 100 [ 1+ dup 15 mod zero? [ drop "FizzBuzz" ] [ dup 3 mod zero? [ drop "Fizz" ] [ dup 5 mod zero? [ drop "Buzz" ] [ number>string ] if ] if ] if print ] each
Name:
Anonymous2007-09-02 7:46 ID:2Kos+EeY
print '\n'.join((lambda o=('','Fail')[not n%3]+('','Sage')[not n%5]:(o,`n`)[not o])()for n in xrange(1,101))
Untested, but should work.
Name:
Anonymous2007-09-02 7:50 ID:TNwnL0cy
so this is like 4chan but with cranky programmers instead of pictures huh
Name:
Anonymous2007-09-02 8:01 ID:yXa5pFup
>>36 so this is like 4chan
Look at your address bar, cockfuck, this is 4chan.
>>42
True. It will probably work as a first screening test to weed out the ENTERPRISE fuckwits who have an impressive list of certificates and no programming skills whatsoever, though.
>>42
Well, if your employer gives you this problem, he's probably crap.
Name:
Anonymous2007-09-02 9:53 ID:yXa5pFup
>>45
or he reads digg all day to check out WHATZ NEW IN DA BLOGOSFEAR
Name:
Anonymous2007-09-02 10:02 ID:dQk8FlwG
;FIZZBUZZ - DOES STUFF ABOVE
CHROUT EQU $FFD2 ;COMMODORE 64 ROUTINE THAT PRINTS CHARACTER IN .A TO STDOUT; CHANGE ACCORDINGLY FOR OTHER ARCHITECTURES
WHICH .DB 0
POINT3 .DB 0
POINT5 .DB 0
FZBZ LDA #$00
STA WHICH
NEXT INC WHICH
LDA WHICH
JSR PRINTA
LDA #32 ;PRINT A SPACE
JSR CHROUT
LDA WHICH
CMP #101
BEQ GTFO
INC POINT3
LDA POINT3
CMP #3
BNE SKIP1
JSR BUZZ
LDA #0
STA POINT3
SKIP1 INC POINT5
LDA POINT5
CMP #5
BNE SKIP2
JSR FIZZ
LDA #0
STA POINT5
SKIP2 LDA #13 ;PRINT A CR
JSR CHROUT
JMP NEXT
;I'M TOO TIRED RIGHT NOW TO ACTUALLY WRITE OUT THESE ROUTINES
;REST ASSURED THAT DUE TO MY EXPERT PROGRAMMER ABILITY THAT I CAN
FIZZ ;PRINTS FIZZ
BUZZ ;PRINTS BUZZ
PRINTA ;CONVERT NUMBER IN .A TO INTEGER AND PRINT IT, NO NEWLINE
Name:
Anonymous2007-09-02 10:30 ID:G85jbkOo
Python, 1.5 minutes:
def fizzbuzz():
for num in range(1, 101):
out = ""
if num % 3 == 0: out = "Fizz"
if num % 5 == 0: out += "Buzz"
print num, out
Factor, roughly 5 minutes:
: check ( str div num -- ) swap mod 0 = [ write ] [ drop ] if ;
: output ( num -- num ) dup number>string write " " write ;
: fizz ( num -- num ) dup "Fizz" 3 rot check ;
: buzz ( num -- num ) dup "Buzz" 5 rot check ;
: fizzbuzz 1 100 [ output fizz buzz "" print 1 + ] times drop ;
>>61 has a bug, but fixed :)
%a = ('fizz', '$i % 3', 'buzz', '$i % 5');
for $i (1..100) {
eval $b and $c .= $a while ($a, $b) = each %a;
print "$i$c\n" and $c = q{}}
int main(void)
{
int i;
int r = 0;
char buff[] = "FizzBuzz";
for (i = 1; i <= 100; i++) {
sprintf(buff, "%d", i);
if (! (i % 3))
r = sprintf(buff + r, "%s", "Fizz");
if (! (i % 5))
r = sprintf(buff + r, "%s", "Buzz");
puts(buff);
r = 0;
}
Both my posts :)
I just really enjoy writing prolog. It was previously said about another language, but I do think programs written in prolog take longer to think about than to type.
Still you wouldnt want to implement an operating system with it.....
Or would you?
Name:
Anonymous2007-09-03 8:41 ID:KJvW0vyq
So is anyone going to attempt to solve this in constant time using a template function?
Name:
Anonymous2007-09-03 8:49 ID:KJvW0vyq
public class FizzBuzz
{
public static void main ( String [] argv )
{
for( int i=0,j=0,k=0; i <= 100; i++ )
{
System.out.print(i+" ");
if( j == 3 )
{ System.out.print("fizz"); j = 0; }
if( k == 5 )
{ System.out.print("buzz"); k = 0; }
j++;
k++;
System.out.println();
}
}
}
>>82
This doesn't proactively create synergies or maximize profits by following best practices. Make it more ENTERPRISE.
Name:
Anonymous2007-09-03 8:59 ID:KJvW0vyq
/**
* Autor: Anonymous
* Date: 2007-09-03
* Class: FizzBuzz
* Description: Outputs the numbers 1 to 100 on each line
* Whilst writing fizz or buzz if the linenumber is
* divisible by 3 or 5 respectivley.
*/
public class FizzBuzz
{
/* main entry point */
public static void main ( String [] argv )
{
/* For loop from 1 to 100 inclusive */
for( int i=0,j=0,k=0; i <= 100; i++ )
{
System.out.print(i+" ");
if( j == 3 )
{
//Prints fizz when required
System.out.print("fizz"); j = 0; }
if( k == 5 )
{
//Prints buzz when required
System.out.print("buzz"); k = 0; }
//Increment
j++;
k++;
//Print a newline character
System.out.println();
}//EndFor
}//End main
}//End Class: FizzBuzz
>>98
Which is basically what I wrote yesterday, only using more forced indentation of code.
Name:
Anonymous2007-09-03 12:31 ID:KJvW0vyq
>>96
Is this the difference between a Webmonkey and a Codemonkey?
A webmonkey is concerned with how it looks,
Whereas a codemonkey is only concerned with how it works?
Maybe the challenge was not to produce code, but to challenge the requirements. That is what any software engineer, no any self respecting IT professional should do.
(defun fizzbuzz ()
(dotimes (i 100)
(format t "~&~a " (1+ i))
(when (zerop (mod (1+ i) 3)) (format t "fizz"))
(when (zerop (mod (1+ i) 5)) (format t "buzz~%"))))
Fuck yeah format. I wonder if you could do it using only a call to format.
>>108
What errors did gcc give you? (and what version of gcc are you building it with?) I tested it with 3.4.6 when I wrote it, though looking back there are at least a few unnecessary parts, if not something that breaks standards.
# gcc fizzbuzz.cpp
fizzbuzz.cpp:27: error: template header not allowed in member definition of explicitly specialized class
fizzbuzz.cpp:41: error: template header not allowed in member definition of explicitly specialized class
fizzbuzz.cpp:60: error: template header not allowed in member definition of explicitly specialized class
>>110
Huh, that's weird. The only thing I can think of would be to remove the "template <>" from those three lines. I didn't have them there originally, but 3.4.6 bitched at me to put them in.
fizzbuzz.cc:26: error: template header not allowed in member definition of explicitly specialized class
fizzbuzz.cc:40: error: template header not allowed in member definition of explicitly specialized class
fizzbuzz.cc:59: error: template header not allowed in member definition of explicitly specialized class
someone needs to make a language where you can do this: fizzbuzz ( int x ) = x
fizzbuzz ( * 3 int x ) = 'Fizz'
fizzbuzz ( * 5 int x ) = 'Buzz'
fizzbuzz ( * 15 int x ) = 'FizzBuzz'
main = print fizzbuzz range ( 1 100 )
New challange.
Write a simple shell interpreter using fork/exec. It should be able to support simple pipelining and redirection.
Is /prog/ up for the challenge?
>>136
The shell interpreter is to be a little simplistic, all you can do is execute commands, pipe output to other programs as well as redirect input/output. To implement command execution you should use the function call fork() and exec(). Note that there are many variations on the exec() function call. To block the shell, you
should use the wait() (or similar) function call.
You should use dup2 and pipe to redirect stdin and stdout.
Your interpreter should be able to do the following
$ ./foobar
./foobar does not exist.
$ ls –la > out.txt
$ ls -la | wc > foo
>>138
Looool this reeks of homework. Everybody and their mother did this at uni. I could lend you my code, I still have it somewhere, but I won't, lol ;)
Name:
Anonymous2007-09-04 11:05 ID:2GCF1QO3
open Printf
let rec homework x =
printf "%d" x;
if (x mod 3 = 0) then (printf "fizz");
if (x mod 5 = 0) then (printf "buzz");
printf "\n";
if (x < 100) then (homework (x+1))
I've studied CS, I'm working in a well payed position and consider myself more of a designer, most of my code I just fiddle around 'till it works. You know, my knowledge is in theoretical stuff, not so much in coding. But this program was so goddamn easy, you guys should all learn to program better.
String s[] = {null,null,"Fizz",null,"Buzz","Fizz",null,null,"Fizz","Buzz",null,"Fizz",null,null,"FizzBuzz"};
int i = 0;
for (int count = 1; count <=100; count++)
{
if (i < 15)
{
if (s[i] == null)
System.out.println(count);
else
System.out.println(s[i]);
}
else
i = 0;
i++;
}
>>154
I'm not. I'm pretty sure no one gives a shit about how much I lurk, but I'm also pretty sure I can't technically lurk more, as >>152 suggested.
Name:
Anonymous2007-09-04 17:51 ID:O+kYIzQQ
>>123
You seem to be talking about the logical approach to programming.
I mean, rather than defining how the solution works, instead you define the problem, and let the computer work out the solution from what you've told it.
Name:
Anonymous2007-09-04 18:06 ID:NmDJuoHh
for i = 1, 100 do
local j = tostring(i) .. " "
if i % 3 == 0 then j = j .. "Fizz" end
if i % 5 == 0 then j = j .. "Buzz" end
print(j)
end