COMP204_S2020_final

pdf

School

McGill University *

*We aren’t endorsed by this school

Course

204

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

18

Uploaded by mayarogerss

Report
December 2019 Supplemental Examination Intro to Computer Programming for Life Sciences COMP-204 March, 2020 EXAMINER: Mathieu Blanchette ASSOC. EXAMINER: Yue Li STUDENT NAME: McGILL ID: INSTRUCTIONS CLOSED BOOK X OPEN BOOK SINGLE-SIDED PRINTED ON BOTH SIDES OF THE PAGE X MULTIPLE CHOICE Note: The Examination Security Monitor Program detects pairs of students with unusually similar answer patterns on multiple-choice exams. Data generated by this program can be used as admissible evidence, either to initiate or corroborate an investigation or a charge of cheating under Section 16 of the Code of Student Conduct and Disciplinary Procedures. ANSWER BOOKLET REQUIRED: YES NO X EXTRA BOOKLETS PERMITTED: YES NO X ANSWER ON EXAM: YES X NO EXAM: SHOULD THE EXAM BE: RETURNED X KEPT BY STUDENT PERMITTED X Specifications: Two double-side pages, 8.5 inches x 11 inches NOT PERMITTED CRIB SHEETS: DICTIONARIES: TRANSLATION ONLY REGULAR X NONE CALCULATORS: NOT PERMITTED X PERMITTED ( Non-Programmable ) ANY SPECIAL INSTRUCTIONS: Answer all questions on this exam. This exam contains 22 questions on 18 pages. If you need extra space, use the empty pages at the end of the exam. Course: COMP-204 Intro to Computer Programming for Life Sciences Page number: 1 / 18
Multiple choice questions (21 points). Consider the following code 1 c l a s s A() : 2 def __init__( s e l f , B) : 3 s e l f .C = B 4 5 def D( s e l f ) : 6 s e l f .C = 1 7 8 def E() : 9 F = A() 1. (2 points) Which of A, B, C, D, E, or F is a method? Select ALL correct answers. A B C D E F 2. (2 points) Which of A, B, C, D, E, or F is an attribute? Select ALL correct answers. A B C D E F 3. (2 points) Which of A, B, C, D, E, or F is an object? Select ALL correct answers. A B C D E F 4. (3 points) Which line(s) of this program will trigger an error when it is executed. Select ALL correct answers. 1 d = { "Mathieu" : " Blanchette " , "Yue" : " Li " , 15:[1 ,3 ,5 ,15]} 2 d [ "Yue" ]= " L a n n i s t e r " 3 d [ "Mathieu" ][3]= "X" 4 d [ 1 5 ] = 10 5 d [ ( 3 , 8 ) ] += 1 A. Line 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 Course: COMP-204 Intro to Computer Programming for Life Sciences Page 2 Page number: 2 / 18
5. (3 points) Assume s = "AGAGTC". Which of the following boolean expression(s) will evaluate to True? Select ALL correct choices. A. s[4] == "T" B. s[-2:-1][0] == "T" C. len([ x for x in s if x=="T"]) == 1 D. not ( len(s)==6 or s == set(s) ) E. s[0]<s[1] and s[1]<s[2] 6. (3 points) What would be the sensitivity and specificity of the following prostate cancer prediction decision tree on the test data given below? Select ONE correct answers. Age PSA Gender Status 76 1.87 Male Cancer 56 1.74 Female Normal 59 1.72 Male Normal 76 1.82 Male Cancer 75 1.97 Male Normal 73 1.45 Female Normal 67 1.63 Male Cancer A. Sensitivity = 75%, Specificity = 66.67% B. Sensitivity = 66.67%, Specificity = 75% C. Sensitivity = 25%, Specificity = 33.33% D. Sensitivity = 33.33%, Specificity = 25% E. Sensitivity = 66.67%, Specificity = 25% Course: COMP-204 Intro to Computer Programming for Life Sciences Page 3 Page number: 3 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
7. (3 points) The functions below are supposed to determine whether a list of strings contains dupli- cates (i.e. at least two elements that are equal). Which ones work correctly? Select ALL correct functions. 1 def contains_duplicates_A (L) : 2 r e t u r n le n ( s e t (L) )>0 3 4 def contains_duplicates_B (L) : 5 s = {x f o r x in L} 6 r e t u r n le n ( s ) < l en (L) 7 8 def contains_duplicates_C (L) : 9 d = {} 10 f o r index , x in enumerate (L) : 11 d [ index ] = x 12 i f l en (d . keys () )!= l en (L) : 13 r e t u r n True 14 r e t u r n False 15 16 def contains_duplicates_D (L) : 17 f o r index , x in enumerate (L) : 18 i f x in L [ index +1: l en (L) ] : 19 r e t u r n True 20 r e t u r n False 21 22 def contains_duplicates_E (L) : 23 f o r index , x in enumerate (L) : 24 i f x not in L [ index +1: l e n (L) ] : 25 r e t u r n False 26 r e t u r n True A. contains_duplicates_A B. contains_duplicates_B C. contains_duplicates_C D. contains_duplicates_D E. contains_duplicates_E Course: COMP-204 Intro to Computer Programming for Life Sciences Page 4 Page number: 4 / 18
Short answer questions (35 points) 8. (2 points) What will be printed when this program is executed? 1 L = [5 , (3 ,6) , " Hello " , 4] 2 output = [ ] 3 f o r i in range (2) : 4 x = L . pop (0) 5 y = L . pop (0) 6 output . extend ( [ x , x ] ) 7 p r i n t ( output ) 9. (2 points) What will be printed when this program is executed? 1 def f u n c t i o n ( x , y ) : 2 f o r i in range ( y ) : 3 x += y 4 r e t u r n x 5 6 x = 2 7 y = 3 8 z = f u n c t i o n ( x , y ) 9 p r i n t ( x , y , z ) 10. (2 points) What will be printed when this program is executed? 1 def f u n c t i o n ( x , y ) : 2 f o r i in y : 3 x . extend ( y ) 4 r e t u r n x 5 6 x = [ 2 ] 7 y = [ 3 , 4 ] 8 z = f u n c t i o n ( x , y ) 9 p r i n t ( x , y , z ) Course: COMP-204 Intro to Computer Programming for Life Sciences Page 5 Page number: 5 / 18
11. (3 points) What will be printed when this program is executed? 1 import math 2 def fun1 ( x ) : 3 i f x <0: 4 r a i s e ValueError 5 r e t u r n math . s q r t ( x ) 6 7 def fun2 () : 8 fun1 ( - 2) 9 r e t u r n fun1 (1) 10 11 a = b = c = 0 12 t r y : 13 a = fun1 (4) 14 b = fun2 () 15 c = fun1 (9) 16 except ValueError : 17 p r i n t ( "Problem" ) 18 p r i n t (a , b , c ) 12. (3 points) What gets printed when this program is executed? 1 L = [1 , 6 , 8 , 2 , 8] 2 p r i n t ( [ x f o r x in L i f x >5] ) 3 p r i n t ( {x f o r x in L i f x>5} ) 4 p r i n t ( [ i f o r i , x in enumerate (L) i f x >5] ) 13. (3 points) Write the function dot_product that takes as arguments two lists of integers of the same lengths, and returns the sum of the products of each corresponding elements. For example, dot_product([3, 1, 2], [4, 1, 3]) should return 19, because 3*4+1*1+2*3 = 19. Make your function as short as possible. Course: COMP-204 Intro to Computer Programming for Life Sciences Page 6 Page number: 6 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
14. (3 points) What gets printed when this code is executed? 1 def fun1 ( x ) : 2 p r i n t ( x ) 3 4 def fun2 () : 5 fun1 (3) 6 r e t u r n 1 7 8 fun1 (1) 9 fun2 () 10 x=fun1 (1) 11 y=fun2 () 12 p r i n t ( x , y ) 15. (3 points) The code shown below executes a binary search over a sorted list. We have added a print statement on line 6. What would be printed when binary_search( [1, 1, 3, 4, 6, 7, 8, 8, 9], 3) is executed? 1 def binary_search ( sequence , key ) : 2 low = 0 3 high = l e n ( sequence ) - 1 4 while low <= high : 5 mid = ( low + high ) //2 6 p r i n t ( mid ) 7 i f sequence [ mid ] > key : 8 high = mid - 1 9 e l i f sequence [ mid ] < key : 10 low = mid + 1 11 e l s e : 12 r e t u r n mid 13 r e t u r n None Course: COMP-204 Intro to Computer Programming for Life Sciences Page 7 Page number: 7 / 18
16. (6 points) What would be printed when this program is executed? Indicate your answer next to the arrows on lines 34-49. If executing a particular line would cause an error/exception, just indicate "ERROR" on that line, but assume that the execution continues. 1 c l a s s Molecule : 2 def __init__( s e l f , comp) : 3 s e l f . composition = {a : comp . count ( a ) f o r a in s e t (comp) } 4 5 def __str__( s e l f ) : 6 r e t u r n " Molecule " + s t r ( s e l f . composition ) 7 8 def nb_atoms ( s e l f ) : 9 r e t u r n sum ( s e l f . composition . v a l u e s () ) 10 11 c l a s s Nucleotide ( Molecule ) : 12 def __init__( s e l f , nuc ) : 13 s e l f . name=nuc 14 i f nuc== "A" : 15 Molecule . __init__( s e l f , "CCCCCHHHHHNNNNN" ) 16 e l i f nuc== "C" : 17 Molecule . __init__( s e l f , "CCCCHHHHHNNNO" ) 18 e l i f nuc== "G" : 19 Molecule . __init__( s e l f , "CCCCCHHHHHNNNNNO" ) 20 e l i f nuc== "T" : 21 Molecule . __init__( s e l f , "CCCCCHHHHHHNNOO" ) 22 23 def __str__( s e l f ) : 24 r e t u r n Molecule . __str__( s e l f )+ " has name " + s t r ( s e l f . name) 25 26 def mystery ( s e l f ) : 27 L=[ "A" , "C" , "G" , "T" ] 28 r e t u r n Nucleotide (L[3 - L . index ( s e l f . name) ] ) 29 30 31 a = Molecule ( "COO" ) 32 b = Nucleotide ( "C" ) 33 34 p r i n t ( a ) # -- > 35 36 37 p r i n t (b) # -- > 38 39 40 p r i n t ( a . nb_atoms () ) # -- > 41 42 43 p r i n t (b . nb_atoms () ) # -- > 44 45 46 p r i n t ( a . mystery () ) # -- > 47 48 49 p r i n t (b . mystery () ) # -- > Course: COMP-204 Intro to Computer Programming for Life Sciences Page 8 Page number: 8 / 18
17. (4 points) Connect each situation listed on the left to the Python compound type that would be most appropriate, by drawing a line between each item on the left to an item on the right. 1. The composition of an ecosystem, de- scribed as the number of individuals of each species. 2. The group of symptoms experienced by each patient at the hospital. 3. The record of the group of animal species present on the island of Montreal, for each year from 1900 to today. 4. The spatial coordinates of a tumour in the brain. List of strings List of set of strings Tuple of float Dictionary with key = strings, values = int String of lists Dictionary with key = strings, values = set of strings 18. (4 points) Write a function called after that takes a string S and a string marker as arguments, and returns a string made of the individual characters that immediately follow every occurrence of string marker in S . For example, after("AGCAGCCAGCT","GC") should return the String "ACT", whereas after("AGCAGCCAGCT","GCC") should return the String "A". Course: COMP-204 Intro to Computer Programming for Life Sciences Page 9 Page number: 9 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Long answer questions (44 points) 19. (12 points) Write the nutritive_profile function, as described in the comments below. See examples at the bottom. 1 def n u t r i t i v e _ p r o f i l e ( meal , value_per_gram ) : 2 """ 3 Arguments : 4 meal : A d i c t i o n a r y with keys = i n g r e d i e n t s , and 5 v a l u e s = q u a n t i t i e s ( in grams ) 6 value_per_gram : A d i c t i o n a r y with keys = i n g r e d i e n t s and 7 v a l u e s = d i c t i o n a r y of elemental composition . 8 An elemental composition d i c t i o n a r y 9 has keys = type of element and 10 v a l u e s = q u a n t i t y ( in grams ) per gram of i n g r e d i e n t . 11 Returns : An elemental composition d i c t i o n a r y , with the meal ’ s 12 t o t a l number of grams of each type of element . 13 """ 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 value_per_gram={ " p i z z a " :{ " l i p i d s " : 0 . 5 , " carbs " : 0 . 4 , " p r o t e i n s " : 0 . 1 , " s a l t " : 0 . 1 } , 40 " milk " : { " l i p i d s " : 0 . 2 , " carbs " : 0 . 3 , " p r o t e i n s " : 0 . 2 } , 41 " apple " :{ " carbs " : 0 . 2 , " p r o t e i n s " : 0 . 2 } , 42 " cake " : { " l i p i d s " : 0 . 3 , " carbs " : 0 . 5 , " p r o t e i n s " : 0 . 1 } , 43 " p i e " : { " l i p i d s " : 0 . 2 , " carbs " : 0 . 3 , " p r o t e i n s " : 0 . 2 } } 44 my_meal = { " milk " :1000} 45 p r i n t ( n u t r i t i v e _ p r o f i l e (my_meal , value_per_gram ) ) 46 # { ’ l i p i d s ’ : 200.0 , ’ carbs ’ : 300.0 , ’ p r o t e i n s ’ : 200.0} 47 48 my_other_meal = { " p i z z a " :100 , " cake " :100} 49 p r i n t ( n u t r i t i v e _ p r o f i l e ( my_other_meal , value_per_gram ) ) 50 # { ’ l i p i d s ’ : 80.0 , ’ carbs ’ : 90.0 , ’ p r o t e i n s ’ : 20.0 , ’ s a l t ’ : 10.0} Course: COMP-204 Intro to Computer Programming for Life Sciences Page 10 Page number: 10 / 18
20. (10 points) You are working at Environment Canada and you are asked to write a program to calculate the average monthly temperature based on past recorded temperatures. You are asked to write the avg_monthly_temp function, which takes as input A list of temperature measurements (float) for a certain number of consecutive days. An object of class MyDate (see below), containing the date of the first measurement in the list. The function should return a dictionary with keys = month names and values = average temperature for that month. Only months for which temperatures were recorded should be included as keys in the dictionary. For example, avg_monthly_temp([20, 24, 22, 18, 16, 16, 18], MyDate(29, "July", 1980) ) should return the dictionary {"July":22.0, "August":17.0}. Assume the class MyDate is given to you; y ou do not need to write it. The class contains an __init__ method that takes a day (integer), month (string), and year (integer) as arguments. It also contains a next_date() method, which returns an object of type MyDate that corresponds to the date of the day following the date on which it is called. For example, if today = MyDate(31, "December", 2019), then today.next_date() returns object MyDate(1, "January", 2020). The next_date method knows about each month duration, as well as leap years, and behaves accordingly. 1 def avg_monthly_temp ( temp , date ) : 2 """ 3 Input : temp : a l i s t of temperatures ( f l o a t ) , taken d a i l y , 4 s t a r t i n g on the s p e c i f i e d s t a r t date . 5 date : Object of type MyDate , corresponding to 6 the date of the f i r s t measurement 7 Returns : D i c t i o n a r y with key = month names ( s t r i n g ) 8 and v a l u e s = average temperature f o r each month 9 """ Course: COMP-204 Intro to Computer Programming for Life Sciences Page 11 Page number: 11 / 18
21. (12 points) A microscopy image shows a set of cells of different types. The image has been treated to identify edges in the image (boundary of cells). It has a black background, and white edges. Bacteria of type bacillus have a long, narrow shape. Bacteria of type E. coli have approximately round shape. Human cells are large and have an approximately round shapes. Our goal is to write a program that counts the number of each type of cells present in the image. a) (6 points) Start by writing a modified version of the seedfill function seen in class and reproduced below. Your modified version should return a tuple of 3 numbers: number of pixels in the portion filled, length of the portion filled, height of the portion filled. For example, on the image shown below, seedfill(4,3, ..... ) should return (10, 4, 3), and seedfill(9,12) should return (11,7,3). You can either indicate the changes directly on the code below, or rewrite the function (without the comments) using one of the supplementary pages at the end of this booklet. 1 def s e e d f i l l (im , seed_row , seed_col , f i l l _ c o l o r , bckg ) : 2 """ 3 Arguments : 4 im : The image on which to perform the s e e d f i l l algorithm 5 seed_row and seed_col : p o s i t i o n of the seed p i x e l 6 f i l l _ c o l o r : Color f o r the f i l l 7 bckg : Color of the background , to be f i l l e d 8 Returns : Number of p i x e l s f i l l e d 9 Behavior : Modifies image by performing s e e d f i l l 10 """ 11 s i z e =0 12 13 n_row , n_col = im . shape 14 15 f r o n t =[( seed_row , seed_col ) ] 16 17 18 while l e n ( f r o n t ) >0: 19 20 r , c = f r o n t . pop () 21 22 im [ r , c]= f i l l _ c o l o r 23 24 s i z e+=1 25 26 27 f o r i in range ( max (0 , r - 1) , min (n_row , r +2)) : 28 29 f o r j in range ( max (0 , c - 1) , min ( n_col , c+2)) : 30 31 i f im [ i , j ]==bckg and ( i , j ) not in f r o n t : 32 33 f r o n t . add (( i , j ) ) 34 35 36 r e t u r n s i z e Course: COMP-204 Intro to Computer Programming for Life Sciences Page 12 Page number: 12 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
b) (6 points) Write the function count_cells, which takes as argument a black-and-white image (with black background and white edges), and returns a tuple of three integers: the number of bacillus cells, E. coli cells, and human cells (in that order). Bacillus cells have between 5 and 12 black pixels (inclusively), and have a ratio of length to height of at least 2 (i.e. their length must be at least twice their height) E. coli cells have between between 4 and 10 pixels (inclusively), and have a ratio of length to height of at most 1.5 (i.e. their length must be at most 1.5 times their height) Human cells have size at least 15 pixels, and have a ratio of length to height of at most 1.5. Any region that does not meet the conditions above is not a cell. For example, called on the image shown on the previous page, the count_cells function should return the tuple (2, 1, 1), because we have (in clock-wise order, starting from the top left) An E. coli cell of 10 pixels, length 4 and height 3. A Bacillus cell of 11 pixels, length 8, and height 2. A Bacillus cell of 11 pixels, length 7, and height 3. An object that is not a cell, of 3 pixels, length 2, and height 2. A human cell of 28 pixels, length 5, and height 7. Course: COMP-204 Intro to Computer Programming for Life Sciences Page 13 Page number: 13 / 18
22. (10 points) In this question you will write code to simulate the following system. In a medical clinic, there are patients and doctors. Patients are in a waiting list, in the order in which they arrived at the clinic. Each patient has a certain set of symptoms that are present. Each doctor is only accredited to treat only a certain set of symptoms. If a patient has at least one symptom the doctor is unable to treat, the doctor will not treat that patient at all. If a doctor is able to treat a patient, the procedure will take exactly one hour. A doctor can only treat one patient at a time, and each patient only needs to be treated by one doctor. Assuming we start with a given waiting list of patients, and set of available doctors with their respective accreditation, the goal of our simulation it to determine how many hours it will take before all patients are treated. You have have access to the Patient and Doctor classes defined below. a) (2 points) Write the can_treat_patient method of the Doctor class, which takes as argument a Patient object and returns True if the doctor is able to treat the patient, and False otherwise. A doctor can treat a patient if they can treat all of the symptoms that patient has. 1 c l a s s Patient : 2 def __init__( s e l f , name , symptoms ) : 3 s e l f . name = name 4 s e l f . symptoms = symptoms 5 6 c l a s s Doctor : 7 def __init__( s e l f , name , can_treat ) : 8 s e l f . name = name 9 s e l f . can_treat = can_treat 10 s e l f . a v a i l a b l e = True 11 12 # WRITE THE can_treat_patient METHOD HERE Course: COMP-204 Intro to Computer Programming for Life Sciences Page 14 Page number: 14 / 18
b) (8 points) Write the function time_until_empty, which takes as argument a List of Patient objects, and a List of Doctor objects. Each hour, the patients at the front of the waiting list get assigned to an available doctor who can treat their symptom, if one is available. If a patient’s symptoms cannot be treated by any available doctor, the patient remains in the waiting list. In the example given below, time_to_empty(patients, doctors) should return 3, because: Hour 1: Ana is treated by Dr. House, Suzanne is treated by Dr. Greene, Elvis is treated by Dr. Yang, and the other patients cannot be treated. Hour 2: Amol is treated by Dr. House, Sharon is treated by Dr. Greene, and the other patient cannot be treated. Hour 3: Frank is treated by Dr. House. 1 # WRITE THE time_to_empty FUNCTION HERE 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 d o c t o r s = [ Doctor ( " Green " , { " headache " , " dementia " }) , 32 Doctor ( "House" , { " sore_throat " , " headache " , " h e a r t _ f a i l u r e " , " dementia " }) , 33 Doctor ( " P y c e l l " ,{ " sore_throat " , "cough" , " headache " }) , 34 Doctor ( "Yang" , { " h e a r t _ f a i l u r e " , " arrythmia " , "cough" }) , 35 Doctor ( "McCoy" , { " klingon_fleshwound " }) ] 36 37 p a t i e n t s = [ P a t i e n t ( "Ana" , { " h e a r t _ f a i l u r e " }) , 38 P a t i e n t ( " Suzanne " ,{ " headache " , " dementia " }) , 39 P a t i e n t ( "Amol" , { " h e a r t _ f a i l u r e " , " sore_throat " }) , 40 P a t i e n t ( " Sharon " , { " dementia " }) , 41 P a t i e n t ( " E l v i s " , { " h e a r t _ f a i l u r e " , " arrythmia " }) , 42 P a t i e n t ( " Frank " , { " h e a r t _ f a i l u r e " , " headache " }) ] 43 44 t=time_to_empty ( p a t i e n t s , d o c t o r s ) 45 p r i n t ( t ) # should p r i n t 3 Course: COMP-204 Intro to Computer Programming for Life Sciences Page 15 Page number: 15 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
This page is left empty intentionally. Use it as you need. Course: COMP-204 Intro to Computer Programming for Life Sciences Page 16 Page number: 16 / 18
This page is left empty intentionally. Use it as you need. Course: COMP-204 Intro to Computer Programming for Life Sciences Page 17 Page number: 17 / 18
This page is left empty intentionally. Use it as you need. Course: COMP-204 Intro to Computer Programming for Life Sciences Page 18 Page number: 18 / 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help