联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2021-04-21 11:15

CPSC 103 Midterm

Monday March 22nd, 2021, 18:00 to 19:45

– You have 105 (90 for the exam and 15 for file uploading) minutes to write the 5 questions

on this examination. A total of 39 marks are available.

– This exam is to be handwritten. You are not allowed to type during the

exam. The only exception to this rule is if you are typing a message to the

invigilators or typing a post on our exam Piazza page.

– If we ask you to design a solution, we mean to design it using the appropriate design

recipes (i.e., HtDF, HtDD, or both) taught in CPSC 103.

– You are allowed to access Canvas, your notes, and (only to ask a question if you believe

you have found an error on the exam) private posts on the exam Piazza page, but not

other web sites. You are not allowed to contact people other than the course staff. You

are not allowed to use a calculator, Jupyter/Sygyzy or any other application on your

computer.

– You must write this exam under invigilation. Failure to do so will result in a grade of 0.

– If you are not directly writing on a printed copy of the exam, you do not need to copy

the given code onto your paper. For example, if we ask you to finish implementing a

function and we have already given you the signature/purpose/stub/tests, you just need

to clearly indicate which function the answer is meant for and then write your answer.

– Use the number of marks allocated to each question to help you determine how much

time you should spend on them.

– Good luck!

UNIVERSITY REGULATIONS:

– Each candidate should be prepared to produce, upon request, his/her UBC card.

– No candidate shall be permitted to enter the examination room after the expiration of

one half hour, or to leave during the first half hour of the examination.

– CAUTION: candidates guilty of any of the following, or similar, dishonest practices shall

be immediately dismissed from the examination and shall be liable to disciplinary action.

1. Having at the place of writing, or making use of, any books, papers or memoranda,

electronic equipment, or other memory aid or communication devices, other than

those authorised by the examiners.

2. Speaking or communicating with other candidates.

page 2 out of 14

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 3 out of 14

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 4 out of 14

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 out of 14

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()

page 6 out of 14

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 7 out of 14

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 8 out of 14

# 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)

page 9 out of 14

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 10 out of 14

@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

page 11 out of 14

@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()

page 12 out of 14

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 13 out of 14

@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 14 out of 14

@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()


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp