by jamesdhutton · 6y ago · 0 comments
from itertools import cycle
fizz = cycle(['', '', 'Fizz'])
buzz = cycle(['', '', '', '', 'Buzz'])
for n in range(1, 100):
fizzbuzz = next(fizz) + next(buzz)
print (fizzbuzz if fizzbuzz else n) <T> Iterator<T> cycle(List<T> list) {
return IntStream.iterate(0, x -> (x + 1) % list.size()).mapToObj(list::get).iterator();
}
<T, U, V> Iterator<V> zip(Iterator<T> it1, Iterator<U> it2, BiFunction<T,U,V> merger) {
return Stream.generate(() -> merger.apply(it1.next(), it2.next())).iterator();
}
var fizz = cycle(List.of("Fizz", "", ""));
var buzz = cycle(List.of("Buzz", "", "", "", ""));
var ints = IntStream.iterate(0, x -> x + 1).boxed().iterator();
var fizzbuzz = zip(
zip(fizz, buzz, String::concat),
ints, (f, i) -> f.isEmpty()? "" + i: f);
Stream.generate(fizzbuzz::next).skip(1).limit(100)
.forEach(System.out::println);That's my version to make it more "functional":
from itertools import cycle, islice
from operator import add
fizz = cycle(['', '', 'Fizz'])
buzz = cycle(['', '', '', '', 'Buzz'])
strings = map(add, fizz, buzz)
strings_or_nos = (s or i for i, s in enumerate(strings, start=1))
print(list(islice(strings_or_nos, 100))) from itertools import islice, cycle
print("\n".join(islice((
f + b if f or b else str(i+1)
for i, (f, b) in enumerate(
zip(cycle(["", "", "Fizz"]), cycle(["", "", "", "", "Buzz"]))
)), 100)))https://gist.github.com/kbob/f46776fdc950e6a948b04c76bc23e87...
from itertools import cycle
fizz = cycle([lambda x: ''] * 2 + [lambda x: 'Fizz'])
buzz = cycle([lambda x: ''] * 4 + [lambda x: 'Buzz'])
numb = cycle([lambda x: x] * 2 + [lambda x: '', lambda x: x, lambda x: ''])
for n in range(1, 100):
print(next(fizz)(n) + next(buzz)(n) + next(buzz)(n))