代做CSC 108 H1F December 2019 Examinations代写R编程

Faculty of Arts & Science

December 2019 Examinations

CSC 108 H1F

Question 1. [4 marks]

Part (a) [1 mark] Complete the function is_eligible_for_award according to its docstring below.

def is_eligible_for_award(gpa: float, major: str, req_gpa: float, req_major: str) -> bool:

"""Return True if and only if gpa is greater than or equal to req_gpa and

major is equal to req_major.

>>> is_eligible_for_award(4.0, ‘CS’, 3.5, ‘CS’)

True

>>> is_eligible_for_award(4.0, ‘Econ’, 3.5, ‘CS’)

False

>>> is_eligible_for_award(3.3, ‘CS’, 3.5, ‘CS’)

False

"""

return

Part (b) [3 marks] Complete the function get_award_winners according to its docstring below. Your code must call the function is_eligible_for_award from Part (A).

def get_award_winners(applicants: List[Tuple[float, str, str]], req_gpa: float,

req_major: str) -> List[Tuple[float, str]]:

"""Return a new list of students from applicants who are eligible for an

award, based on req_gpa and req_major. Each student in applicants is of

the form. (gpa, name, major).

In the new list, award winning students should be of the form. (gpa, name)

and should appear in the same order as they appear in the original list.

>>> get_award_winners([(3.6, ‘JC’, ‘CS’), (4.0, ‘MB’, ‘CS’), (3.7, ‘JW’, ‘Econ’)], 3.2, ‘CS’)

[(3.6, ‘JC’), (4.0, ‘MB’)]

>>> get_award_winners([(3.6, ‘JC’, ‘Bio’), (4.0, ‘MB’, ‘Bio’),

(3.7, ‘JW’, ‘Econ’)], 3.7, ‘Bio’)

[(4.0, ‘MB’)]

"""

Question 2. [4 marks]

Function mask_string has been correctly implemented. Fill in the missing parts of the docstring description and examples so that they match the function’s header and body.

def mask_string(s: str, mask: List[bool], ch: str) -> str:

"""Return a new string that is the same as

but with each character that corresponds to a True element in

replaced with      .

Precondition : len(ch) == 1 and

>>> mask_string(‘mask’,[True, True, True, True],‘?’)

>>> mask_string(‘ComSc’, [   ,   ,   ,   ,   ],   )

‘C--S-’

"""

masked_string = ‘’

for i in range(len(s)):

if mask[i]:

masked_string = masked_string + ch

else:

masked_string = masked_string + s[i]

return masked_string

Question 3. [6 marks]

In Assignment 1, we split a message into substrings (called tweets), where each tweet had a length equal to MAX_LENGTH characters, except the last tweet which had a length of at least 1 up to MAX_LENGTH (inclusive). For example, for a MAX_LENGTH of 10 and the message ‘This is such an amazing note!’, in A1 we split the message into 3 tweets: ‘This is su’, ‘ch an amaz’, and ‘ing note!’.

You must implement an improved version in which the message is split so that no word is divided across multiple tweets. In this improved version, the message above is split into the 4 tweets with lengths as close to MAX_LENGTH as possible (but not longer than MAX_LENGTH) and spaces omitted from the beginning and end of tweets: ‘This is’, ‘such an’, ‘amazing’, ‘note!’.

Complete the function split_message according to the description above and docstring below. Do not add any code outside of the boxes. Your code must use the provided constant. You may assume that there is exactly one space between words in the message and that there is no whitespace at the beginning or end of the message.

MAX_LENGTH = 10

def split_message(msg: str) -> List[str]:

"""Return a new list containing msg split into tweets so that no words are

divided across multiple tweets. A word is a sequence of non-whitespace

characters. In the tweets, each pair of words must have a space between them.

There should not be any spaces at the beginning or end of tweets.

Precondition: no word in msg is longer than MAX_LENGTH

>>> split_message(‘One hundred and one’)

[‘One’, ‘hundred’, ‘and one’]

"""

# split message into words

tweets =

i=0

while i <   :

# combine tweet i and tweet i+1 into a potential tweet

potl_tweet =

if   :

# update tweet i to be potential tweet; remove tweet i + 1

else:

# couldn’t combine tweets, so increment i (tweet i is finalized)

i=i+1

return tweets

Question 4. [6 marks]

This problem involves using a point system to score words. Each letter has a point value and is represented in a "letter to points" dictionary, where each key is a lowercase letter and each value is the points corresponding to that letter. An example of a "letter to points" dictionary is:

{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 2, ‘e’: 1, ‘f’: 4, ..., ‘z’: 10}

Words are scored by adding up the points associated with each of their letters. According to the dictionary above, the word ‘Bee’ is worth 4 points (2 + 1 + 1) and the word ‘bEAd’ is worth 6 points (2 + 1 + 1 + 2). Only lowercase letters appear in "letter to points", so uppercase letters are scored based on the corresponding lowercase letter’s points.

Consider this example ! le with one word per line:

a

BE

bee

Bee

bEAd

