You are on page 1of 5

In collaboration with Staffordshire University

SOFTWARE DEVELOPMENT AND APPLICATION


MODELLING
Software In-Class Test (Remote Alternative)

STUDENT COPY

Date: 09/03/2022 Module Code: COMP40003


Time: 10:00 Weighting: 50%
Duration: TWENTY- Examiner: James Stovold
FOUR hours

Instructions to candidates:

• You have TWENTY-FOUR hours for this exam, which is worth 50% of your overall
module grade.
• Answer all questions.
• Questions relating to the material of the exam should be sent to James before 12.00
noon in order to give sufficient time to reply to all students. Queries received after
this stage will not be answered.
• There are 100 marks overall. Question 1 is worth 50, question 2 is worth 50. No
partial marks will be awarded. Intermediate marks will be awarded.
• Your solution should be saved and submitted to Canvas as either a zip or tar.gz
file. N.B. rar files will not be accepted.
• You should only use Python 3 for this exam and are only permitted to use the
following external Python libraries: math, random, json, unittest.

1
1 (50 marks) Your code for this question should be stored in a file named “q1.py”

Doris has a large number of children. Each week she writes a list of tasks that need to be
completed by her children (e.g. sweep the floor, take out the bins, etc.). Her children, however,
are particularly mischievous and whoever gets the list first splits up the tasks among the other
children so that they don’t have to do any work themselves.

Each child can recruit a maximum of two other children to do their tasks for them, splitting
the tasks equally between them. For example, child 1 (with tasks [A,B,C]) can recruit child
2 and child 3 and allocate [A,B] and [C] to each of them. If a child has only one task, they
are not able to recruit another child to complete it, and must complete the task themselves.
If a child has an odd number of tasks, they should allocate the extra task to their first recruit.
Once a child has been recruited, they cannot be recruited by another child, even if they have
managed to recruit two other children and have no tasks to do themselves.

Given a list of tasks, and a maximum number of children, your task is to work out which tasks
are performed by which children.

(i) [25 marks]

(a) [20 marks] Write a function named “allocate_tasks” to allocate tasks to children.
Your function should accept as input (via an argument) a list of tasks and a maximum
number of children. It should return a list where the tasks allocated to the first child
are stored as a sub-list in index 0, the tasks allocated to the second child are stored as
a sub-list in index 1, and so on.

(b) [5 marks] Extend your function from part (a) to return a tuple containing both the
allocation and the structure of who was recruited by whom, and provide a second function
named “print_all” that takes the allocation and structure and prints them out to the
terminal/command prompt (i.e. using print) (see examples below).

(ii) [25 marks] Write a fully-formed testing document for the problem described above. Be sure
to include a list of requirements, test cases, and a test plan (detailing expected vs. actual
results).

2
Examples for Question 1

N.B. Unit tests are provided for these example cases

Example 1: Given tasks: [A,B,C,D,E,F], and a maximum of 4 children, the output from your
first function should be the list [ [], [C], [D, E, F], [A, B] ]. The output from your
second function should be as follows:

Allocations:
Child 1: []
Child 2: [C]
Child 3: [D,E,F]
Child 4: [A,B]

Recruitment:
1: [2, 3]
2: [4]
3:
4:

Example 2: Given tasks: [A,B,C,D,E,F], and a maximum of 6 children, the output from your
first function should be: [ [], [], [F], [A, B], [C], [D, E] ]. The output from your
second function should be:

Allocations:
Child 1: []
Child 2: []
Child 3: [F]
Child 4: [A,B]
Child 5: [C]
Child 6: [D,E]

Recruitment:
1: [2, 3]
2: [4, 5]
3: [6]
4:
5:
6:

3
2 (50 marks) Your code for this question should be stored in a file named “q2.py”

While Doris was allocating tasks to her children, a zombie apocalypse hit Earth. The fate
of the world, and humankind, rests in your ability to determine whether there are more zom-
bies or survivors in the world. Each street has kindly provided you with a breakdown of every
house on the street, and whether they contain zombies (z), survivors (s), or are empty (o).
These have been collated into different regions, such as wards, communes, provinces, etc.

(i) [25 marks] Write a function named “survival” that can determine whether there are more
zombies overall, more survivors overall, or an even number of the two. If there are more
zombies, the zombies will win; if there are more survivors, the humans will win; if they are
even, everybody will die and the Earth will be taken over by cats.

Your function should accept as an argument a multi-dimensional list of houses, and should
return an integer representing one of the three possible outcomes:

• -1 if zombies win

• 0 if cats win

• 1 if humans win.

Your function should also print out the relevant string before it returns: “Zombies will win.”,
“Humans will win.”, “Cats will win. Meow!”.

Once you have written the function, you should include (as a block comment in your code)
approximately 100 words describing your approach and why you chose to take that approach.
Please note that this section is mandatory.

(ii) [25 marks] Now you have your “survival” function that can determine whether zombies,
humans, or cats will inherit the Earth, you can start analysing the data that each region sends
you. This data is sent to you in json format and stored in files.

(a) [10 marks] You need to write a function named “read_survival_data” that can
take a file name as an argument, and will read that json file in, process the json data
and return a multi-dimensional list which can be passed to your function from part (i).

(b) [10 marks] Due to the nature of an apocalypse, some of the data may have been
corrupted in its transfer. This means that some of the streets may be accurate (i.e.
[o,z,s]) but other streets may be corrupted (i.e. [o,z,s,y]). Adapt your function
from part (a) to deal with this situation: if there is corrupted data (i.e. data other than
z, s, o) then it should skip that house and assume it is empty.

(c) [5 marks] If the json file is corrupted (i.e. it does not conform to standard json format),
then you should handle the issue and print out an appropriate message to the terminal.

4
Examples for Question 2

N.B. Unit tests are provided for these example cases

Example 1: Given a single street: [ o, o, o, z, o, o, z, o, s ], your function should


return -1 and print out “Zombies will win.” because there are two zombies, one survivor, and
six empty houses.

Example 2: Given a ward with only one street: [[ o, o, o, z, o, o, z, o, s ]], your


function should return -1 and print out “Zombies will win.” because there are two zombies,
one survivor, and six empty houses.

Example 3: Given a ward with multiple streets: [[o, o, o], [z, o, o], [z, o, s]],
your function should return -1 and print out “Zombies will win.” because there are two
zombies, one survivor, and six empty houses.

Example 4: Given a commune with one ward containing one street and a single house:
[[[o]]], your function should return 0 and print out “Cats will win. Meow!” because there
are no survivors or zombies.

END OF EXAM

You might also like