4 ms·
Fast. Faster. Fastest. The FizzBuzz Gold Standard. Lookup pre-calculated ("hard coded") constants. Don't over-engineer :-): def fizzbuzz [1, 2, "Fiz
by geraldbauer 8y ago
Fast. Faster. Fastest. The FizzBuzz Gold Standard. Lookup pre-calculated ("hard coded") constants. Don't over-engineer :-):
def fizzbuzz
[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14,
"FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26,
"Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38,
"Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fizz", 49, "Buzz",
"Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62,
"Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74,
"FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86,
"Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98,
"Fizz", "Buzz"]
end
Source: https://yukimotopress.github.io/fizzbuzz#gold-standard https://yukimotopress.github.io/fizzbuzz#gold-standard
- a3n 8y agoPlus a program to produce the pre-calculated data, to avoid typos. :-)
- ben-schaaf 8y agoWhy not both at the same time. All you need is a language with compile-time execution: auto fizzbuzz(size_t n) { return iota(n) .map!((i) { if ((i % 15) == 0) { return "FizzBuzz"; } else if ((i % 3) == 0) { return "Fizz"; } else if ((i % 5) == 0) { return "Buzz"; } else { return i.to!string; } }); } void main() { // enum here forces compile-time execution enum fizzbuzz100 = fizzbuzz(100).join("\n"); writeln(fizzbuzz100); }
- a3n 8y agoOr: $ date +%A Tuesday $ anyfizzbuzzprogram > fizzbuzz100 $ cat fizzbuzz100 [output] $ date +%A Wednesday $ cat fizzbuzz100 [output] :-)
- romwell 8y agoFast? Yes. The Gold Standard? Pshaw. This thread is utterly incomplete without Fizz Buzz Enterprise Edition™[1]. If you want to get straight to the good parts of it, just look at the Factories folder[2]. [1]https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris... [2]https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/tree/master/src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/factories https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...
- rpz 8y agoThat's some top notch programming satire
- Someone 8y agoFastest? We need benchmarks to show that. I can see this being slower than traditional approaches on systems with slow memory, tiny CPU caches, and compilers that don’t do constant string folding. You can also replace the array by a single string. That may or may not be faster, depending on CPU and language implementation. I think it likely is (rationale: the code writing the array to stdout either does multiple write calls, or builds up the string in memory and then does what the code writing the string does. The code writing the array need not load the string, but it needs to store the array, and that won’t be much smaller, even when it shares strings)
- bjoli 8y agoIn racket I would probably use a wheel for a more general solution: (define wheel '(#f #f "fizz" #f "buzz" "fizz" #f #f "fizz" "buzz" #f "fizz" #f #f "fizzbuzz")) (for ((fb? (in-cycle wheel)) (i (in-range 1 101))) (displayln (or fb? i))) It would be more elegant in a lazy language, but it is probably quite fast since it doesn't need any division (?? My assumption here might be wrong). I typed this out on my phone, so it might not work, and racket isn't my daily driver. Edit: My initial assumption was correct, but the in-cycle sequence creator has a very large overhead. The final solution for racket (Ugly, because mlist doesn't have any nice things included): https://pastebin.com/aB2BLY1U https://pastebin.com/aB2BLY1U The mutable list solution (as one would write it in scheme) is more than twice as fast as the remainder one, and I expect it to be for other languages as well.