Each line in the ! le contains a unique word composed only of letters. Each word has at least one letter. There are no blank lines in the ! le. Words are case-sensitive (e.g., ‘bee’ and ‘Bee’ are considered to be di" erent words.)

Given the example ! le above opened for reading and the "letter to points" dictionary above, function build_word_to_score should return this "word to score" dictionary:

{‘a’: 1, ‘BE’: 3, ‘bee’: 4, ‘Bee’: 4, ‘bEAd’: 6}

Complete the function build_word_to_score according to the example above and the docstring below. You may assume the given ! le has the correct format and the "letter to points" dictionary contains all 26 lowercase letters.

def build_word_to_score(word_file: TextIO, letter_to_points: Dict[str, int]) -> Dict[str, int]:

"""Return a "word to score" dictionary, where each key is a word from word_file and

each value is the score for that word based on the points in letter_to_points.

"""

Question 5. [7 marks]

In this question, you will implement two di" erent algorithms for solving the same problem. Given a list of integers in which each item occurs either once or twice, count the number of items that occur twice.

Part (a) [4 marks]

Complete this function according to its docstring by implementing the algorithm below:

1. Use an integer accumulator to keep track of the number of matches.

2. For each item in lst, compare it to all other items in lst and if the item matches an item other than itself, add to the number of matches.

3. Return the number of duplicates.

Note: you must not use any list methods, but you may use the built-in function len().

To earn marks for this question, you must follow the algorithm described above.

def count_duplicates_v1(lst: List[int]) -> int:

"""Return the number of duplicates in lst.

Precondition: each item in lst occurs either once or twice.

>>> count_duplicates_v1([1, 2, 3, 4])

0

>>> count_duplicates_v1([2, 4, 3, 3, 1, 4])

2

"""

Part (b) [3 marks]

Complete this function according to its docstring by implementing the algorithm below:

1. Use a dictionary accumulator to track items and the number of times they occur in lst.

2. For each item in lst, if it is not already a key in the dictionary, add it along with a value of 1. If it is already a key in the dictionary, increase the value associated with it by 1.

3. Return the di" erence between the number of items in lst and the number of items in the dictionary.

Note: you must not use any list methods, but you may use the built-in function len().

To earn marks for this question, you must follow the algorithm described above.

def count_duplicates_v2(lst: List[int]) -> int:

"""Return the number of duplicates in lst.

Precondition: each item in lst occurs either once or twice.

>>> count_duplicates_v2([1, 2, 3, 4])

0

>>> count_duplicates_v2([2, 4, 3, 3, 1, 4])

2

"""

Question 6. [4 marks]

Part (a) [3 marks] Consider using a List[List[int]] to represent a square grid of integers (as we did in A2 to represent an elevation map). Complete the function swap_rows_and_columns according to its docstring below.

def swap_rows_and_columns(grid: List[List[int]]) -> None:

"""Modify grid so that each row in grid is swapped with the corresponding column.

For example, row 0 is swapped with column 0, row 1 is swapped with column 1, and so on.

Precondition: each list in grid has the same length as grid and len(grid) >= 2

>>> G2 = [[1, 2],

... [3, 4]]

>>> swap_rows_and_columns(G2)

>>> G2 == [[1, 3],

... [2, 4]]

True

>>> G3 = [[1, 2, 3],

... [4, 5, 6],

... [7, 8, 9]]

>>> swap_rows_and_columns(G3)

>>> G3 == [[1, 4, 7],

... [2, 5, 8],

... [3, 6, 9]]

True

"""

Part (b) [1 mark] Circle the term below that best describes the running time of the swap_rows_and_columns function.

constant                 linear                quadratic              something else

Question 7. [6 marks]

For this question, a "person to friends" dictionary (Dict[str, List[str]]) has a person’s name as a key and a list of that person’s friends as the corresponding value. You can assume that each person has at least one friend.

Complete the function complete_person_to_friends according to its docstring below. Do not modify the provided code and only write your code after the provided code.

def complete_person_to_friends(p2f: Dict[str, List[str]]) -> None:

"""Modify the "person to friends" dictionary p2f so that all friendships are

bidirectional. That is, if person B is in person A’s list of friends, then

person A must be in person B’s list of friends. The friend lists must be

sorted in alphabetical order.

>>> friend_map = {‘JC’: [‘MB’]}

>>> complete_person_to_friends(friend_map)

>>> friend_map == {‘JC’: [‘MB’], ‘MB’: [‘JC’]}

True

>>> friend_map = {‘B’: [‘A’, ‘D’], ‘A’: [‘B’, ‘C’, ‘D’]}

>>> complete_person_to_friends(friend_map)

>>> friend_map == {‘B’: [‘A’, ‘D’], ‘A’: [‘B’, ‘C’, ‘D’], \

‘D’: [‘A’, ‘B’], ‘C’: [‘A’]}

True

>>> friend_map = {‘JC’: [‘MB’], ‘MB’: [‘JW’]}

>>> friend_map == {‘JC:’: [‘MB’], ‘MB’: [‘JC’, ‘JW’], ‘JW’: [‘MB’]}

>>> complete_person_to_friends(friend_map)

True

"""

keys = list(p2f.keys())

for person in keys:

Question 8. [7 marks]

Consider the function below:

def get_first_even(items: List[int]) -> int:

"""Return the first even number from items. Return -1 if items contains

no even numbers. The number 0 is considered to be even.

"""

Part (a) [5 marks] Add ! ve more distinct test cases to the table below. Do not test if the input, items, is mutated. No marks will be given for duplicated test cases. The six test cases below are not intended to be a complete test suite.

Test Case Description                            items                     Expected Return Value

The empty list                                         []                                       -1

Part (b) [2 marks] A buggy implementation of the get_first_even function is shown below. In the table below, complete the two test cases by ! lling in the three elements of items. The ! rst test case should not indicate a bug in the function (the test case would pass), while the second test case should indicate a bug (the test case would fail). Both test cases should pass on a correct implementation of the function. If appropriate, you may use test(s) from Part (a).

def get_first_even(items: List[int]) -> int:

even_number = -1

for item in items:

if item % 2 == 0:

even_number = item

return even_number

                                                     items               Expected Return Value                 Actual Return Value

Does not indicate bug (pass)              [,,]

Indicates bug (fail)                            [,,]




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图