CSC 111: Intro to Computer Science through Programming

Lab 3

Due: Friday, Feb. 17 at 11:59pm on Moodle

In this lab we will practice several key concepts (conditionals and Boolean variables, string methods, lists, and modules). In the last two parts of the lab, you'll put these concepts together to create two programs inspired by biological applications.

For this lab, first find your randomly assigned partner. Introduce yourselves - the person with the first name that comes first alphabetically should begin as the "driver", with the other partner as the "navigator". The driver will have the code open, and the navigator will have these instructions open.

During this lab we will be switching more frequently. At the end of the lab, email the code and transcript to the person who started as the navigator. Both partners should submit the same code on Moodle.

Part A: Conditionals

First create a new file called lab3A-C.py. Since we are starting to develop longer programs, for this lab keep all your code in files, only using the shell for testing. Still save all your shell content to submit at the end.

Inside this first file, create a function called valentine() (slightly belated!) Ask the user a question and then save the answer using a variable. Using an if/elif/else block, see if the answer is "yes", "maybe", or "no" and respond accordingly. An example is shown below. Feel free to change the question, add answer types, and/or change the responses. Thank you to Sarah Sorensen for this program!

valentine

Test your function on a variety of inputs to make sure it is working the way you want.

Switch Driver and Navigator here!

Part B: String methods

Inside the same file as Part A, create a new function that will display some information about a string. Call this function count(string, character). This function takes in two arguments, and will determine how many times character appears in string. A few examples are shown below:

count1

Here are a few steps that should help with developing this function:

  1. Find the length of the string first, using the built-in len(...) method. Then print out the length to the user, in the same format as shown above.

  2. Next, set up a variable to keep track of the number of times the given character occurs in the given string. Initially, assign this variable the value of 0.

  3. After that, set up a for loop that goes over each character in the string. For each character, use an if-statement to test whether it is equal to the given character. If yes, update your counting variable by 1. What should you do if it is not equal to the given character?

  4. Finally, print out the number of occurrences, in the format shown above.

You have actually just implemented another string function (we have seen split and replace; this new one is called count). Try out the code below in the shell:

count2

The goal of implementing this function from scratch is to shown you that the built-in methods in Python are not necessarily special or overly complicated. It's good to know where they come from and that you can add further functionality through your own methods.

Switch Driver and Navigator here!

Part C: math and random modules

Still in the same file, create a new function called angle(num_angles). This function takes one argument, the number of angles. Inside, it generates that number of angles randomly, in the range 0-360. We will interpret these angles as being in degrees. The program then converts the angle to radians (use the math module for pi). Finally, using the sin method in the math module, take the sin of the angle (should this be in degrees or radians?) An example with 5 angles is shown below:

angle

A few notes about producing similar output:

  • To create the random angles, you can either use randint(...) or uniform(...) (think about the difference between these functions). For this function I'm not too worried about the endpoints being inclusive or exclusive, either way is fine.

  • If you use uniform(...), round the angle to the nearest degree when printing.

  • Round the angle in radians to two decimal places, and the sin of the angle to three decimal places.

  • Make sure you are using a for-loop so that a flexible number of angles can be accommodated.

  • Format the output as shown in the picture above.

Switch Driver and Navigator here!

Part D: Random DNA sequences

For this part, create a new file called lab3D.py. In this file, create a main method and invoke the main method at the end of your code. The goal for this part is to print some random DNA sequences based on user inputs. We'll call these DNA sequences genes, although these random sequences would probably not be found naturally. However, with new technology, it is actually possible to order custom DNA sequences online, so it's possible the sequences you are generating could actually be created.

  1. First, inside your main method, create a list of the four DNA bases as shown below:

    bases
  2. Then ask the user for the number of bases for each gene. Using a for-loop, select a random base from the list of bases and add it onto the gene using string concatenate. When you are building up a string in this way, it is often helpful to define a gene variable as the empty string before the for-loop. At the end of this step, you should be able to produce one gene:
    randomGenes0

  3. Now we are going to use the INSIDE-OUT nested for-loop technique. The idea is that you first develop the inner for-loop (which we just did in the previous step). Then create an outer for-loop that will create multiple genes. Then we will indent the entire inner for-loop, and also any surrounding code that should be inside the outer loop (in this case: setting the gene variable to the empty string and printing the finished gene).

    For this part, pay close attention to the indentation level of each line of your code. Nested for-loops are very powerful, but it's also easy to get one line off and then achieve unexpected results. If everything went well, you should be able to see output like this:

    Example 1:

    randomGenes1

    Example 2:

    randomGenes2

  4. Note that your DNA sequences will be different since you're using random bases (think about it like rolling a 4-sided die for each new base). Make sure you're not missing any bases in your output!

Switch Driver and Navigator here!

Part E: Comparing DNA sequences

For this final part you will implement a procedure that is actually very common in genetics: comparing two DNA sequences. In this example we will compare two (fictitious) sequences: one from a human and one from a chimpanzee. Similar methods are used to determine when humans and chimps diverged (somewhere in the range 10-12 million years ago).

For this question, create a new file lab3E.py, with a main method. Inside the main method, include the two DNA sequences (copy/paste okay):


    human = "ATA?CAAGACCTCGTTATTAATACGGCGCCATGTGAGTAATCCTATC?GA"
    chimp = "ATAACAAGAGCTAGTTATTA?TACTGCGCCATGTGAGAAATCCTATAGGA"
The goal is to use a for-loop to count how many bases are the same, how many are different, and how many are unknown (represented by a question mark in either sequence). Use three variables to keep track of these different counts. Think about how to structure your conditional control statements to create an elegant program. I would recommend first testing the unknown case, then the different case, then the same case. But there are multiple ways of accomplishing this task. In the end, when your program is run you should get these results (no user input required):

human_chimp

Note: this is an artificially high number of differences. Also note: just from looking at the sequences, it is difficult to tell what the right answer should be. An actual human genome is 60 million times longer than this sequence, so a computer is absolutely necessary to determine the number of differences!

Transcript and Submit

From the shell, highlight all the testing you have done and all the output (since the beginning of lab) and copy it into a plain text file (.txt extension). On Windows you can use the program Notepad (under Accessories) and on Mac you can use the program TextEdit (under Applications). For Mac, if it doesn't have an option to save as txt, go to "Format" -> "Make Plain Text", then save again. Save this file as lab3_transcript.txt.

Make sure that both you and your partner have a copy of all the code written during this lab, and the transcript. Both partners should submit the same files:

  • lab3A-C.py

  • lab3D.py

  • lab3E.py

  • lab2_transcript.txt
on Moodle. If you did not finish, you can either meet to finish or finish separately. All labs must be submitted by Friday night. If you finish early, I encourage you to start on Homework 3, but you are also welcome to depart.