r/deftruefalse • • 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 effects that 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 effects that 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 effects that does IO than allowed
11 Upvotes

29 comments sorted by

View all comments

1

u/BushDid38F Jun 15 '16

It doesn't get worse than this. Source

+/u/compilebot javascript

function FizzBuzzBazz (n) {
	for (i = 1; i < n; i++) {
		var div3 = false;
		var div5 = false;
		var div7 = false;
		
		// Sums the digits until less than 10
		string = i.toString();
		while (string.length > 1) {
			var temp = 0;
			for (j = 0; j < string.length; j++) {
				temp += +string[j];
			}
			string = temp.toString();
		}
		// if the result is 0, 3, 6 or 9 it can be divided by 3
		if (string == 0 || string == 3 || string == 6 || string == 9) {
			div3 = true;
		}
		
		// if last digit is 5 or 0 is can be divided by 5
		string = i.toString();
		if (string[string.length - 1] == 0 || string[string.length - 1] == 5) {
			div5 = true;
		}

		// doubles last digit and then substracts that from the others until less than 10
		string = i.toString();
		while (string.length > 1 && !string.startsWith("-")) {
			string = Math.abs(+string.slice(0,string.length-1) - +string[string.length - 1]*2).toString();
		}
		// 
		if (string == 0 || string == 7) {
			div7 = true;
		}

		var output = "";
		if (div3) {output += "Fizz"}
		if (div5) {output += "Buzz"}
		if (div7) {output += "Bazz"}
		if (output == "") {output = i}
        console.log(output);
	}
}

FizzBuzzBazz(100);