Programming Bootcamp 2016

Lesson 2 Exercises


Earning points (optional)

  • Enter your name below.
  • Email your .ipynb file to me (sarahmid@mail.med.upenn.edu) before 9:00 am on 9/13.
  • You do not need to complete all the problems to get points.
  • At the end of the course, everyone who gets at least 90% of the total points will get a prize (bootcamp mug!).

Name:


1. Guess the output: conditionals practice (1pt)

For the following blocks of code, first try to guess what the output will be, and then run the code yourself. Points will be given for filling in the guesses; guessing wrong won't be penalized.

NOTE: The first cell below holds the variables that will be used in the problems. Since variables are shared across cells in Jupyter notebooks, you just need to run this cell once and then those variables can be used in any other code cell.

(However, remember that closing the notebook or restarting the kernel clears all the variables, so you will need to re-run the cell to re-populate those variables after doing that.)


In [ ]:
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
a = True
b = False
x = 2
y = -2
cat = "Mittens"

(^^ you might find it helpful to copy these variables somewhere you can easily see them when doing the problems. e.g. a piece of paper or a text file.)


In [ ]:
print a

Your guess:


In [ ]:
print (not a)

Your guess:


In [ ]:
print (a == b)

Your guess:


In [ ]:
print (a != b)

Your guess:


In [ ]:
print (x == y)

Your guess:


In [ ]:
print (x > y)

Your guess:


In [ ]:
print (x = 2)

Your guess:


In [ ]:
print (a and b)

Your guess:


In [ ]:
print (a and not b)

Your guess:


In [ ]:
print (a or b)

Your guess:


In [ ]:
print (not b or a)

Your guess:


In [ ]:
print not (b or a)

Your guess:


In [ ]:
print (not b and a)

Your guess:


In [ ]:
print not (b and a)

Your guess:


In [ ]:
print (x == abs(y))

Your guess:


In [ ]:
print len(cat)

Your guess:


In [ ]:
print cat + x

Your guess:


In [ ]:
print cat + str(x)

Your guess:


In [ ]:
print float(x)

Your guess:


In [ ]:
print ("i" in cat)

Your guess:


In [ ]:
print ("g" in cat)

Your guess:


In [ ]:
print ("Mit" in cat)

Your guess:

What is "in"? This is just another Python operator that you can use in your conditionals. As you may have guessed, (x in y) evaluates to True if x is found in y. This can be used to check if a string is contained in another string (e.g. "Mit" in "Mittens").


2. Guess the output: if statement practice (1pt)

Same directions as above.


In [ ]:
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
x = 2
y = -2
cat = "Mittens"

In [ ]:
if (x % 2) == 0:
    print "x is even"
else:
    print "x is odd"

Your guess:


In [ ]:
if (y - 4*x) < 0:
    print "Invalid!"
else:
    print "Banana"

Your guess:


In [ ]:
if "Mit" in cat:
    print "Hey Mits!"
else:
    print "Where's Mits?"

Your guess:


In [ ]:
x = "C"
if x == "A" or "B":
    print "yes"
else:
    print "no"

Your guess:


In [ ]:
x = "C"
if (x == "A") or (x == "B"):
    print "yes"
else:
    print "no"

Your guess:

Surprised by the last two? It's important to note that when you want compare a variable against multiple things, you can only compare it to one thing at a time. Although it makes sense in English to say "is x equal to A or B?", in Python you must write: ((x == "A") or (x == "B")) to accomplish this. The same goes for e.g. ((x > 5) and (x < 10)) and anything along those lines.

So why does the first version give the answer "yes"? Basically, anything that isn't False or the literal number 0 is considered to be True in Python. When you say 'x == "A" or "B"', Python reads it as '(x == "A") or (B)', which evaluates to '(False) or (True)', which is True!


3. On your own (2pts)

Write code to accomplish the following tasks using the concepts we went over in the lecture.

