Re:The Cursed Computer Iceberg Meme
by strogonoff on Sunday April 11, 2021 @07:58AM
(#26769673)
After reading “There’s no elegant solution to FizzBuzz”, immediately had to check how I might try to be unnecessarily clever if I had to do it in Python and arrived at:
(In reality, of course, I would likely have had to look up the modulo operator first.)
[ Parent
| on HN ]
Re:The Cursed Computer Iceberg Meme
by inoursweetz on Sunday April 11, 2021 @08:39AM
(#26769939)
There is a nice paper, which explores possible elegant solutions to FizzBuzz.
https://themonadreader.files.wordpress.com/2014/04/fizzbuzz....
Re:The Cursed Computer Iceberg Meme
by strogonoff on Sunday April 11, 2021 @09:08AM
(#26770097)
> FizzBuzz in Haskell by Embedding a Domain-Specific Language
I was not disappointed.
> Meanwhile, we want there to be at most one place that outputs fizz, buzz, or the string representation, and each test to be performed only once.
These do intuitively seem like the properties of an elegant solution.
JavaScript can eliminate all repetition thanks to how its || operator works:
for (let i = 1; i <= 100; i++) {
console.log(
(i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "")
|| i
);
}
Whether or not you consider this more elegant is a matter of taste, though.
Re:The Cursed Computer Iceberg Meme
by strogonoff on Sunday April 11, 2021 @08:47AM
(#26769975)
While I like to think the opposite, I can see myself using || with an empty string occasionally (at least with TS, where I can be reasonably sure the variable will definitely be a string at runtime). However, concatenating strings with + is a bit implicit to my taste.
Re:The Cursed Computer Iceberg Meme
by makerofthings on Sunday April 11, 2021 @10:11AM
(#26770485)
I do it like this.
fizzbuzz n = case (n^4 `mod` 15) of
1 -> show n
6 -> "fizz"
10 -> "buzz"
0 -> "fizzbuzz"
main :: IO ()
main = print $ map fizzbuzz [1..30]
Re:The Cursed Computer Iceberg Meme
by eurasiantiger on Sunday April 11, 2021 @10:46AM
(#26770678)
That is Euler’s FizzBuzz: http://philcrissman.net/posts/eulers-fizzbuzz/
I think it is an elegant solution.
Here's a (perhaps "unnecessarily clever") Python 3 version I wrote a while back, for some reason:
import functools
def fizzbuzz(n):
if not n % 3: yield (r := "fizz")
if not n % 5: yield (r := "buzz")
if not "r" in locals(): yield n
compose = lambda f, g: lambda *args: f(g(*args))
functions = "".join, functools.partial(map, str), fizzbuzz
transform = functools.reduce(compose, functions)
print("\n".join(map(transform, range(1, 100))))I've always thought that an elegant solution wouldn't iterate by ones, but would instead increment the correct amount between each print statement... 0+3+2+1+3+1+2+3+3+2+...
Maybe the fact that there is no elegant solution makes it useful.
My 2 cents:
import itertools
# this block is just for simple functional pipes
class B:
def __init__(self, f): self.f = f
class Pipe (B): __ror__ = lambda self, x: self.f(x)
class Map (B): __ror__ = lambda self, x: map (self.f, x)
class MapValues(B): __ror__ = lambda self, it: it | Map(lambda kv: (kv[0], self.f(kv[1])))
# here the actual FizzBuzz
(
range(1, 100)
| Map(lambda i: (i, itertools.compress(('Fizz', 'Buzz'), (i % 3 == 0, i % 5 == 0))))
| MapValues(''.join)
| Map(lambda kv: kv[1] or kv[0])
| Pipe(list)
)
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, ... ]
if you're interested in pipes like above - here my simple library for that (no install needed, just copy some lines)Euler’s fizzbuzz is elegant http://philcrissman.net/posts/eulers-fizzbuzz/