Computing in the Life Sciences CPSC 301, Term 2, Winter 2016–2017

Lab 05

Practice with Conditionals and Loops

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

Due : Sunday, Feb 12, 9:00 am MARK: / 27

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.

·  Write new functions which may include choices and loops and use them to perform computations

·  Design functions with default parameters.

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

·  Read code other people have written and modify it

2.  [/8] 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 2017 tax year are posted (note that this webpage includes rates from multiple years, but we will use the 2017 rates for this exercise). In this section you will design a Python function bc_income_tax_2017() that takes as input the taxable income and returns the tax. Both values should be rounded to the nearest cent.

1)  [/3] Before lab: Create a new file in Spyder or a text editor, and 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_2017(). Taxable income should be positive. You should have an example to test each of the brackets, and each example should test the upper boundary case when there is one. You can save the header and docstring as bc_income_tax_2017.py (ensure you transfer the file to your Lab05/ directory when you get to the lab if using the lab computers).

2)  In lab: Work through step five of the function design recipe: create the body.

·  Create local variables to store each of the bracket income levels and rates, and any other values that you will refer to. Make sure to use these variables (and not hard coded values) in your calculations of the tax owing.

·  Use a series of if statements to implement the various tax brackets. If you need help figuring out an approach calculation, pay close attention to how the calculation is described on the webpage with the listed rates. There is more than one way to implement the calculation, but we recommend the cumulative approach.

·  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_2017.py in your Lab05/ subdirectory. Save your file.

3)  In lab: 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)  [/5] In lab: Show the TA the following:

a)  Your body is designed according to the instructions in step 2 above

b)  Copy & paste your docstring examples into the Python console and demonstrate that they return the correct results.

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

3.  [/9] 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). 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 simulates this process.

1)  [/3] 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] 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 Lab05/ directory. Show the TA that your code responds properly to the three examples from the docstring plus the example given above.

2)  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 Lab05/ 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.

3)  [/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 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.

4.  [/10] Extend and Improve the Room Painting Cost Program

In this part of the lab you are going to modify and improve the last version of the program that calculates the cost of painting a room which we have discussed in the lectures. Your modified program will :

·  Include the ceiling in the paint cost calculations.

·  Make sure that the calculations for the labour and paint costs are robust, by checking that the area to be painted and the coats of paint are not negative values.

·  Calculate the GST and the PST tax for the cost and add it to the total cost. GST tax applies only to labour and PST applies only to paint.

·  Allow each run of the program to make cost calculations for multiple rooms.

·  At the end of each run, display the number of rooms provided by the user during the run, the total cost for painting all these rooms and the average cost per room.

Before lab : Go over the file paintcost5.py, check the code and start thinking about the modifications you need to perform. Most of the changes have been documented in the program with comments that start with “# ***** “ . You can even start writing code before you go to the lab either as notes on a separate text document or as comments on the actual program file. When you open it with Spyder you can start removing the ‘#’ signs and turn your comments into actual code.

In lab : If using the lab computers, make a lab05 folder on your Z drive and transfer paintcost5.py in that folder. Open the file in Spyder and perform the following tasks:

1.  [ / 2] Modify the wall_area function to include the ceiling area in the area to be painted. Also make the function more robust by including a precondition and returning 0 if any of its arguments is negative or 0. Update the docstring and the code. Run the program and test the new function. Save your changes.

Show your TA.

2.  [ / 1] Modify the paint_cost function to include a precondition that its arguments be positive, and to return 0 if either argument is not positive. Update the docstring and the code, run the program and test the function. Save your changes.

Show your TA.

3.  [ / 1] Modify the labour_cost function to include a precondition that its arguments be positive, and to return 0 if either argument is not positive. Update the docstring and the code, run the program and test the function. Save your changes.
Show your TA.

After Lab: Make the following modifications to the main program.

4.  [ / 2] Complete the tax function to return the tax t for the given amount and tax rate. The function should return 0 is any of its arguments are not positive. Modify the main program to calculate and display a 7% pst tax for the paint cost and a 6% gst tax for the labour cost of the room and add it to the total cost for the room. Finally, complete the docstring. Run and test the program. Save your changes.

5.  [ / 2] Modify the main program to

  1. keep calculating and displaying the costs for a number of rooms (one after the other) until the user enters 0 for room length. In that case the program stops,
  2. display all the values that represent currency (like costs, etc.) rounded to 2 decimal places. For better precision, you should keep the intermediate calculations unrounded and round the results before you display them.

The following is a sample output that calculates the costs for two rooms:

Enter paint price per gallon: 25

Enter labour price per hour: 20

Enter room's length: 10

Enter room's width: 10

Enter room's height: 10

Total area is: 500.0

Paint cost is: 250.0

Labour cost is: 1000.0

pst is: 17.5

gst is: 60.0

Total cost is: 1327.5

Enter room's length: 20

Enter room's width: 20

Enter room's height: 10

Total area is: 1200.0

Paint cost is: 600.0

Labour cost is: 2400.0

pst is: 42.0

gst is: 144.0

Total cost is: 3186.0

Enter room's length: 50

Enter room's width: 25

Enter room's height: 8

Total area is: 2450.0

Paint cost is: 1225.0

Labour cost is: 4900.0

pst is: 85.75

gst is: 294.0

Total cost is: 6504.75

Enter room's length: 0

Bye now. Hope to hear from you soon

6.  [ / 2] Modify the main program to accumulate the total costs for the rooms we want to paint. At the end of the run (i.e. when the while loop terminates) the program must print the number of rooms, the total cost for all rooms and the average cost per room. The average cost per room is calculated by taking the total cost for all rooms and dividing it by the number of rooms.

The following is a sample output:

Enter paint price per gallon: 25

Enter labour price per hour: 20

Enter room's length: 10

Enter room's width: 10

Enter room's height: 10

Total area is: 500.0

Paint cost is: 250.0

Labour cost is: 1000.0

pst is: 17.5

gst is: 60.0

Total cost is: 1327.5

Enter room's length: 20

Enter room's width: 20

Enter room's height: 10

Total area is: 1200.0