CPSC_103_Midterm_2020W2_Solution-1

pdf

School

University of British Columbia *

*We aren’t endorsed by this school

Course

103

Subject

Statistics

Date

Apr 3, 2024

Type

pdf

Pages

18

Uploaded by AgentBatPerson1076

Report
CPSC 103 Midterm Monday March 22nd, 2021, 18:00 to 19:45 Academic Integrity Pledge We are taking this exam under unusual circumstances. It is important that this exam is fair to all students, and that no student has an unfair advantage. With that in mind, after reading the following paragraph, please affirm that you will neither give assistance to, nor receive assistance from, another student about this exam. Note that some students may be writing another version of this exam at a different time than you are. So, be sure to keep quiet about its contents (for the next 72 hours). I hereby pledge that I have read and will abide by the rules, regulations, and expectations set out in the Academic Calendar, with particular attention paid to: 1. The Student Declaration http://www.calendar.ubc.ca/vancouver/index.cfm?tree=3,285,0,0 2. The Academic Honesty and Standards http://www.calendar.ubc.ca/vancouver/index.cfm?tree=3,286,0,0 3. The Student Conduct During Examinations http://www.calendar.ubc.ca/vancouver/index.cfm?tree=3,41,90,0L 4. And any special rules for conduct as set out by the examiner. You are free to choose whichever option you want below but do keep in mind that we will not be able to grade your exam if you indicate that you disagree with this, or if you do not answer this question. What You Need To Do If you agree to the conditions above, please write the following sentence and sign your name next to the statement . ”I agree to abide by the exam’s academic integrity pledge.”
page 2 1. [6 marks] Give the data type would be best suited for the following situations. 1. The Piazza posts that are yet to be answered 2. The colours of the rainbow 3. The planets in our solar system 4. The colour of a sweatshirt 5. The distance you can drive before you need to charge your car 6. Courses a student has taken at UBC 7. The position of a driver in a car race, considering that not all driver finish all race (some may be out of the race due to mechanical issues or accidents). 8. The types of batteries sold at a store (e.g., AAA, AA, CR2032, etc.) 9. Whether or not you can dine in at a restaurant 10. Your grocery bill for the week 11. Email messages received by someone 12. The number of hours you slept last night
page 3 Solution : 1. List 2. Enum 3. Enum 4. Simple Atomic 5. Interval 6. List 7. Optional 8. Enum 9. Simple Atomic 10. Interval 11. List 12. Interval
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
page 4 2. [4 marks] Arty has written some messy code that doesn’t follow our HtDF recipe and doesn’t quite work. Fix this code so it can accomplish the task listed in the purpose. The data definition is correct. The errors are in HtDF. Identify the line number where each error is found and describe how you would fix each error (if you believe a line should be removed, write its number and LINE SHOULD BE REMOVED after it). 01. from cs103 import * 02. from typing import List 03. 04. # List[str] 05. # a list of strings 06. 07. L0 = [] 08. L1 = ["apple"] 09. L2 = ["apple", "orange", "banana", "Anne", "Oswald", "berry"] 10. 11. @typecheck 12. def fn_for_los(los: List[str]) -> ...: 13. # description of the accumulator 14. acc = ... # type: ... 15. 16. for s in los: 17. acc = ...(s, acc) 18. 19. return ...(acc)
page 5 01. @typecheck 02. def find_words_starting_with_a_and_o(los: List[str]): 03. """ 04. Returns all the words that start with an "a" or an "o", 05. in lowercase or uppercase. 06. """ 07. # return [] # stub 08. # template from List[str] 09. # all words that start with a or o seen so far 10. acc = [] # type: List 11. 12. for s in los: 13. if s[0] == "a" or "s[0] == A": 14. acc.append("a") 15. return acc 16. 17. for s in los: 18. if s[0] == "o" or s[0] == "O": 19. acc.append("o") 20. 21. return acc 22. 23. start_testing() 24. 25. expect(find_words_starting_with_a_and_o(L0), []) 26. expect(find_words_starting_with_a_and_o(L2), ["apple, "orange", "Anne", "Oswald"]) 27. 28. summary() Solution : 02. def find_words_starting_with_a_and_o(los: List[str]) -> List[str]: 10. acc = [] # type: List[str] 13. if s[0] == "a" or s[0] == "A": 14. acc.append(s) 15. LINE SHOULD BE REMOVED 17. LINE SHOULD BE REMOVED 19. acc.append(s)
page 6 26. A closing quotation mark was missing from ”apple”. This was an error that also exists in the data definition and as the question stated that the data definition is correct, this error may not have been included in the student answer. As such, not including this error will not negatively impact your overall grade for this question.
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
page 7 The rest of the questions on the exam are based on an Animal Hospital that treats cats and dogs. Each animal has a name, colour, age, gender, species (dog or cat), weight, and a status to indicate whether or not it was rescued from the street. 3. [9 marks] Complete the data definitions used in this Animal Hospital. from cs103 import * from enum import Enum from typing import NamedTuple from typing import List Species = Enum(’Species’, [’cat’, ’dog’]) # interp. An animal’s species. It can be a cat or a dog. # Examples are redundant for enumerations # template based on Enumeration @typecheck def fn_for_species(s: Species) -> ...: if s == Species.cat: return ... elif s == Species.dog: return ... Gender = Enum(’Gender’, [’male’, ’female’]) # interp. of an animal gender # examples are redundant for enumerations # template based on Enumeration @typecheck def fn_for_gender(g: Gender) -> ...: if g == Gender.male: return ... elif g == Gender.female: return ...
page 8 Animal = NamedTuple(’Animal’, # interp. An animal has a name, colour, age (in years), # gender, species, weight (in kg), # and if it is rescued from the street or not. A1 = A2 = A3 = # template based on @typecheck def fn_for_animal(a: Animal) -> ...:
page 9 # List[Animal] # interp. A list of animals in an animal hospital L0 = [] L1 = [A1, A2, A3] # template based on arbitrary-sized and reference rule @typecheck def fn_for_loa(loa: List[Animal]) -> ...: # description of the accumulator acc = ... # type: ... for a in loa: acc = ...(fn_for_animal(a), acc) return ...(acc) Solution : Animal = NamedTuple(’Animal’, [(’name’, str), (’colour’, str), (’age’, int), # in range [0, ...) (’gender’, Gender), (’species’, Species), (’weight’, float), # in range [0, ...) (’is_rescued’, bool)]) # interp. An animal with its name, color, age (in years), # gender, species, weight (in kg), # and if it is rescued from the street or not. A1 = Animal("Puppy", "brown", 10, Gender.female, Species.dog, 18.9, True) A2 = Animal("Milly", "brown", 12, Gender.female, Species.dog, 10.2, True) A3 = Animal("Sophia", "white", 0, Gender.female, Species.cat, 1.3, False) # template based on Compound and reference rule @typecheck def fn_for_animal(a: Animal) -> ...: return ...(a.name, a.colour, a.age, fn_for_gender(a.gender), fn_for_species(a.species),
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
page 10 a.weight, a.is_rescued)
page 11 4. [13 marks] The following function examines a list of animals determine whether it contains a male animal that is the given age and colour. Complete the helper functions and add new ones if needed. You can assume has male dog() is complete and correct. Assume the tests for all functions are present, complete, and correct. Hint: You have to create at least 2 new helper functions. @typecheck def has_male_dog(animals: List[Animal], age: int, color: str) -> bool: """ Returns True if there is at least one male dog in animals that has the given color and age; False otherwise. """ # return True # stub # template copied from List[Animal] with 2 additional parameters for a in animals: if is_animal_male(a) and is_animal_dog(a) and has_age(a, age) and has_color(a, color): return True return False @typecheck def is_animal_male(a: Animal) -> bool: """ Returns True if a is male; False otherwise. """ # return True # stub
page 12 @typecheck def is_animal_dog(a: Animal) -> bool: """ Returns True if a is a dog; False otherwise. """ # return True # stub @typecheck def has_age(a: Animal, age: int) -> bool: """ Return True if a is exactly age years old """ # return True # stub
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
page 13 @typecheck def has_color(a: Animal, colour: str) -> bool: """ return True if a’s colour is colour """ # return True # stub start_testing() # Assume the set of tests is correct and complete summary() Solution : @typecheck def is_animal_male(a: Animal) -> bool: """ Returns True if a is a male; False otherwise. """ # return True # stub # template copied from Animal return is_male(a.gender)
page 14 @typecheck def is_male(g: Gender) -> bool: """ Returns True if g represents a male; False otherwise. """ # return True # stub # template copied from Gender if g == Gender.male: return True elif g == Gender.female: return False @typecheck def is_animal_dog(a: Animal) -> bool: """ Returns True if a is a dog; False otherwise. """ # return True # stub # template copied from Animal return is_dog(a.species) @typecheck def is_dog(s: Species) -> bool: """ Returns True if s is a dog; False otherwise. """ # return True # stub # template copied from Species if s == Species.cat: return False elif s == Species.dog: return True @typecheck def has_age(a: Animal, age: int) -> bool: """ Returns True if a is exactly age years old; False otherwise. """ # return True # stub # template copied from Animal with additional parameter (age) return a.age == age
page 15 @typecheck def has_color(a: Animal, colour: str) -> bool: """ Returns True if a’s colour is colour; False otherwise. """ # return True # stub # template copied from Animal with additional parameter (colour) return a.colour == colour
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
page 16 5. [7 marks] The following function should return all female cats heavier than a given weight. It is possible that your answer will not call every helper function listed in this question. You cannot write any other helper functions for this question. Only use what you are given. You are not required to complete the helpers; you only need to complete find female cats() . You can assume that the tests are complete and correct for all functions. @typecheck def find_female_cats( """ Returns a list of female cats that are strictly heavier than weight. """ # return [] # stub # template based on composition
page 17 @typecheck def find_all_cats(animals: List[Animal]) -> List[Animal]: """ Returns all cats in animals. """ return [] # stub @typecheck def is_animal_cat(a: Animal) -> bool: """ Returns True if a is a cat; False otherwise. """ return True # stub @typecheck def is_cat(s: Species) -> bool: """ Returns True if s is a cat; False otherwise. """ return True # stub @typecheck def find_all_female(animals: List[Animal]) -> List[Animal]: """ Returns all female animals. """ return [] # stub @typecheck def is_animal_female(a: Animal) -> bool: """ Returns True if a is female; False otherwise. """ return True @typecheck def is_female(g: Gender) -> bool: """ Returns True if g represents a female; False otherwise. """ return True
page 18 @typecheck def find_animals_weight(animals: List[Animal], weight: float) -> List[Animal]: """ Returns all animals that are strictly heavier than weight. """ return [] # stub @typecheck def weight_more_than(a: Animal, weight: float) -> bool: """ Returns True if a is strictly heavier than weight; False otherwise. """ return True # stub start_testing() # Assume the tests are complete and correct summary() Solution : @typecheck def find_female_cats(animals: List[Animal], weight: float) -> List[Animal]: """ Returns a list of female cats that are strictly heavier than weight. """ # return [] # stub # template based on composition # 1. filter all cats # 2. filter all female cats # 3. filter all female cats that weight more than weight # 4. return final list list_cats = find_all_cats(animals) list_female_cats = find_all_female(list_cats) list_female_cats_weight = find_animals_weight(list_female_cats, weight) return list_female_cats_weight
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