4 ms·
Good 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,
by geraldbauer 9y ago
Good 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), } } }