Re:Fizz Buzz in Tensorflow (2016)
by geraldbauer on Monday February 12, 2018 @10:22AM
(#16359134)
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
Re:Fizz Buzz in Tensorflow (2016)
by arcticbull on Monday February 12, 2018 @01:10PM
(#16360515)
Hah, 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),
}
}
}
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).