# Initialize Otter
import otter
grader = otter.Notebook("hw7-loops.ipynb")

Homework 7 - Loops and Flow Control#

This assignment includes three problems on the topic of loops.

Entering Your Information for Credit#

To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

# Please provide your first name, last name, Drexel ID, and Drexel email. Make sure these are provided as strings. "STRINGS ARE TEXT ENCLOSED IN QUOTATION MARKS."

# In the assignments you will see sections of code that you need to fill in that are marked with ... (three dots). Replace the ... with your code.
first_name = ...
last_name = ...
drexel_id = ...
drexel_email = ...
grader.check("q0-Checking-Your-Name")

Question 1: Fibonacci Sequence

Fibonacci was born sometime around 1170 in the Kingdom of Pisa in what is now Italy. The sequences we now refer to as Fibonacci sequences first appeared in his treatise Liber Abaci from 1202 as a model of how rabbit populations grow.

A Fibonacci sequence is a list of integers in which the \(n\text{th}\) entry \(x_n\) is computed from:

\[ x_n = x_{n-1} + x_{n-2} \]

To initiate a Fibbonaci sequence, one must provide values for \(x_0\) and \(x_1\).

For example, if \(x_0 = 0\) and \(x_1 = 1\), the first 10 terms of the sequence are:

\[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 \]

Your task is to implement a function that takes in two starting values and a length and returns the resulting Fibonacci sequence.

Write python code to do the following:

  • Define a function called fibonacci which accepts 3 input arguments: two starting values x0 and x1, and a sequence length L

  • Your function should return a list containing the first L fibonacci numbers starting with x0 and x1

  • You may assume the value L >= 2

Your code replaces the prompt: ...

...
Ellipsis
grader.check("q1-fibonacci")

Question 2: Brute-Force Equation Solver

Numerous engineering and scientific applications require finding solutions to a set of equations. For example, the following system of equations:

\[ 8x + 7y = 38 \]
\[3x - 5y = -1 \]

have a solution: \( x = 3\), \(y = 2\).

You will implement a function that acts as a simple equation solver. For systems of equations of the form:

\[ ax + by = c \]
\[ dx + ey = f \]

You will use the following brute-force algorithm to find an integer solution for x and y in the range \([-10, 10]\) (if such a solution exists):

Notes:

  • You may assume that if a solution exists, there is only one solution. Your function should return the first solution found using the algorithm outlined above

  • Elegant mathematical techniques exist to solve such linear equations. However, for other kinds of equations or situations, brute-force approaches can be handy.

Write python code to do the following:

  • Define a function called eq_solver which accepts 6 input arguments, a, b, c, d, e, and f the coefficients for two equations

  • Your function should return a tuple of integers, containing solution values for (\(x\),\(y\))

  • If no solution was found, your function should return a string: "no solution found"

Your code replaces the prompt: ...

...
Ellipsis
grader.check("q2-equation-solver")

Question 3: Convert to Binary

A natural number’s binary representation tell which powers of \(2\) sum to produce the number.

For instance: \(6\) in binary is \(110\), since: $\( 6 = (1)2^2 + (1)2^1 + (0)2^0 \)$

(\(110\) are the coefficients of the descending powers of \(2\).)

Your task is to write a function that takes in a positive integer and returns a string containing its binary representation. The following algorithm can be used to generate a natural number’s binary representation:

Reminders:

  • β€œx modulo 2”, written in python as x % 2, returns the remainder when dividing by two, either 0 or 1

  • Integer division written in python as, x // 2, truncates any remainder.

    • For example 5 // 2 = 2 since the remainder is truncated

  • Use str() to convert a number to a string

Write python code to do the following:

  • Define a function called to_binary that takes an input positive integer x

  • Using the algorithm described in pseudocode above, compute the binary representation of x

  • Return the binary as a string of "1"s and "0"s

Your code replaces the prompt: ...

...
Ellipsis
grader.check("q3-binary")

Submitting Your Assignment#

To submit your assignment please use the following link the assignment on GitHub classroom.

Use this link to navigate to the assignment on GitHub classroom.

If you need further instructions on submitting your assignment please look at Lab 1.

Viewing your score#

It is your responsibility to ensure that your grade report shows correctly. We can only provide corrections to grades if a grading error is determined. If you do not receive a grade report your grade has not been recorded. It is your responsibility either resubmit the assignment correctly or contact the instructors before the assignment due date.

Each .ipynb file you have uploaded will have a file with the name of your file + Grade_Report.md. You can view this file by clicking on the file name. This will show you the results of the autograder.

We have both public and hidden tests. You will be able to see the score of both tests, but not the specific details of why the test passed or failed.

Note

In python and particularly jupyter notebooks it is common that during testing you run cells in a different order, or run cells and modify them. This can cause there to be local variables needed for your solution that would not be recreated on running your code again from scratch. Your assignment will be graded based on running your code from scratch. This means before you submit your assignment you should restart the kernel and run all cells. You can do this by clicking Kernel and selecting Restart and Run All. If you code does not run as expected after restarting the kernel and running all cells it means you have an error in your code.

Fin#