pouët.net

questions for programmers interview

category: code [glöplog]
Code:for($i=1;$i<=100;$i++) echo ((!($i%3))?"Fizz":((!($i%5))?"Buzz":$i))."\n";
rasmus: did you actually try that :P
added on the 2012-07-02 08:21:19 by Gargaj Gargaj
yes? not sure if the output was supposed to look like this. But it's just a conversion of what tomaes did.

Code:1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 Fizz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 Fizz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 Fizz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 Fizz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 Fizz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
You're missing all the FizzBuzz'es in that one, Rasmus ;)
added on the 2012-07-02 08:59:34 by Punqtured Punqtured
Mhmm.
added on the 2012-07-02 08:59:36 by tomaes tomaes
How about this. :)

Code: #include <stdio.h> int main( int argc, char ** argv ){ int k = 0142, i = (~~!'\0'<<0xA)-(!'\0'<<5)+(!'\0'<<3); char s[] = {k, k+4, k+7, k+19, k+24, !!(k-~~k)}, f[*s-0x5d], b[(!i|!i-~!-03)*5]; f[!s] = s[!!k]; f[1?1&!!i+!i:!i] = s[1<<1]; f[(!!k^!k)<<!!k] = *(s+(0x400>>010)); f[3] = s[(-1^!-1)&(!!~1<<2)]; f[1^!-1+01^~-1+1<<2-!'?'] = *(s+!!~0+2*2); b[!s] = s[!f]; b[0?0:!!'Z'] = s[-1&0x3]; b[~0*-1<<1] = *(f+!0*2); b[3] = f[1^!-1+2]; b[~!+04?04:04] = s[~0&0x10-~~0xb]; while(i--) (!(i%15))?printf("%s%s\n",f,b):(!(i%5))?puts(b):(!(i%3))?puts(f):""; }
added on the 2012-07-02 09:00:59 by tomaes tomaes
That's what I'd call badly obfuscated. And it's not even coming from f2c since there are no gotos :)
added on the 2012-07-02 09:16:42 by 216 216
There's insane and then there's evil. ;)
But you're right. Every version that uses mod gives too much away. Feel free to do a "good" obfuscated one. I collect them and put them up on github later today. :)
added on the 2012-07-02 10:11:34 by tomaes tomaes
I think tomaes won the thread.
added on the 2012-07-02 10:45:06 by kusma kusma
This is going mental!
added on the 2012-07-02 11:02:09 by TLM TLM
C++ compilers are fun (look at the disassembly output :)

Code: #include <stdio.h> template <int A, int B> inline void recurse() { if (B>A) { recurse<A,(A+B)/2>(); recurse<(A+B)/2+1,B>(); } else { if (!(A%15)) { printf("fizzbuzz\n"); } else if (!(A%3)) { printf("fizz\n"); } else if (!(A%5)) { printf("buzz\n"); } else printf("%d\n",A); } } int main() { recurse<1,100>(); return 0; }
added on the 2012-07-02 12:29:05 by kb_ kb_
Code:for($i=1;$i<=100;$i++) echo ((!($i%15))?"FizzBuzz":((!($i%5))?"Buzz":((!($i%3))?"Fizz":$i)))."\r\n";
i'm sorta considering making one based on the principle of that both "Fizz" and "Buzz" are dword-sized, or just jumping around in a "FizzBuzz" string, but i dont think they'll obfuscate that well.
added on the 2012-07-02 12:43:09 by Gargaj Gargaj
I was going to do one that runs on a shader that displays fizzbuzz as a texture, based on the color (index, 0 to 100) of the underlying pixels, but on second thought no.
added on the 2012-07-02 12:48:07 by Navis Navis
templates, bah humbug:
Code: #ifndef TENS #define PASTE_HLP(a, b) a##b #define PASTE(a, b) PASTE_HLP(a, b) #define STRFY_HLP(a) #a #define STRFY(a) STRFY_HLP(a) #include <stdio.h> int main(void) { puts( #define TENS #include "fizzbuzz.c" #define TENS 1 #include "fizzbuzz.c" #define TENS 2 #include "fizzbuzz.c" #define TENS 3 #include "fizzbuzz.c" #define TENS 4 #include "fizzbuzz.c" #define TENS 5 #include "fizzbuzz.c" #define TENS 6 #include "fizzbuzz.c" #define TENS 7 #include "fizzbuzz.c" #define TENS 8 #include "fizzbuzz.c" #define TENS 9 #include "fizzbuzz.c" #define TENS 10 #include "fizzbuzz.c" ); return 0; } #elif !defined ONES #define ONES 0 #include "fizzbuzz.c" #define ONES 1 #include "fizzbuzz.c" #define ONES 2 #include "fizzbuzz.c" #define ONES 3 #include "fizzbuzz.c" #define ONES 4 #include "fizzbuzz.c" #define ONES 5 #include "fizzbuzz.c" #define ONES 6 #include "fizzbuzz.c" #define ONES 7 #include "fizzbuzz.c" #define ONES 8 #include "fizzbuzz.c" #define ONES 9 #include "fizzbuzz.c" #undef TENS #else #define CTR PASTE(TENS, ONES) #if CTR>0 && CTR<=100 #if CTR%15==0 "fizzbuzz" #elif CTR%5==0 "buzz" #elif CTR%3==0 "fizz" #else STRFY(CTR) #endif #if CTR<100 "\n" #endif #endif #undef ONES #endif
added on the 2012-07-02 13:54:54 by 216 216
haha :-D
added on the 2012-07-02 14:15:13 by kusma kusma
216: how about adding that one here? :)
added on the 2012-07-02 14:19:18 by kusma kusma
skrebbel: your design pattern wankfest made me all wet! the LINQ code i didn't even understand, seems like some SQL-ish thingy?
added on the 2012-07-02 14:32:18 by earx earx
I put something up: 7 versions from the very reasonable to the very unreasonable. :)

