Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook 05_Python-II_assignment/Lesson2_exercises.ipynb

Views: 54
Kernel: Python 2 (SageMath)

Lesson 2: In-class exercises


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):

# 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.

if (num1 > 2*num2): print "yes" else: print "no"
no

(B) Print a random integer between num2 and num3.

import random print random.randint(num2,num3)
60

(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.

if len(motif) <= 5: print "yes" else: print "no"
no

(D) Round num1 to the nearest hundredth.

print round(num1,2)
3.14

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.

a=-2 b=2 c=1 delta=b**2-4*a*c if delta >=0: print "x=",(delta**0.5-b)/(2*a),"or",(-delta**0.5-b)/(2*a) else: print "non-real anwser"
x= -0.366025403784 or 1.36602540378
---
# Homework exercise (10 Points)
---

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)

sequence = raw_input("DNA sequence") motif = raw_input("motif") if len(sequence) > len(motif): print len(sequence), len(motif) else: print "Sequences should be longer than motif"
DNA sequenceAGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG motifATG 49 3

(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)

sequence = raw_input("DNA sequence") motif = raw_input("motif") if len(sequence) > len(motif): if motif in sequence: print "Motif is found in DNA sequence." else: print "Motif is not found in DNA sequence" else: print "Sequences should be longer than motif"
DNA sequenceAGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG motifATG Motif is not found in DNA sequence

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))