4 ms·
Love the write-up. FYI: I've published a (free online) book(let) about FizzBuzz titled "FizzBuzz by Example - There's More Than One Way To Do It" [1] in the Yuk
by geraldbauer 9y ago
Love the write-up. FYI: I've published a (free online) book(let) about FizzBuzz titled "FizzBuzz by Example - There's More Than One Way To Do It" [1] in the Yuki & Moto Press Bookshelf series.
[1] http://yukimotopress.github.io/fizzbuzz http://yukimotopress.github.io/fizzbuzz
PS: My favorite FizzBuzz version - the gold standard with constant lookup:
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
- LanceH 9y agoI took a wild stab at fizzbuzz and got lucky: https://play.golang.org/p/Q7Ek05QjOP https://play.golang.org/p/Q7Ek05QjOP
- dweekly 9y agoThis is brilliant and evil: LuckyFizz! Quick nit: FizzBuzz is supposed to print the number if not printing Fizz, Buzz, or FizzBuzz. So the output is not correct.
- buzer 9y agoSeems like all of the examples (short of precomputed table) use modulo. There are couple of other ways to deal with it as well. Two that I came up with at one point: 1. Create your own mod function (you can most likely leave out dealing with negatives and overflows as long as you just point that out). Given dividend X and divisor Y, while X>=Y: X=X-Y and finally return X. 2. Take human approach and convert the number to string. Skip 0/print it depending on definition. If the last character is 0 or 5, it's dividable by 5. For dividable by 3, you loop thru the string and sum the numbers. Repeat until the length of string is 1. Now compare it to 3, 6 and 9 (you can convert it back to number or just compare the string of those). If it's true, it's dividable by 3. Not nearly as optimized as mod-based solution or precomputed tables, but they show it's possible to solve it even if you are not familiar with modulo (it's not very commonly used in some higher level languages).
- geraldbauer 9y agoGood point. Another example without modulo in the FizzBuzz by Example book is using cycle e.g.: def fizzbuzz fizzy = [nil, nil, :Fizz].cycle buzzy = [nil, nil, nil, nil, :Buzz].cycle (1..100).map do |n| "#{fizzy.next}#{buzzy.next}"[/.+/] || n end end
- arcticbull 9y agoHah, you beat me to it, I just wrote this up in Rust. fn main() { let mut seq_mod_3 = [false,false,true] .into_iter() .cycle(); let mut seq_mod_5 = [false,false,false,false,true] .into_iter() .cycle(); for i in 1..101 { let divisible_by_3 = *seq_mod_3.next().unwrap(); let divisible_by_5 = *seq_mod_5.next().unwrap(); match (divisible_by_3, divisible_by_5) { (true , true ) => println!("fizzbuzz"), (true , false) => println!("fizz"), (false, true ) => println!("buzz"), (false, false) => println!("{}", i), } } }
- dictum 9y agoThat's the test suite, isn't it?