Lab 3
Due: Friday, Feb. 17 at 11:59pm on Moodle
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.
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!
Test your function on a variety of inputs to make sure it is working the way you want.
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:
Here are a few steps that should help with developing this function:
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.
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:
A few notes about producing similar output:
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:
Example 2:
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):
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!
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: