Skip to content

CS5001-khoury/hw07

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Homework 07 - Submission

  • Student: YOUR NAME
  • Semester: SEMESTER

Common

  • List any coding practice problems you completed, feel free to comment at needed (did it help, was it too easy, etc):

  • List any other resources you used to help you complete the assignment (make sure to share good ones to the Tips,Tricks, and Resources channel in teams!):

Module Specific

  1. When you fail to add a base case in recursion, you will return a error value (crashing your program). What is the error value? If you don't know, try running the following code...
def factorial(n):
    return n * factorial(n-1)
  1. Describe a base case in your own words. What is the base case needed for the code above?

  2. Thinking about for-in loops, they all work on sequential data, so based on that, what is each "item" for each of these sequential data types. Separate each item by a comma after the => symbol.

    • Example: range(1, 3) => 1, 2
    • ('aloha', 'world') =>
    • ['aloha', 'world'] =>
    • 'aloha world' =>
  3. For this for-in loop, write an equivalent while loop.

for i in range(1, 10, 2):
    print(i)
# your code here

Deeper Thinking

The fibonacci sequence is a very famous sequence found in nature. It is defined as the sum of the previous two numbers in the sequence. The first two numbers are 0 and 1. So the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, etc. If you do a quick search online, you will find that it can be written both recursively and iteratively.

For this deeper thinking, you will work on an experiment. Write a recursive fibonacci function and an iterative fibonacci function. Then, time how long it takes to run each function for the first 30 fibonacci numbers. You can use the following code to time your functions.

import time

def fibonacci_iterative(n):
    # your code here
    pass

def fibonacci_recursive(n):
    # your code here
    pass


def time_function(func, *args):
  # Get the start time
  start = time.time()
  # Call the function with the given arguments
  result = func(*args)
  # Get the end time
  end = time.time()
  # Calculate the elapsed time
  elapsed = end - start
  # Return the result and the elapsed time
  return result, elapsed

def main():

    print("Fibonacci(10) =", time_function(fibonacci_iterative, 10))
    print("Fibonacci(20) =", time_function(fibonacci_iterative, 20))
    print("Fibonacci(30) =", time_function(fibonacci_iterative, 30))

    print("Fibonacci(10) =", time_function(fibonacci_recursive, 10))
    print("Fibonacci(20) =", time_function(fibonacci_recursive, 20))
    print("Fibonacci(30) =", time_function(fibonacci_recursive, 30))

Report on your results here:

Which one takes longer? Why do you think that is?

About

Homework 07, focusing on recursion

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages