Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.
Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).
Every function should have at least a docstring at the start that states what it does (see Lesson3 Team Notebook if you need a reminder). Make other comments as necessary.
Make sure that you are running test cases (plural) for everything and commenting on the results in markdown. Your comments should discuss how you know that the test case results are correct.
In part 1 you will define and test a recursive_summation
function. We thought about summation in the Team Notebook, review that info if you need to.
A. Write test cases for your function - fill in the table below before you start coding! Fill in the expected and rationale for all, and more test cases for the extra $n$s below - at least one more should be negative, the rest positive.
n | expected sum | rationale |
---|---|---|
4 | 10 | positive integer, should work, we have already calculated expected |
0 | ||
-1 | ||
n | ||
n | ||
n | ||
n |
B. Define and test a recursive_summation
function that:
recursive_summation
function should: Make sure to run all of the test cases above, leave in the results, and comment on the results.
In [ ]:
In this part, you will define a recursive power
function that takes two numbers as parameters, $x$ and $n$ ($x$ can be any integer, but $n$ should be positive), calculates and returns $x^n$. Use the following recursive definitions:
$x^n = (x^{n/2})^2 \hspace{20mm} \text{when n is even}$
$x^n = x \cdot(x^{(n-1)/2})^2 \hspace{8.5mm} \text{when n is odd}$
$x^0 = 1 \hspace{30mm} \text{when n is 0}$
Further considerations:
if-elif-else
in your recursive function. C. Write test cases for your function - fill in the table below before you start coding! Fill in the expected and rationale for all and more test cases for the extra $x$s and $n$s below.
x | n | expected | rationale |
---|---|---|---|
3 | 0 | ||
3 | 1 | ||
3 | 2 | ||
3 | 3 | ||
-3 | 3 | ||
3 | -3 | ||
x | n | ||
x | n | ||
x | n | ||
x | n |
D. Design a recursive power
function using the above equations, by answering the following questions:
E. Define and test a recursive power
function that meets all the criteria above and correctly executes all of the tests. Make sure to comment on your results.
Copy and paste your recursive factorial
function below:
In [ ]:
F. Before we use it inside another function, we need to clean it up.
Copy, paste, your recursive factorial
function again below and edit so that it:
None
for negative numbers
In [ ]:
G. test the new version of factorial
with:
comment on the results.
In [ ]:
Review the information on permutations and combinations from preactivity
H. Design and write a new function gene_permutations
that:
total
number of genes in the organism ($n$)genes
that you want to consider ($k$)genes
from total
factorial
function Write, predict the results of, and provide a rationale for at least 3 test cases for the above function before you write the code.
Provide your pseudocode here (this can be short):
In [1]:
# Provide your Python code here:
I. write a new function gene_combinations
that:
total
number of genes in the organism ($n$)genes
that you want to consider ($k$)genes
from total
factorial
functionWrite, predict the results of, and provide a rationale for at least 3 test cases for the above function before you write the code.
Provide your pseudocode here (this can be short):
In [ ]:
# Provide your Python code here: