代做COMP10001 Foundations of Computing Final Exam, Semester 1 2023代写Python语言

COMP10001 Foundations of Computing

Final Exam, Semester 1 2023

Instructions to Students: This exam contains 100 marks, and counts for 50% of your final grade. Be sure to write your student number clearly in all places where it is requested.

This assessment is closed book, and you may not make use of any printed, written, electronic, or online resources.

All questions should be answered in the spaces provided on the exam paper.  There is also an overflow page available at the end of the paper. If you write any answers there, be sure to also write the corresponding question number.

There are a number of places where you are asked for a single Python statement, but two or more answer lines are provided.  In these cases you may draft your answer in one line and then copy it neatly to another line within the answer boxes if you wish, but if you do so you must then cross out the first draft answer.

You must not communicate with any other student in any way from the moment you enter the exam venue until after you have left the exam venue. All phones and other network, communication, and electronic devices must be switched completely off while you are in the exam room.

All material that is submitted as part of this assessment must be completely your own unassisted work, and undertaken during the time period allocated to the assessment.

Calculators and dictionaries are not permitted.

In your answers you may make use of built-in functions, but may not import functions from any other libraries.

You may use any blank pages to prepare drafts of answers, but you must copy those answer onto the correct answer box before the end of the exam.

Question 1 (20 marks)

Each subquestion is worth 2 marks. To get full marks you must specify both the correct value and be clear in your answer that you understand what the type will be. If you think an expression is not legal according to the rules of Python, you should write “Error” in the box. If you want to include any blanks in output strings, draw one  symbol for each blank character.

(a)    2  +  3  *  4  //  5

(b)    4  -  12  %  5  *  3

(c)     [1,  3,  4]  *  3

(d)    "a"  +  "fine"  +  "day"

(e)    "sunshine"[2:5]

(f)    (1,  2,  5,  4,  8,  7)[2:][1]

(g)    sorted("raining")

(h)    [x/2  for  x  in  range(1,5)]

(i)    set("apples")  -  set("peas")

(j)    (False,)  *  (True  +  True)

Question 2 (20 marks)

Each subquestion is worth 4 marks. There are two lines provided for each answer, but you should give a single Python assignment statement if you can.   If your answer requires more than one assignment statement you will be eligible for partial marks.

(a)    Suppose that vals is a Python list. Give a Python assignment statement that assigns True to even   size if vals has an even number of items in it, and assigns False if not.

(b)    Suppose that vals is a Python list of numbers.   Give a Python assignment statement that assigns True to all equal if all of the values in vals are the same, and assigns False if not.

(c)    Suppose that n is a positive integer.  Give a Python assignment statement that creates a list list of tup containing n tuples, with each tuple containing n values all of which are zeros.

(d)    Suppose that text is a Python string.  Give a Python assignment statement that assigns the number of digit characters in text to the variable n digits.

(e)    Suppose that nums is a Python list of numbers. Give a Python assignment statement that cre- ates a new version of nums in which 1 has been added to the first element in nums, 2 has been added to the second element, 3 to the third element, and so on through the remaining elements.

Question 3 (15 marks)

The following function reformats text so that input lines of any length are formed into a paragraph in which all of the lines are of roughly equal length, except for the last one.


def formatter(lines, linelen=DEF_LINE_LEN): # 01 ’’’ Takes input ‘lines‘ and restructures their words into output lines that are as long as possible without exceeding ‘linelen‘, creating a left-justified formatted paragraph. Adjacent whitespaces are replaced by single blanks; output lines start/end with non-blanks; and too-long-words are placed on lines by themselves and allowed to break the right margin. ’’’ # first break the input into a sequence of words XXXXXXXXXX # 02 for line in lines: # 03 XXXXXXXXXX # 04 all_words += words # 05 # then build the output lines out_lines = [] # 06 out_line = "" # 07 XXXXXXXXXX # 08 # try and fit the next word XXXXXXXXXX # 09 # can’t place next word, so need to start a new line out_lines.append(out_line) # 10 out_line = "" # 11 # now know we should have space in out_line if len(out_line) > 0: # 12 out_line += " " # 13 out_line += word # 14 # might still have a partial line XXXXXXXXXX # 15 out_lines.append(out_line) # 16 # all done, time to finish up return out_lines # 17


