Instructions: For each problem, write code in the provided code block. Don't forget to run your code to make sure it works.
1. Warm up review for lesson 2
Write code to accomplish the following tasks using the concepts from Lesson 2. These exercises will use the following variables (make sure to run this block of code first to initiate these variables):
In [ ]:
# RUN THIS BLOCK FIRST!
num1 = 3.14159
num2 = 6
num3 = 100
motif = "GTAAGTC"
(A) Check if num1
is greater than twice num2
. Print a different message depending on the outcome.
In [ ]:
(B) Print a random integer between num2
and num3
.
In [ ]:
(C) Check if the length of the string stored in motif
is less than or equal to 5. Print a different message depending on the outcome.
In [ ]:
(D) Round num1
to the nearest hundredth.
In [ ]:
2. Quadratic formula: checking for negative roots
Recall that when calculating the quadratic formula, you will get an error if b^2-4ac
is negative, since you can't take the square root of a negative number.
Edit your quadratic formula program from Lesson 1 so that it checks for this potential error before it occurs. If the error is going to occur, print an informative message saying "non-real answer" or similar, and do not calculate the values of x. If the error is not going to occur, continue on to calculating and printing the values of x as you did before.
In [ ]:
Motif Checker
Prompt the user for a DNA sequence and a motif to search for using raw_input()
(the sequence and motif can be any string of A/T/G/C's; see below for examples).
(A) Find the length of the motif and length of the DNA sequence and make sure the motif is shorter than the sequence. If it is not, print an informative error message. (5 Points)
In [ ]:
(B) Adding to your code from part (A): If and only if the motif is shorter than the sequence, go on to check if the motif can be found somewhere within the sequence (hint: use "in
"). Print a message saying whether it was found or not. (5 Points)
In [ ]:
Example input:
Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif: ATG (This should pass the check from part (a) but not part (b))Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif: ATCGA (should pass both checks)Sequence: CTAGCC
Motif: ATGGCTAGCTA (code should not pass the check from part (a))