https://github.com/tomaes/fizzbuzz/blob/master/fizzbuzz.c
added on the 2012-07-02 14:54:37 by tomaes tomaes
fizzbuzz.com (74 bytes)
Code:40 43 D1 CB 72 FC 60 93 BA 36 01 A9 48 12 75 1B B2 40 A9 20 04 75 14 B2 3C 48 74 0F 93 F6 74 48 05 30 30 89 44 46 3C 31 80 D2 0A B4 09 CD 21 61 40 3C 64 76 CD C3 46 69 7A 7A 0A 24 46 69 7A 7A 42 75 7A 7A 0A 24 00 00 0A 24
added on the 2012-07-02 17:54:58 by rrrola rrrola
like always crazy stuffs from rrrola !!!
added on the 2012-07-02 18:20:53 by panic panic
File dumper + starter for rrrola's version. ;)

Code: char c[] = { 0x40, 0x43, 0xD1, 0xCB, 0x72, 0xFC, 0x60, 0x93, 0xBA, 0x36, 0x01, 0xA9, 0x48, 0x12, 0x75, 0x1B, 0xB2, 0x40, 0xA9, 0x20, 0x04, 0x75, 0x14, 0xB2, 0x3C, 0x48, 0x74, 0x0F, 0x93, 0xF6, 0x74, 0x48, 0x05, 0x30, 0x30, 0x89, 0x44, 0x46, 0x3C, 0x31, 0x80, 0xD2, 0x0A, 0xB4, 0x09, 0xCD, 0x21, 0x61, 0x40, 0x3C, 0x64, 0x76, 0xCD, 0xC3, 0x46, 0x69, 0x7A, 0x7A, 0x0A, 0x24, 0x46, 0x69, 0x7A, 0x7A, 0x42, 0x75, 0x7A, 0x7A, 0x0A, 0x24, 0x00, 0x00, 0x0A, 0x24 }; FILE * file = fopen ("fizzbuzz.com","wb"); if (file != NULL) { fwrite (c, 1, sizeof(c), file ); fclose (file); } system("fizzbuzz.com");
added on the 2012-07-02 19:24:51 by tomaes tomaes
rrrola: Is the code public domain? Because I like to throw it into fizzbuzz.c and maybe compress/modify it a little more later on. :)
added on the 2012-07-02 19:41:01 by tomaes tomaes
moar compile-time FizzBuzz
Code: import std.stdio, std.conv; void main() { enum string ctfeFizzBuzz = (){ string res = ""; for (int i = 1; i <= 100; ++i) { if (0 == i % 15) res ~= "FizzBuzz"; else if (0 == i % 3) res ~= "Fizz"; else if (0 == i % 5) res ~= "Buzz"; else res ~= to!string(i); res ~= "\n"; } return res; }(); writeln(ctfeFizzBuzz); }
added on the 2012-07-02 21:35:07 by ponce ponce

login