Ingenious FizzBuzz solution
-
We were talking on TDWTF IRC channel about the FizzBuzz test and out of curiosity I gave the test to my wife who is not a professional programmer. She didn't know the modulus syntax of PHP (her choice) so she came up with this gem:
<?php $i = 1; while ($i <= 100) { if ((strlen($i/3) < 3) && (strlen($i/5) < 3)) { echo "FizzBuzz\n"; } else if (strlen($i/3) < 3) { echo "Fizz\n"; } else if (strlen($i/5) < 3) { echo "Buzz\n"; } else echo $i."\n"; $i++; } ?>
For the disbelievers it does work: http://codepad.org/9KcyveJF
I'm so proud!
-
@hifi said in Ingenious FizzBuzz solution:
She didn't know the modulus syntax of PHP
I don't think I've ever seen a language where it wasn't
%
, other than assembly languages and languages where there is no modulus operator.
-
@ben_lubar said in Ingenious FizzBuzz solution:
@hifi said in Ingenious FizzBuzz solution:
She didn't know the modulus syntax of PHP
I don't think I've ever seen a language where it wasn't
%
, other than assembly languages and languages where there is no modulus operator.It's the only language she knows and she has never needed the modulus operator for anything she does with PHP.
-
@hifi said in Ingenious FizzBuzz solution:
@ben_lubar said in Ingenious FizzBuzz solution:
@hifi said in Ingenious FizzBuzz solution:
She didn't know the modulus syntax of PHP
I don't think I've ever seen a language where it wasn't
%
, other than assembly languages and languages where there is no modulus operator.It's the only language she knows and she has never needed the modulus operator for anything she does with PHP.
You should tell her that
?>
is optional at the end of a file. When I found that out, I used it every time, sinceWarning: Cannot modify header information - headers already sent by [...]
can happen due to whitespace at the end of a file after the?>
-
I'd suggest asking her to think about how she'd modify it so that it'll work all the way up to 1,000, but I have a feeling I already know how that would turn out...
-
@ben_lubar said in Ingenious FizzBuzz solution:
I don't think I've ever seen a language where it wasn't %
"didn't know the modulus syntax of PHP" is not mutually-exclusive with "didn't know about modulus at all", Ben.
-
@ben_lubar The Oracle has heard your plea, and has come up with an answer!
-
This post is deleted!
-
Using
strlen()
to determine what to print? Brillant!
-
This reminds me how in the beginning I thought that the logical not operator in C++ was
~
instead of!
(~
inverts all bits,!
inverts truthiness - so~1
still evaluates to true)
-
@adynathos said in Ingenious FizzBuzz solution:
This reminds me how in the beginning I thought that the logical not operator in C++ was
~
instead of!
(~
inverts all bits,!
inverts truthiness - so~1
still evaluates to true)~x
is the same as-x - 1
, so~1
evaluates to-2
.
-
@adynathos Did you start in Matlab? It had to be unique and use
~
and~=
for bools and comparisons.
-
@ben_lubar said in Ingenious FizzBuzz solution:
~x
is the same as-x - 1
, so~1
evaluates to-2
.Only on a two's-complement machine. Which is almost all modern systems. ;)
However, it's a very useful identity if you're working with infinite-precision integers, as those are internally a lot more like one's-complement systems; they tend to keep the sign bit separate from the value as the representation is otherwise much easier to work with in operations like addition and multiplication. Which is OK until you need bitwise negation…
-
@ben_lubar It's probably not that in Common Lisp, though I'm not going to check.
-
@magus said in Ingenious FizzBuzz solution:
@ben_lubar It's probably not that in Common Lisp, though I'm not going to check.
Does Common Lisp even have operators? I thought it was just a bunch of lists that got transformed into function calls.
-
@ben_lubar Yes, but they're weird:
(+ 2 3)
is just as valid as(+ 2 3 78 34 95)
-
@mzh No, I started with C++, but didn't know what the negation operator is.
And in school classes they wrote negation as~
, so I thought its the same in programming :P
-
@magus said in Ingenious FizzBuzz solution:
Yes, but they're weird
No, they're just (variadic) functions.
-
@mzh said in Ingenious FizzBuzz solution:
@adynathos Did you start in Matlab? It had to be unique and use
~
and~=
for bools and comparisons.Fun fact: our data science team is starting in R, where the assignment operator is
<-
.
-
Another fun fact: in Standard ML, negation was
~
because the language parser couldn't cope with any operator having two fixities and precedences, so-
was stuck being subtraction. But as compensation you could make your own operators with their own totally new symbols, still a relatively rare capability among programming languages to this day.
-
@adynathos said in Ingenious FizzBuzz solution:
@mzh No, I started with C++, but didn't know what the negation operator is.
And in school classes they wrote negation as~
, so I thought its the same in programming :PThey didn't use ¬?
-
@dreikin said in Ingenious FizzBuzz solution:
@adynathos said in Ingenious FizzBuzz solution:
@mzh No, I started with C++, but didn't know what the negation operator is.
And in school classes they wrote negation as~
, so I thought its the same in programming :PThey didn't use ¬?
My keyboard doesn't have one of those.
-
@anotherusername said in Ingenious FizzBuzz solution:
@dreikin said in Ingenious FizzBuzz solution:
@adynathos said in Ingenious FizzBuzz solution:
@mzh No, I started with C++, but didn't know what the negation operator is.
And in school classes they wrote negation as~
, so I thought its the same in programming :PThey didn't use ¬?
My keyboard doesn't have one of those.
Neither does mine (unless I use an alternate key set like US International).
¬
.
-
@dreikin said in Ingenious FizzBuzz solution:
¬
Never seen a programming language that allowed you to use escape codes for its own operators.
-
@anotherusername said in Ingenious FizzBuzz solution:
@dreikin said in Ingenious FizzBuzz solution:
¬
Never seen a programming language that allowed you to use escape codes for its own operators.
There's plenty of time left to make one!
-
@dkf said in Ingenious FizzBuzz solution:
But as compensation you could make your own operators with their own totally new symbols, still a relatively rare capability among programming languages to this day.
One might think that's for a good reason.
-
@heterodox said in Ingenious FizzBuzz solution:
One might think that's for a good reason.
Not as much as you might think. It's quite difficult to do (it requires a different design of parser, AIUI) but once you've done the parse, you're just working with a normal function call tree just as you effectively would be with normal operators (which might then be inlined for efficiency reasons). When the purpose of the language is specifically to enable “interesting” custom parsing of things (SML was designed to support building theorem provers — especially for ones that work on the more intractable forms of logic such as involving higher-order types — so it needed to support embedding of different languages within itself) the effort required to support the complex parser is justifiable.
There's a few other ways to achieve similar effects, but they mostly come down to some form of compile-time processing of non-parsed strings.
-
@anotherusername said in Ingenious FizzBuzz solution:
@dreikin said in Ingenious FizzBuzz solution:
¬
Never seen a programming language that allowed you to use escape codes for its own operators.
Those are just for slackers who can't be bothered to use trigraphs.
-
@dkf said in Ingenious FizzBuzz solution:
Not as much as you might think. It's quite difficult to do (it requires a different design of parser, AIUI) but once you've done the parse, you're just working with a normal function call tree just as you effectively would be with normal operators (which might then be inlined for efficiency reasons).
Yeah, you're speaking exclusively from the computer's perspective. I think it'd be hard for a human to parse if each project in the language can have a different set of operators.