Use the following variables in your code. You must actually use the variables; pretend you don't know their values.


In [ ]:
# RUN THIS BLOCK FIRST!
num1 = 3.14159
num2 = 6
num3 = 100
num4 = 6 * -5 - 4 * 2 + -7 * -8 + 3
motif = "GTAAGTCGGTAACGTAAGTCGGTAAC"

(A) Check if num3 is greater than num4. Print "yes" if it is, and "no" if it isn't.


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 25. Print "yes" if it is, and "no" if it isn't.


In [ ]:

(D) Round num1 to the nearest hundredth.


In [ ]:

(E) Check if num4 is positive or negative.


In [ ]:


4. Quadratic formula: checking for negative roots (1pt)

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 the code below so that it checks for this potential error before it occurs:

  • If the error is going to occur, print a message saying "non-real answer" and do not calculate the values of x.
  • If the error is not going to occur, calculate and print the values of x.

In [ ]:
a = float(raw_input("Enter value for a: "))
b = float(raw_input("Enter value for b: "))
c = float(raw_input("Enter value for c: "))

underRoot = (b**2 - 4*a*c)

x1 = ( (-b) + underRoot ** 0.5 ) / float(2*a)
x2 = ( (-b) - underRoot ** 0.5 ) / float(2*a)

print "x =", x1, "or", x2

[ Check your answer ]

  • If a=1, b=4, and c=1, expected output is:
    x = -0.267949192431 or -3.73205080757
  • If a=2, b=2, and c=2, expected output is:
    non-real answer

5. Motif checker (2pts)

Using raw_input(), prompt the user for a DNA sequence and a motif to search for (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 check if the motif is longer than the sequence. If it is, print "Motif is too long".


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"). If it is found, print "Motif found". If it is not found, print "Motif not found".


In [ ]:

[ Check your answer ] Try running your code above using the following input and make sure your results match the expected output.

Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif: ATG
Expected output:

Motif not found

Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif: ATCGA
Expected output:

Motif found

Sequence: CTAGCC
Motif: ATGGCTAGCTA
Expected output:

Motif is too long

6. Password protection (3pts)

Write code that prompts the user to guess a password and checks whether the password is correct.

  • First create a variable called password and set it equal to whatever you want the password to be (any string).
  • Then prompt the user to guess the password, and read their input from the terminal.
  • Check if what they entered matches your password, and print a message saying whether or not the guess was correct.

(A) Do this giving the user only one chance to guess.


In [ ]:

(B) Do this giving the user 3 chances to guess. Only prompt for another guess if the previous guess was incorrect. When they guess correctly, print a confirmation message and end the program. If they don't guess correctly in 3 tries, print "Access denied!" and end the program.


In [ ]:


7. Coin flip simulation (2pts)

For this problem, we will simulate a coin flip using the random number generator in the random module.

Try running the following code several times:


In [ ]:
import random

randNum = random.randint(0,1)
print randNum

You should see that you always get either 0 or 1. We will pretend that each time we run random.randint(0,1) we are flipping a coin, where 1 represents heads and 0 represents tails.

Write code that "flips a coin" 10 times and counts how many times it comes up heads. At the end, print how many of the flips were heads.


In [ ]:



Extra problems (0pt)

The following problems are for people who would like more practice. They will not be counted for points.

(A) Following from the coin flip problem above:

Change your code so that the coin is now "unfair" (i.e. the chance of heads is not equal to the chance of tails). See if you can make it so the probability of heads is 75%.

Hint: There are several ways to do this, but one simple way involves using a larger range of random ints and assigning heads/tails differently.


In [ ]:

(B) Using raw_input(), prompt the user for a number. Check if that number is between 50 and 100, and print different messages depending on the outcome.


In [ ]:

(C) Using raw_input(), prompt the user for an integer. Check if the integer is a multiple of 7, and print different messages depending on the outcome.


In [ ]: