Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Fibonacci Spiral's End by kdim
from operator import add, sub
from itertools import cycle
def fibo_spiral_end(elem: int) -> list[int]:
x = y = 0
operation = cycle((add, sub, add, add, sub, add, sub, sub))
for i in range(elem):
f = round(1.618033988749895 ** (i + 1) / 5 ** 0.5)
x, y = next(operation)(x, f), next(operation)(y, f)
return [x, y]
print("Example:")
print(fibo_spiral_end(5))
# These "asserts" are used for self-checking
assert fibo_spiral_end(0) == [0, 0]
assert fibo_spiral_end(1) == [1, -1]
assert fibo_spiral_end(2) == [2, 0]
assert fibo_spiral_end(3) == [0, 2]
assert fibo_spiral_end(4) == [-3, -1]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 18, 2023
Comments: