5 ms·
A girl on irc once asked if there is a way to solve FizzBuzz with just one comparison. Yours is indeed one and it is pretty clever :) Here is another one https:
by ggurgone 11y ago
A girl on irc once asked if there is a way to solve FizzBuzz with just one comparison.
Yours is indeed one and it is pretty clever :)
Here is another one https://gist.github.com/giuseppeg/5939177 https://gist.github.com/giuseppeg/5939177
- adrae5df 11y agoThere are two conditionals in that code.
- ggurgone 11y agoindeed :) it should be: words[c] || i I focused on the bitwise thing and I overlooked that part
- adrae5df 11y ago|| is a conditional. Is first expression true? If it isn't evaluate the second expression.
- ggurgone 11y agosure, what other conditional are you talking about? the for condition?
- adrae5df 11y agoSecond expression in the for loop and ||, both behave as conditionals, i.e. are equivalent to an if statement and the generated instruction will be a branch. At least it would in C. Of course I'm ignoring the fact that JS is interpreted as will cause a lot of additional bloat, but were not counting that.
- ggurgone 11y agoNo doubt about it. The question was about avoiding the if/else within the for loop body. In js the for could be replaced with an array of 100 elems and a forEach: Array.apply(null, { length: 100 }).forEach(function (_, i) { })
- deleted 11y ago[deleted]
- cronin101 11y agoFizzBuzz from 1 to N (supplied as command-line argument) and exiting gracefully without using loops, conditionals, or segfaults - in C: #include <stdio.h> #include <stdlib.h> int i(const int i) { printf("%d\n", i); return i; } int f(const int i) { printf("Fizz\n"); return i; } int b(const int i) { printf("Buzz\n"); return i; } int fb(const int i) { printf("FizzBuzz\n"); return i; } int (* PC[15])(int i) = { i,i,f,i,b,f,i,i,f,b,i,f,i,i,fb }; // Printer Cycle int go(const int arg) { return PC[(arg - 1) % 15](arg) + 1; } int stop(const int arg) { PC[(arg - 1) % 15](arg); exit(0); } int (* LC[2]) (int index) = { go, stop }; // Logic Cycle void recurse(const int i, const int kill_num) { recurse(LC[!(i % kill_num)](i), kill_num); } int main(const int argc, const char *argv[]) { recurse(1, atoi(argv[1])); }
- eCa 11y agoYes, a Perl one-liner: perl -E 'map { say } map { { 3 => "Fizz", 5 => "Buzz", 15 => "FizzBuzz" }->{ sub { my $val = shift; (grep { $val % $_ == 0 } (3, 5, 15))[-1] }->($_) } || $_ } (1..100)' Or a little more readable: map { say } map { { 3 => "Fizz", 5 => "Buzz", 15 => "FizzBuzz" }->{ sub { my $val = shift; (grep { $val % $_ == 0 } (3, 5, 15))[-1] }->($_) } || $_ } (1..100)