# Recursive Functions lab

This is a fun (in my opinion) little lab that I wrote on recursive functions. It was eventually removed from the curriculum as it was deemed too challenging, but I attest that wrapping your head around recursive functions is an indispensible concept for all programmers, especially aspiring data scientists. I hope you find these problems to help push you along that path. You can also check out the answers to this lab here .

# Recursive Functions - Lab

## Introduction

Now that you've seen a little preview of recursive functions, it's time to give them a try!

## Objectives

You will be able to:

• Describe the concept of a recursive function
• Create a recursive function
• Demonstrate how local scope changes with recursive functions
• Compare depth first versus breadth first searches

## Fibonacci

The Fibonacci sequence starts off: 1,1,2,3,5,8,13,21,34,...

Each number is the sum of the two preceding. Write a recursive function that calculates the nth number of the Fibonacci sequence. For example, our sequence above would correspond to:

fib(1) = 1 #The 1st element in the sequence is 1

fib(2) = 1 #The 2nd element in the sequence is 1

fib(3) = 2 #The 3rd element in the sequence is 2

fib(4) = 3 #The 4th element in the sequence is 3

fib(5) = 5 #The 5th element in the sequence is 5

fib(6) = 8 #The 6th element in the sequence is 8

fib(7) = 13 #The 7th element in the sequence is 13

fib(8) = 21 #The 8th element in the sequence is 21

fib(9) = 34 #The 9th element in the sequence is 34

In [ ]:
# Your code here
def fib(n):
return nth_number


## Flat list

Write a function that takes a nested list and flattens it to a list of ints, floats and strings. For example the nested list [1, [2, 3, [4, 5, 6]], 7, , [9, 10]] would become [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] or [1, 2, [3, 4, ]] would become [1, 2, 3, 4, 5].

Note: Be careful how you initialize your function! See this link for some potential pitfalls you could encounter if you're not careful!

In [ ]:
# Your code here
def flat_list(L):
return flattened


Did you use breadth or depth first recursive calls above? Explain.

In [ ]:
# Your answer here


## Summary

Well done! Recursive functions are an advanced topic in Python and you got some good practice tackling classic problems here.