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
12
Upvotes
View all comments
Show parent comment
1
u/jP_wanN May 27 '16
I assume
xrangeactually allocates a container of indexes then? I basically did the same thing in C++ because a normal index-based loop would use mutation, but I didn't actually use the data in the loop (only the offset of each element from the first element in the container) and chose the smallest available data type, so it works with 231 but requires 2GiB RAM for that and takes forever for the output – I think it was somewhere between 10 and 30 minutes without writing the output to the console or disk (I think the output would be bigger than 20GB).Would love to see a version that uses some sort of stream instead of a statically allocated range to get around the mutation. Or only call the output function a single time and recurse to collect the full output into one string, as you would probably do in Haskell.