CPSC 301, Term 2, Winter 2015–2016 Lab 04 Practice with Strings and Conditionals

Due: Wednesday, Feb 10, 9:00am MARK: / 16

Name Submitting Number Student Account Name Partner Number Account Of student Email submitted the lab Before Lab Approximate In Lab time to finish After Lab

1. Objectives After this lab you will be able to:

 Design functions which use if / elif / else to execute different commands depending on input data values.

 Design functions which can work with strings.

 Design functions with default parameters.

 Design functions which produce screen output only when told to do so.

 Trace or summarize code which uses the features above.

 Use Online Python Tutor to trace code.

CPSC 301: Lab 04 1 Version: 5-Apr-18 2. [/4] Working with Strings in Different Ways In this section we play around a little with strings and conditionals.

1) [/1] Before lab: Download working_with_strings.py from the course website to your Lab04/ directory. Trace the following function calls; in other words, pretend to be Python and perform the steps that Python would. Note that the operator string1 in string2 returns True if string1 is a substring of string 2 and returns False otherwise. (Of course you can just run the function to determine its return value and output, but we recommend that you practice your tracing skills before you have to do something similar on an exam.) What is the return value and screen output for each of the function calls?

Call: work('abcdefg', 'cd') Return value: Screen output (output generated by Print()):

Call: work('12345', 'Mary had a little lamb.') Return value: Screen output:

2) [/1] Before lab: Here is a draft description for the return value of function work() (our summary includes only information about the return value; it purposefully ignores any output generated by print()).

Returns the number of copies (rounded up) of the shorter input string which would be required to create a string which is longer than the longer input string.

Is this description a reasonable summary of the return value of work()? If not, correct it and/or add to it (you may continue to ignore output generated by print()):

CPSC 301: Lab 04 2 Version: 5-Apr-18 Answer:

3) [/1] Before lab: Design two more example calls which test the other branches of the conditionals within the function work(), and provide their corresponding output and return values.

Call 1: Return value: Screen output:

Call: Return value: Screen output:

4) [/1] In lab: Open the Online Python Tutor’s visualization page. Copy the definition of the work() function into the code box and then add line(s) so that the function is called with the second example from step (1) above: work('12345', 'Mary had a little lamb.'). Set the Online Python Tutor’s parameters as described in the lecture (eg: “Python 3.3”(on top), “hide exited frames ”, “render all objects on the heap”, “draw pointers as arrows”). Click the “visualize execution” button. Show the TA the state of the memory and the output window just after the return statement is executed (while the local frame is still visible).

3. [/5] Income Tax Function British Columbia’s income tax is progressive, meaning that the tax rate on taxable income increases as the taxable income increases. The rates for the 2015 tax year are posted (note that this webpage includes rates from multiple years, but we will use the 2015 rates for this exercise). In this section you will design a Python function bc_income_tax_2015() that

CPSC 301: Lab 04 3 Version: 5-Apr-18 takes as input the taxable income and returns the tax. Both values should be rounded to the nearest cent.

1) [/2] Before lab: Work through the first four steps of the function design recipe (type contract, header, description, examples) to create a docstring for the function bc_income_tax_2015(). Be sure to design examples which test each of the tax brackets. Save the header and docstring as bc_income_tax_2015.py in your Lab04/ subdirectory.

2) [/1] In lab: Work through step five of the function design recipe: create the body. Use if / elif / else statements to implement the various tax brackets. Use an assert statement to ensure that the precondition on the taxable income holds. Use round() to ensure that the return value is rounded to the nearest cent. Put your body together with the header and docstring in bc_income_tax_2015.py in your Lab04/ subdirectory.

3) In lab: Save your file. Work through step six of the function design recipe: test your function with your examples. Click the green arrow (the “Run file”) button above Spyder’s editor to load your function into the Python console. The first time you click this arrow, you may see the “Run Settings” popup window; just click “OK” or “Run” to accept the defaults. (Normally this popup only appears the first time you run a file. If you want to change the settings at a later time, you can click Run > Configure in Spyder’s menu or press F6 in Windows). If you discover an error and have to fix your Python function, be sure to save the file and click the green arrow again before performing a new test (otherwise the Python console will continue to use the old version of the function).

4) [/2] In lab: Show the TA that you can:

a) Copy & paste your examples from the docstring into the Python interpreter and see that they return the correct results.

b) Demonstrate an AssertionError by making a function call which violates the precondition on the taxable income.

4. [/7] DNA Replication Deoxyribonucleic acid (DNA) is a complex molecule used by living organisms to encode genetic information that controls the function of most cellular processes. DNA molecules (or strands) are long polymer chains made up of nucleotide units, each of which is composed of a backbone and one of four bases: adenine (A or a), cytosine (C or c), guanine (G or g) or thymine (T or t).

CPSC 301: Lab 04 4 Version: 5-Apr-18 DNA typically appears as two such strands, where the strands are held together by hydrogen bonds between pairs of nucleotides, and the nucleotides always pair up in a complementary fashion: A and T bind together and G and C bind together. One of the key steps in replicating or using the information stored in DNA is to make a copy of a single strand using the complementary base pairs. For example, during replication specialized molecules move along the single strand of DNA constructing a complementary strand using the base pairing rules: Read an A then create a T; read a C then create a G; read a G then create a C; or read a T then create an A. In this part of the lab we will create a Python function which simulated this process.

1) [/1] Before lab: Consider the following strand of bases: "AaCcGgTtAaCaGaTa". What do you expect the complementary strand to be?

Answer:

Download replicate_partial.py from the course website. Trace the following function call and determine its return value. Of course you can just run the function to determine its return value, but we recommend that you practice your tracing skills before you have to do something similar on an exam.

replicate_dna('AaCcGgTtAaCaGaTa') return value:

Briefly describe what is wrong with the return value of the current version of replicate_dna():

Answer:

2) [/2] In lab: Edit replicate_dna() so that it responds properly to the examples in the docstring and to the example given above. Add the feature that it can take capital letters for the bases (eg: A, C, G, T) in the input string (although the output will still be all lower case). Save the result as replicate_complete.py in your Lab04/

CPSC 301: Lab 04 5 Version: 5-Apr-18 directory. Show the TA that your code responds properly to the three examples from the docstring plus the example given above.

3) After lab: Add an optional Boolean parameter report_counts to your function replicate_dna() whose default value is False. When report_counts is False, your function will continue to display nothing on the screen and return the complementary DNA strand as it did before. When report_counts is True, in addition to returning the complementary DNA strand, your function should print a table of the following form onto the screen:

Number of base a: num_a

Number of base c: num_c

Number of base g: num_g

Number of base t: num_t

Number of unknown bases: num_x

where num_a, num_c, num_g, and num_t are replaced by the number of each of the corresponding bases (upper or lower case) that were seen in the input strand, and num_x is a count of how many characters in the input stream did not correspond to any known base. Save your results into the file replicate_counts.py in your Lab04/ directory. Hints: These numbers are merely displayed on the screen by the function, never returned; however, you may find it convenient to compute these numbers even if they are not going to be displayed. To check that your code is working correctly, you should hand-count the bases in each of the example input strings in the docstring and compare them with your function’s reported counts.

4) [/4] After lab: Add commands to the bottom of your replicate_counts.py file which call the modified replicate_dna() function with the example input strings from the docstring and with report_counts set to True and to False (this will involve at least six separate function calls). Confirm that running the file (using the green arrow button) now produces the appropriate output in the Python interpreter window. The “appropriate output” should include some kind of report of the return value for every function call, plus the counts printed to the screen by the function in those cases where counts are printed. Before submitting, you should ensure that the TA can load replicate_counts.py into Spyder, click the green arrow button, and quickly check that your function is producing the correct results by looking at the Python

CPSC 301: Lab 04 6 Version: 5-Apr-18 interpreter window (without having to type anything into the interpreter window). If the TA cannot do this simple check, you may receive zero for this portion of the lab.

5. Submission Checklist Only one person in each group will submit the lab using the handin tool.

The assignment name you should use with handin to submit this assignment is of the form “lab04x” where x is the lower case last letter of your lab section, that is, a for section L2A, b for section L2B, c for section L2C, d for section L2D, and e for section L2E. If you are working with a partner in another section, submit to the section which you attended.

While you may submit multiple times using handin (before the deadline), only your last submission will be graded. Therefore, you must submit and re-submit all the relevant files in a single zip archive. For this lab your submission archive should include

 A completed version of this lab document. Do not forget to fill in the table at the top with your identity information, your partner’s identity information, and a rough estimate of how long each component of the lab took (to the nearest 10 minutes is fine).

 Your solution to the various Python functions: bc_income_tax_2015.py, replicate_complete.py, and replicate_counts.py.

CPSC 301: Lab 04 7 Version: 5-Apr-18