Coder Social home page Coder Social logo

dsc-recursive-functions-lab-nyc01-dtsc-ft-062220's Introduction

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
# 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, [8], [9, 10]] would become [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] or [1, 2, [3, 4, [5]]] 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!

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

Depth vs Breadth First Search

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

# Your answer here

Summary

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

dsc-recursive-functions-lab-nyc01-dtsc-ft-062220's People

Contributors

lindseyberlin avatar lmcm18 avatar loredirick avatar mathymitchell avatar sumedh10 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.