There are five locations in the function where one line of Python code has been replaced by XXXX symbols. The number of X’s should not be used as an indication of the length of the text that has been covered up.

In the boxes below, write the Python statement that has been covered up at each of the named locations. Each subquestion is worth 3 marks.

(a)    What has been covered up at line 02? 

(b)    What has been covered up at line 04?

(c)    What has been covered up at line 08?

(d)    What has been covered up at line 09?

(e)    What has been covered up at line 15?

Question 4 (15 marks)

Consider the following Python function, which is designed to find the most frequently occurring vowel in some given string text. There are five vowels in English: a, e, i, o, and u.


def most_freq_vowel(text): # 01 ’’’ Return the most frequently occurring vowel in ‘text‘ in a case insensitive manner (that is, ’a’ should be counted the same as ’A’). If there is a tie, return the vowel that appears first in the English alphabet, and if there are no vowels at all, return None. ’’’ vowels = "eeiou" # 02 counts = {} # 03 for char in text: # 04 if char in vowels: # 05 counts[char] += 1 # 06 if len(counts) != 0: # 07 return None # 08 max_count = max(counts.values()) # 09 for vowel in vowels: # 10 if counts[vowel] > 0 and counts[vowel] == max_count: # 11 return vowel # 12 return None # 13


Unfortunately, the function contains a number of errors. The subquestions on the next two pages are designed to help you first identify those errors, and then correct them.


(a)    [4 marks] Trace the computation that is performed for the function call

most_freq_vowel("hello")

to determine what is returned.  Show both the value and type in the answer that you provide.  Or, if you think an exception will be raised before the function returns, indicate in English what error will arise (for example, “divide by zero”, or “index out of range”, or invalid method for type int”, and so on).

Now write the output that should have been returned for that input if the function was working correctly.

Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is to change, and then providing one replacement line that corrects the error.

(b)    [4 marks] Next, trace the function call

most_freq_vowel("fly")

to determine what is returned (or, if the function does not return, describe the error that occurs), providing the same details required in part (a).

Now write the output that should have been returned for that input if the function was working correctly.

Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is to change, and then providing one replacement line that corrects the error.


(c)    [4 marks] There are two further errors present in the function that are not revealed by the tests shown in parts (a) and (b) of this question. In answering this part of the question, you should assume that the errors already identified in parts (a) and (b) have been fixed.

Write a function call that would expose one of these other two errors (you may choose either).

Describe what the result of that function call should be, and what it would actually be as a result of this error.

Finally, identify a minimal fix for this error, by giving exactly one line number in the code that is to change, and then providing one replacement line that corrects the error.

(d)    [3 marks] Now locate the other error in the function, and briefly describe it in one sentence. You do not need to give a function call that would expose this error.

Now identify a minimal fix for this error, by giving exactly one line number in the code that is to change, and then providing one replacement line that corrects the error.


Question 5 (30 marks)

Recall the Matching Game from Assignment  1, which featured a number of colored pieces on a two-dimensional board, with the board represented in Python by a list of lists.  Each piece was represented as a string containing a single upper-case character between ’A’ and ’Y’ . The character ’Z’ was used to indicate a blank position on the board.  For example, a board might have four columns and three rows and look like this:

board  =  [[’B’,  ’G’,  ’B’,  ’Y’], 

                [’G’,  ’B’,  ’Y’,  ’Y’], 

                [’G’,  ’G’,  ’Y’,  ’Z’]]

In this question you will implement a number of powerups” – functions that manipulate the board in ways that would not normally be permitted by the rules of the game.  You are not required to include comments or docstrings in your functions, but may if you wish.

(a)    [6 marks] Write a Python function row_destroyer(board,  row)

