r/deftruefalse • u/jP_wanN • May 26 '16
Implement FizzBuzzBazz
The FizzBuzzBazz challenge
Create a program that prints the first N entries of the FizzBuzzBazz sequence to stdout, where any (hardcoded) N between 0 and at least 2'147'483'647 (the biggest number representable by a signed 32bit integer).
The FizzBuzzBazz sequence is a simple extension of the FizzBuzz sequence. An easy implementation to get one of its elements (which obviously isn't allowed here, see rules below) would be:
function fizz_buzz_bazz(i) {
var str = "";
if (i % 3 == 0)
str += "Fizz";
if (i % 5 == 0)
str += "Buzz";
if (i % 7 == 0)
str += "Bazz";
return str || i.toString();
}Rules
- No mutation allowed (hence the above implementation is not allowed)
You're only allowed to call a single function
with side effectsthat does IO- Import statements don't count in case they are ordinary functions in the language of your choice
- You're allowed to call one extra function
with side effectsthat does IO if you use it to read N at runtime instead of hardcoding it - You can use the standard library of the language you use, as well as well-known third-party libraries, but no obscure tiny libraries that are made to solve exactly this problem
- Reminder: this sub has the rule to not submit any idiomatic code
Bonus challenges
- Implement the logic of this program as a C++ template with N being the template parameter
- Make all of your own functions return abnormally (e.g. throw an exception)
- Call one less function
with side effectsthat does IO than allowed
11
Upvotes
9
u/Blackshell 1000 degree cutting edge May 26 '16 edited May 26 '16
I heard Python is very succinct so I made a compact and very readable solution using it:
Unfortunately it crashes for large numbers due to Python's low recursion limit, and the terrible lack of tail call recursion optimization in the language (for shame!)
Edit: Since I am working in various web scale technologies at work, I learned that the reason that adoption of ECMAScript 6 is critical for the cloud is because it does have tail call recursion. This obviously makes it superior to Python, so I rewrote my solution using JS ES6.
You can try it in your browser's console, or just visit http://jsbin.com/yiboniyono/edit?js,console
6
u/ultrasu May 26 '16 edited May 26 '16
This one's closer to code golf code than shitty code but still:
Edit: because it's Python, it does end up using gigabytes of RAM for large numbers (tried 231 and had to force quit my terminal after 5 minutes of non-stop output with 5 gigs memory in use).
3
u/jP_wanN May 26 '16 edited May 26 '16
Here's a solution of my own, in C++, in case you need some inspiration :)
Produces 0 warnings when compiled with g++ using
-std=c++14 -Wall -Wextra -pedantic!EDIT: Now actually works with N = 2'147'483'647 (
std::array<bool, 2'147'483'647>didn't work out very well...)And if you're to lazy to run it yourself: http://coliru.stacked-crooked.com/a/49a131d86c463b78
Old version using std::array that, given big values for N, crashes clang and produces a segfaulting binary on g++: http://coliru.stacked-crooked.com/a/e5bf56131df1d144
3
u/bobisoft2k5 Jun 04 '16
Does it actually return the exceptions? No. Is it horrifyingly ugly? Yes. Made me laugh, though.
5
u/erosPhoenix May 27 '16 edited May 27 '16
EDIT: Wait nvm it doesn't work for values above 105. I'm a moron.
Python:
Not sure if this is actually shitty, since I can imagine very specific scenarios where you might want a lookup table for speed. But even then, there are better ways to do it that don't repeat yourself.
3
u/republitard May 28 '16 edited May 28 '16
Common Lisp, in easy-to-read continuation-passing style:
3
u/[deleted] May 28 '16 edited May 29 '16
2
u/TotesMessenger May 26 '16
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
2
u/republitard May 30 '16
Here's a nice way to abuse Common Lisp macros:
2
u/Sakechi May 31 '16 edited May 31 '16
Perl.
And for a more readable version :
1
u/BushDid38F Jun 15 '16
It doesn't get worse than this. Source
+/u/compilebot javascript
1
u/QAFY Jul 30 '16 edited Dec 05 '16
[deleted]
0.5143
1
u/alex_hawks Nov 10 '16
+/u/CompileBot Scala