CS21 Lab 3: If/Else

Due Saturday, September 29, before midnight


Make sure all programs are saved to your cs21/labs/03 directory. Files outside this directory will not be graded.

$ update21
$ cd ~/cs21/labs/03/

Programming Tips

Topics for this assignment


1. Dot Dash (OPTIONAL warm-up problem)

Write a program in dot_dash.py that prompts the user for a string and then prints a symbol between each letter based on the length. If the length of the string is even, put a dot (“.”) between each letter, as well as at the beginning and the end. If the length of the string is odd, use a dash (“-”). Here are a few examples of how your program should work:

$ python3 dot_dash.py
Enter a string: HELLO

-H-E-L-L-O-

$ python3 dot_dash.py
Enter a string: Swarthmore

.S.w.a.r.t.h.m.o.r.e.

Hint: You can use python’s mod operator (%) to determine whether one number is evenly divisible by another number. There are also other ways of determining even/odd status.


2. Bad Password

You are in charge of helping Swat students create better passwords, to secure the lab computers. Rather than make a new password for each student by hand, you’ve decided to create an algorithm to do it automatically. Write a program in password.py that asks the user for their current password, and then generates a new password with these modifications:

In the examples below user input is shown in bold.

$ python3 password.py
Enter your password: swarthmore
Your new password is 5war1hm0r3

$ python3 password.py
Enter your password: password
Your new password is pa55w0rd

$ python3 password.py
Enter your password: jello
Your new password is j3110

You can assume that all text will be entered in lowercase letters.

Note: this kind of character-replacement scheme usually doesn’t make passwords that are much better than plain words. Raw string length is what makes a password really secure.

Credit: xkcd


3. Pythagorean Triples

A Pythagorean triple is a set of three positive integers a, b, and c such that the sum of the squares of two of the numbers equals the square of the third. i.e.

a2 + b2 = c2

In this example, a and b represent the shorter sides of a right triangle and c represents the hypotenuse.

Write a program in pythagoras.py that determines whether or not three numbers entered by the user form a valid Pythagorean triple. The numbers can be in any order. If the numbers do form a Pythagorean triple, print out a message that includes the hypotenuse (longest side). If not, print out a helpful message. Here are a few examples of how your program should work:

$ python3 pythagoras.py
Enter side 1: 4
Enter side 2: 5
Enter side 3: 3
Pythagorean triple with hypotenuse 5

$ python3 pythagoras.py
Enter side 1: 13
Enter side 2: 5
Enter side 3: 12
Pythagorean triple with hypotenuse 13

$ python3 pythagoras.py
Enter side 1: 8
Enter side 2: 4
Enter side 3: 9
Not a Pythagorean triple!

Hint: there are four cases to consider. First consider the case when side 1 is the hypotenuse. Then consider side 2 and side 3. If none of these cases work, then the numbers do not form a valid triple.


4. Comparing DNA sequences

An essential task in bioinformatics is comparing DNA sequences from different species. Among other things, these comparisons help scientists determine the “tree of life”. In this question you will first generate two random DNA sequences, then compare them. First ask the user for the length of the DNA sequences and a level of similarity between the two species (this can be either high, medium, or low). Species with high similarity share a lot of their DNA sequences (i.e. human and chimp), and species with low similarity share less of their DNA (i.e. human and tomato).

After asking these two questions, generate the first species DNA sequence using the letters A, C, G, T. Hint: for each base (up to the user’s chosen length) use random.choice(<seq>)). Then generate the second DNA sequence using the first sequence as a template, but with some modifications:

Here is an example. Suppose the user chose 6 bases, with a medium similarity between the species. And then say the first sequence was randomly generated as follows:

GCTAAG # sequence for species 1

To generate the next sequence, we will keep all the Gs and Ts, and randomly re-chose for As and Cs. Depending on the random number generator, we could end up with a variety of sequences for species 2. For example:

GCTAAG # sequence for species 1
GTTCTG # sequence for species 2 with 3 bases the same

or

GCTAAG # sequence for species 1
GATACG # sequence for species 2 with 4 bases the same

In the second example one of the As did not change, we just happened to choose A again randomly.

After generating the two DNA sequences, print them for the user. Then determine how many bases are the same. A few examples are shown below:

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 40
Similarity between the species: high

species1: AAGCCCAATAAACCACTCTGACTGGCCGAATAGGGATATA
species2: GGGCCCCATACGCCACTCTGCCTGGCCGATTGGGGTTGTC
29 / 40 = 0.725 similar

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 40
Similarity between the species: medium

species1: AAGCCCAATAAACCACTCTGACTGGCCGAATAGGGATATA
species2: GGGCAACGTACATGTGTCTGGGTGGCGGACTCGGGCTTTT
21 / 40 = 0.525 similar

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 40
Similarity between the species: low

species1: AAGCCCAATAAACCACTCTGACTGGCCGAATAGGGATATA
species2: GGCAACGATCATGTGCTGTGCGTACCCTTGTCGACATGTT
16 / 40 = 0.4 similar

Note: you should test your program in a wide variety of situations. However, if you want to generate the output above specifically, you can set the python random number generator to start out with a specific “seed”. For the output above, I used 42. To replicate this, put the seed at the top of main:

def main():
    random.seed(42)

STEPS FOR DNA———————————————————–

We recommend that you use the following incremental development steps to gradually build up your DNA program.

  1. First just build up the first species by randomly choosing bases from either a string or a list (Hint: use random.choice(<seq>)). Here are a few examples using seed 7:
$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 5
species1: GCTAA

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 10
species1: GCTAAAGACA
  1. Then create the second species, assuming the similarity is high. Here are the same two examples, but with this next step:
$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 5
species1: GCTAA
species2: GCTAG

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 10
species1: GCTAAAGACA
species2: GCTATTGACC
  1. After you have created the two species, use another loop to compute how similar they are, checking each base at a time. Now our examples have another line:
$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 5
species1: GCTAA
species2: GCTAG
4 / 5 = 0.8 similar

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 10
species1: GCTAAAGACA
species2: GCTATTGACC
7 / 10 = 0.7 similar
  1. Finally, add in asking the user to choose a similarity. Then add the cases when the similarity is medium and low. Here is the length 10 example with medium and low similarity:
$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 10
Similarity between the species: medium
species1: GCTAAAGACA
species2: GATTTAGCAT
4 / 10 = 0.4 similar

$ python3 dna.py
Welcome to DNA comparison!
Number of bases of DNA: 10
Similarity between the species: low
species1: GCTAAAGACA
species2: ATTTACATAA
3 / 10 = 0.3 similar

OPTIONAL EXTENSIONS FOR DNA———————————————

Optional: try to accommodate an arbitrary number of species. Ask the user how many species they would like. Then build up the first species as before. Then use a loop to build up each additional species. But this time, use a random number generator to see which bases to change (instead of using the identity of each base). At the end, compare all the species pairwise.


5. Answer the Questionnaire

Each lab has a short questionnaire at the end. Please edit the QUESTIONS-03.txt file in your cs21/labs/03 directory and answer the questions in that file.


Turning in Your Labs

Don’t forget to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.