that takes two parameters:  board, a list of lists representing a game board; and row, the index position of a row on the board. You may assume that row has a valid value for the given board.

Your function should alter the board so that all of the pieces in the row specified by row are replaced by blanks (that is, the character ’Z’).

For example:

>>> board  =  [[’B’,  ’G’,  ’B’],  [’G’,  ’B’,  ’Y’],  [’G’,  ’G’,  ’Y’]]

>>>  row_destroyer(board,  1)

>>> print(board)

[[’B’,  ’G’,  ’B’],  [’Z’,  ’Z’,  ’Z’],  [’G’,  ’G’,  ’Y’]]



(b)    [8 marks] Now write a Python function piece_destroyer(board, piece)

that takes two parameters: board, a list of lists representing a game board; and piece, a single- character string representing a piece.

Your function should alter the board so that all pieces that have the same color as piece are replaced by blanks (that is, the character ’Z’).

For example:

>>> board  =  [[’B’,  ’G’,  ’B’],  [’G’,  ’B’,  ’Y’],  [’G’,  ’G’,  ’Y’]]

>>> piece_destroyer(board,  ’B’)

>>> print(board)

[[’Z’,  ’G’,  ’Z’],  [’G’,  ’Z’,  ’Y’],  [’G’,  ’G’,  ’Y’]]



(c)    [8 marks] Now write a Python function put_in_order(board)

that takes one parameter: board, a list of lists representing a game board.

Your function should rearrange the pieces on the board so that they are placed in alphabetically sorted order starting from the lowest index position.

Here are two possible execution sequences:

>>> board  =  [[’A’,  ’C’,  ’E’],  [’D’,  ’F’,  ’H’],  [’B’,  ’G’,  ’I’]]

>>> put_in_order(board)

>>> print(board)

[[’A’,  ’B’,  ’C’],  [’D’,  ’E’,  ’F’],  [’G’,  ’H’,  ’I’]]

>>> board  =  [[’B’,  ’G’,  ’B’],  [’G’,  ’B’,  ’Y’],  [’G’,  ’G’,  ’Y’]]

>>> put_in_order(board)

>>> print(board)

[[’B’,  ’B’,  ’B’],  [’G’,  ’G’,  ’G’],  [’G’,  ’Y’,  ’Y’]]


(d)    [8 marks] Finally, write a Python function

connected_destroyer(board,  row,  col)

that takes three parameters: board, a list of lists representing a game board; row, an index value for a row in the board; and col, an index value for a column in the board.  You may assume that row and col have valid values for the given board.

Your function should modify the board such that the piece at row, col is replaced by a blank (’Z’).

Furthermore, any piece that is immediately above, below, left or right of a piece that was replaced and has the same color as the piece that was replaced should also be replaced by a blank.

This process should continue until no more replacements are possible.

Here are three possible execution sequences:

>>> board  =  [[’B’,  ’B’,  ’A’],  [’B’,  ’A’,  ’A’],  [’A’,  ’A’,  ’B’]]

>>>  connected_destroyer(board,  0,  0)

>>> print(board)

[[’Z’,  ’Z’,  ’A’],  [’Z’,  ’A’,  ’A’],  [’A’,  ’A’,  ’B’]]

>>> board  =  [[’B’,  ’B’,  ’A’],  [’B’,  ’B’,  ’A’],  [’A’,  ’A’,  ’B’]]

>>>  connected_destroyer(board,  1,  1)

>>> print(board)

[[’Z’,  ’Z’,  ’A’],  [’Z’,  ’Z’,  ’A’],  [’A’,  ’A’,  ’B’]]

>>> board  =  [[’B’,  ’B’,  ’B’],  [’B’,  ’A’,  ’B’],  [’B’,  ’B’,  ’A’]]

>>>  connected_destroyer(board,  0,  0)

>>> print(board)

[[’Z’,  ’Z’,  ’Z’],  [’Z’,  ’A’,  ’Z’],  [’Z’,  ’Z’,  ’A’]]

 



热门主题

课程名

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
站长地图