Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Final Examination, Semester 1, 2019

COMP10001 Foundations of Computing

Part 1: Code Interpretation

Question 1

Evaluate the following expressions, and provide the output in each case.

'smart  array'[::-1]

(b)

7 / 2 * 2

(c)

'cat'[1:] in 'hat' and 'fox'[1:] in 'sox'

(d)

sorted ({'hobbits': ['bilbo', 'frodo'],

'dwarves': ['gimli', 'thorin']}.values())[-1][1]

(e)

[x * 2 for x in str (90210)]

Question 2

What are the final values of each of the variables indicated below, on completion of execution of the following code:

(a)  i

(b) deck

(c) hands

Question 3

The following code is intended to calculate a list of valid old school” vs. “new school” superhero running races. The rules are:

1. old school” characters are defined as those who were created prior to a given year, and “new school” characters are those created in the given year or later

2. each old school superhero races against each new school superhero whose costume is a dif- ferent colour to their own

3. superheroes with the same colour costumes don’t race against one another—that would just be confusing! Such pairings are deemed invalid

The code takes the form of the definition of a function called race_list, which takes two argu- ments:

•  afile: a CSV file containing superhero data, including their name, the year they were cre- ated and their costume colour

year: the year used to distinguish old schoolfrom new school superheroes

and returns a 2-tuple containing:  (1) a sorted list of valid races (as strings); and (2) a count of

invalid pairings of superheroes (on the basis of their costumes being the same colour). An example input for afile is the file superheroes.csv:

name,year,colour

Hedgehog  Man,1965,brown

Captain  Penguin,1962,purple

The  Mystifier,1973,purple

Telephone  Girl,1978,green

Little  Llama,1991,beige

An example function call, based on the provided superheroes.csv le, is:

(ie, there is one invalid pairing in this example: Captain Penguin and The Mystifier do not race against one another because their costumes are both purple.)

You can assume that the following two library imports have been made earlier in the code, prior to the function definition:

As presented, the lines of the function are out of order. Put the line numbers in the correct order and introduce appropriate indentation (indent the line numbers to show how the corresponding lines would be indented in your code), by placing line numbers in the table below the code (one line number per row in the table). Note that the provided code makes use of a semi-colon (“;”) at two different locations, to combine two statements (to initialise variables) into a single line of code.

1 if old[pair[0]]   !=  new[pair[1]]:

2      old  =   {};  new  =   {}

3      with open (afile)  as  f:

4      new[row['name']]  =  row['colour']

5 else :

6      old[row['name']]  =  row['colour']

7 for pair in itertools.product(old,  new):

8       invalid  +=  1

9 if int (row['year'])  < year:

10       races.append(f'{pair[0]}  vs.   {pair[1]}')

11 for row in csv.DictReader(f):

12 else :

13 return (sorted (races),  invalid)

14 def race_list(afile,  year):

15       races  =   [];  invalid  =  0

Question 4

The following function is intended to work out the value of the optimal combination of items to place in a bag in terms of maximising the value of the items, subject to a weight limit on the bag. It takes two arguments:

capacity: the total weight that can be held by the bag

•  items: a list of items, each of which is represented as a dictionary storing the weight and value of that item

and returns the maximum value of combined items that can be achieved subject to the weight constraint.

For example, when run over the following example of items:

items  =   [{ 'weight ' :  13,   'value ' :  6},   { 'weight ' :  15,   'value ' :  11},  { 'weight ' :  16,   'value ' :  13},   { 'weight ' :  22,   'value ' :  17}, { 'weight ' :  10,   'value ' :  5}]

the function should produce the following outputs (based on the combination of items provided in the comment, in each case):

As presented, there are bugs in the code. Identify exactly three (3) errors in the code (using the provided line numbers), identify for each whether it is a syntax”, “run-time” or “logic” error, and provide a replacement line which corrects the error.

1 def rec_knapsack(capacity,  items):

2 if capacity  ==  0 or len (items)  ==  0:

3 return 0

4

5                      cur_item  =  items(0)

6

7 if cur_item['weight']  >=  capacity

8 return rec_knapsack(capacity,  items) 9

10                      take_value  =   (cur_item['value']

11                                      +  rec_knapsack(capacity  -  cur_item['value'],  items[1:]))

12                      leave_value  =  rec_knapsack(capacity,  items[1:])

13 return max (take_value,  leave_value)

Question 5

The code on the next page is intended to validate a CSV row as containing the following four fields:

1. a staff ID, in the form of a 5-digit number (e.g. 00525 or 19471)

2. a rst name, in the form of a non-empty ASCII string

3. a last name, in the form of an ASCII string, noting that the field may be blank (i.e. an empty string) if the individual does not have a last name

4. a plain text password, in the form of an ASCII string between 8 and 12 characters in length (inclusive), including at least one lower-case letter, one upper-case letter, and one punctua- tion mark from: (a) a comma (“ ,”), (b) a full stop (“ .”), (c) an exclamation mark (“!”), and

(d) a question mark (?”).

If a valid CSV row is provided to the code (as a string input), the output of the code should be a 4-element list of the four values, each as a string; if an invalid CSV is provided to the code, the output should be None.

The provided code is imperfect, in that it sometimes correctly detects valid and invalid rows, but equally, sometimes misclassifies a valid row as being invalid, and sometimes misclassifies an invalid row as being valid.

(a) Provide an example of a valid row that is correctly classified as such by the provided code (i.e. a valid row input where the return value is the row broken down into a list of valid fields):

(b) Provide an example of an invalid row that is correctly classied as such by the provided code (i.e. an invalid row input where the return value is None):

(c) Provide an vided code

example of an invalid row that is incorrectly classied as a valid row by the pro- (i.e. an invalid row input where the return value is an erroneous list of valid elds):

(d) Provide an vided code

example of a valid row that is incorrectly classied as an invalid row by the pro- (i.e. a valid row input where the return value is None):

Part 2: Generating Code

Question 6

Rewrite the following function, replacing the while loop with a for loop, but preserving the re- mainder of the original code structure:

def n_green_bottles(n):

while n  >  0:

b_word  =   'bottles'

if n  ==  1:

b_word  =  b_word[:-1]