{ "cells": [ { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "solution": false } }, "source": [ "Consider the following piece of code:\n", "\n", "```python\n", "def f(x):\n", " if x == 0 or x == 1:\n", " return x\n", " return f(x - 1) + f(x - 2)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "solution": false } }, "source": [ "---\n", "## Part A (1 point)\n", "\n", "Describe, in words, what this code does, and how it does it." ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "part-a", "points": "1", "solution": true } }, "source": [ "This function computes the fibonnaci sequence using recursion. The base cases are $x=0$ and $x=1$, in which case the function will return 0 or 1, respectively. In all other cases, the function will call itself to find the $x-1$ and $x-2$ fibonnaci numbers, and then add them together." ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "solution": false } }, "source": [ "---\n", "## Part B (2 points)\n", "\n", "For what inputs will this function not behave as expected? What will happen?" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": true, "grade_id": "part-b", "points": 2, "solution": true } }, "source": [ "The function will not work correctly for inputs less than zero. Such inputs will result in an infinite recursion, as the function will keep subtracting one but never reach a base case that stops it." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }