Fibos
def get_input(cast, text=''):
"""Request user for input and tries to cast input to cast function provided
Args:
cast (function): cast function to use
text (string): text to print so user can know what to type in!
Returns:
casted user input
"""
try:
return cast(input(text))
except:
return get_input(cast)
def fib(total_fibos=0, fibos=None):
"""Returns an array with `total_fibos` elements of the Fibonacci sequence!
Args:
total_fibos (int): number of elements to compute
fibos (array): array of elements previously computed (for recursive purposes)
Returns:
array of integers with `total_fibos` of the Fibonacci sequence.
"""
fibos = fibos or []
fibos_count = len(fibos)
if total_fibos is 0:
return fibos
if fibos_count in [0, 1]:
next_item = fibos_count
else:
next_item = fibos[fibos_count-2] + fibos[fibos_count-1]
fibos.append(next_item)
return fib(total_fibos-1, fibos)
if __name__ == '__main__':
total_fibos = get_input(int, 'How many elements of fibonacci\'s series do you want?: ')
fibos = fib(total_fibos)
# convert array of integers to string and join them with a space
print(' '.join([str(fibo) for fibo in fibos]))