data编程辅导 、讲解 python设计编程
or on macOS type:
The meaning of the terms on this line is:
python or python3 The python interpreter. On macos this will be python3
and on Windows, this will be py or python.
contact.py The name of the python program.
DataSet0.txt Name of the data file.
Table 1 Running the program, specifying the file name on the command line
Prompting the user for a file name (possible, but perhaps a bit painful to test)
To prompt the user for a file name, simply run the program in your editor (IDE) as you would normally.
Section 1: Orientation
You have been given several files containing testing and contact tracing data for vampire infiltrations. This data has a
“preamble” (initial part), followed by a list of testing and contact tracing data for a series of days (“body”). The
format will be explained in detail below.
In order to work with the contact tracing data, you will need to load a data file storing contact tracing data and create
appropriate data structures such as a dictionary or a list. These data structures can be used to identify the
relationship between the individuals in the data.
Many of the sections require you to print out data after you have calculated it, often in separately-specified
functions. Sometimes functions will require you to check for some kinds of data errors, print specified error
messages, and then exit by calling sys.exit().
Important: do not modify the main() function; all of your work should be in the other functions as specified in the
various sections. Do not add code that just runs at the “top level”. All of your code should be inside functions.
Important: Part of our testing procedure is to run functions individually. Make sure that your function names,
parameters, and return values don’t change from our specification. Moreover, make sure that your functions do
what they are supposed to do, rather than e.g., trying to do one big function that does the job of several of our
specified functions.
Important: Ensure that your program’s output matches exactly the output given to you unless otherwise specified.
Important: do not import any additional packages; lines 7-9 of the template.py file already imports sys, os.path,
and format_list; that’s all you need (and all you are allowed).
Data files
The files (as well as your assignment’s expected output for each file) are posted on moodle with the assignment
information. Each file ends with the .txt extension. You can open the files in Visual Studio Code or in a text editor to
see the contents.
4 VERSION 1.4.2 AS OF MARCH 9, 2024
Preamble
The preamble begins with a (comma-separated) list of individuals (without specifying which are humans
and which are vampires). These are all of the individuals that may appear in the rest of the dataset. On the
next line, the preamble gives the number of days of data that will follow. For example, a valid preamble
would be as follows:
Bella, Edward, Jacob, Carlisle, Alice, Emmett, Charlie, Renee, Jessica,
Angela
4
N.B. Sometimes spaces are included around names to aid human readability as in the above example; these are
optional, but your code will have to remove them if they appear. The strip() method can help you do this.
Important: names may include a mixture of upper and lowercase letters, as well as some special characters such as
dashes. Names will not include numbers, commas, colons, the newline character (“\n”), tabs (“\t”), or the symbol #.
Spaces may appear within a name, e.g. “Kuan Yew”, but not at its beginning or end, e.g. no “ Kuan Yew ”.
Day
Unlike in some of the legends, our vampires are able to move around during the day, rather than sleeping in coffins.
(Some of them do sleep in coffins at night, but that’s not important for this exercise.) We divide each of our days into
two parts (AM, when testing occurs; and PM, when contact occurs). Each day has the following format. On the first
line is a comma-separated list of people who have been given a vampirism test in the morning (AM) of that day
(perhaps a brief exposure to sunlight); the result of the test (“V” for vampire or “H” for human) is given after each
name, separated by a colon. If no one is given a test on a given day, this line will be the special string ##.
On the second line is a number of groups of people who had interactions in the afternoon (PM) of that day. On the
third and following lines are comma-separated lists of individuals who have been in contact. To keep things simple:
1. Each individual can be in at most one contact group on a given day.
2. On the other hand, there’s no need for an individual to meet with anyone on a given day; in that case, their
name won’t appear in any contact list for that day. When vampires are in the neighbourhood, staying home
alone can be the safest option!
3. All people in a group meet simultaneously (perhaps to play a game of baseball).
Here’s an example of a valid day:
Edward : V, Bella : H, Jacob : H, Jessica : H
3
Bella, Edward
Charlie, Jacob
Alice, Renee
N.B. Again, spaces can appear to aid human readability; in this case you’ll have to remove them.
To complete the demonstration of the input, here are the remaining three days promised in the preamble above:
5 VERSION 1.4.2 AS OF MARCH 9, 2024
Bella : H, Jacob:H, Alice :V, Charlie: H
2
Bella, Edward, Charlie
Angela, Jessica
Emmett : V, Renee : H, Jessica : V
0
Bella : V, Charlie: H
2
Bella, Edward, Charlie, Jessica, Angela
Jacob, Renee
Notice that on the third day everyone stayed home (0 contract groups). None of the days had zero tests (which we
would have indicated with ##).
Note: the above file is in fact one of the input testing files we give (DataSet1.txt).
Section 2: Check the file exists (1 mark)
Take a look at the following code in main():
The python code sys.exit() will cause the program to terminate if the file cannot be opened and read. Important: do
not change the code in main().
The function file_exists() takes the file name given as a parameter, and checks that the file exists. Your first task
is to complete the function file_exists(). The function file_exists()must return True if the file exists and
False if it does not. Hint: use the function isfile() from the python library os.path.
Section 3: Create a structure to hold the input data (6 marks for correctly storing
the data + 1 mark for correctly printing the error message when needed)
Complete the function parse_file(). This function takes a file name as a parameter, reads the file line by line and
creates a data structure to represent the input. The structure has the following format: at the “top” it is a pair. The
first element of the pair is a list of names (the participants) in the order given in the file. The second element of the
pair is a list of pairs; each list cell will correspond to a given day.
Within each list-pair, the first element should be a dictionary whose keys are the names of those tested for vampirism
and whose values are the Booleans True (that is, a vampire, indicated by “V” in the input file) or False (that is, a
human). The second element of a list-pair is a list of lists, with each “outer” list-element being an “inner” list of those
groups who were in contact on that day. After you finish processing the file, make sure to close it, and then return
the structure you’ve created. Here’s the structure we’d expect for the sample file given above, as printed by Python:
6 VERSION 1.4.2 AS OF MARCH 9, 2024
(['Bella', 'Edward', 'Jacob', 'Carlisle', 'Alice', 'Emmett', 'Charlie',
'Renee', 'Jessica', 'Angela'], [({'Edward': True, 'Bella': False, 'Jacob':
False, 'Jessica': False}, [['Bella', 'Edward'], ['Charlie', 'Jacob'],
['Alice', 'Renee']]), ({'Bella': False, 'Jacob': False, 'Alice': True,
'Charlie': False}, [['Bella', 'Edward', 'Charlie'], ['Angela', 'Jessica']]),
({'Emmett': True, 'Renee': False, 'Jessica': True}, []), ({'Bella': True,
'Charlie': False}, [['Bella', 'Edward', 'Charlie', 'Jessica', 'Angela'],
['Jacob',
'Renee']])])
If the file isn’t somehow formatted correctly – for example, if one of the numbers isn’t a number when you convert it
to an integer – then print the error message
Error found in file, aborting.
Then, exit (using sys.exit()).
Important: for this section, and all sections that follow, you should print to the screen (rather than, e.g., writing your
results to a file). Also, it is critical that you produce the exact error message we specify to get the mark.
Section 4: Pretty-print the data structure (4 marks)
Please fill in the function body of the function pretty_print_infiltration_data(data). By “pretty,”
what we mean is in a more human-readable format than the Python default shown above. Specifically, we’re
expecting something in the following exact format (the reason one line is in red is explained below):
Vampire Infiltration Data
4 days with the following participants: Alice, Angela, Bella, Carlisle,
Charlie, Edward, Emmett, Jacob, Jessica and Renee.
Day 1 has 4 vampire tests and 3 contact groups.
4 tests
Bella is human.
Edward is a vampire!
Jacob is human.
Jessica is human.
3 groups
Bella and Edward
Charlie and Jacob
Alice and Renee
Day 2 has 4 vampire tests and 2 contact groups.
4 tests
Alice is a vampire!
Bella is human.
Charlie is human.
Jacob is human.
2 groups
Bella, Charlie and Edward
7 VERSION 1.4.2 AS OF MARCH 9, 2024
Angela and Jessica
Day 3 has 3 vampire tests and 0 contact groups.
3 tests
Emmett is a vampire!
Jessica is a vampire!
Renee is human.
0 groups
Day 4 has 2 vampire tests and 2 contact groups.
2 tests
Bella is a vampire!
Charlie is human.
2 groups
Angela, Bella, Charlie, Edward and Jessica
Jacob and Renee
End of Days
There are a lot of parts here, so let’s take them one at a time.
As you can see, the printout begins with “Vampire Infiltration Data” and ends with “End of Days”.
The next line, in red, is actually a single line, but to avoid small fonts in this writeup, it appears to be broken over two
lines. (You shouldn’t try to change the printing colour in your solution; it’s just here for clarity on this issue.) All of
the other lines for this dataset are short and thus do not have this issue.
There are many lists of participants, separated by commas and with a final “and”. You have been provided with
module format_list.py which contains the function format_list(data). Use the function
format_list(data) to format a list as a string in this way.
The second line (in red) in the printout gives the information that was in the preamble of the input file. Afterwards,
we have a series of days, which begin with the day number, the number of vampire tests, and the number of contact
groups. Notice that when there is only 1 test or group, there is no “s” after “test” or “group.” In general, we make
sure to get all of the plurals correct, in the printout. Hint: you should too.
Within a day, we start by repeating the number of tests. The line is indented to the right by two spaces. Then we
give the results of each test, with the participants grouped in alphabetical order. Participant results are indented by
four spaces. Then we repeat the number of groups (two space indent), and then a list of each of the groups (four
space indent).
Hints: There are a number of other things to be careful of, like periods at the end of sentences (but not at the end of
lists), and so forth. It’ll take some careful work to make sure you have the format exactly right.
Other than within a list of test results, participants should appear in the same order as the test file. (The test results
were stored in a dictionary, which does not guarantee that order will be preserved. Rather than require that you
somehow restore the original order, we simply require that you alphabetize the participants. Reminder: if you have a
list mylst, then mylst.sort() will sort it; there are other ways to sort it too.)
Important: check the format of the output expected for the files given to you; unless we specify otherwise we expect
you to follow this format exactly. The output expected for each data file can be found in the corresponding file name
with the word “out” in the name. For example: the output for DataSet1.txt is given in DataSet1-out.txt.
8 VERSION 1.4.2 AS OF MARCH 9, 2024
Section 5: Write lookup helper function (1 mark for correctness)
To analyse the infiltration carefully, we are going to need a notion of time. Recall that days are divided into two
periods: AM, when testing occurs; and PM, when contact occurs. We also have a special “initial” period, before the
scenario begins. This can be a bit unwieldy, so it’s better to have a more uniform treatment. Therefore, we represent
units of time with integer values as follows:
a. When time = 0, we are in the initial period, also known as day 0.
b. When time = 1, we are just after the AM tests on day 1.
c. When time = 2, we are just after the PM contacts on day 1.
d. When time = 3, we are just after the AM tests on day 2.
e. When time = 4, we are just after the PM contacts on day 2.
f. And so forth…
Accordingly, given d days, we will have 1 + 2d time units.
We’ve provided some useful functions to navigate this in the format_list.py file:
1. time_of_day(d,b) to convert a day (integer) and AM/PM time (Boolean, with True for AM and False for
PM) into one of these time codes (AM/PM doesn’t matter for day 0).
2. day_of_time(t) to convert a time unit into a day.
3. period_of_time(t) to convert a time unit into an AM/PM (will return AM for day 0)
4. is_initial(t) to check whether t is the initial period (i.e., 0)
5. str_time(t) to convert a time period to a string for printing, e.g. “3 (AM)” (i.e., day 3 in the morning).
When t = 0 this returns just “0” (since day 0 does not have an AM or PM).
Your task is to write the helpful to have a lookup function contacts_by_time(participant, time,
contacts_daily). The first parameter is the name of a participant, e.g. “Bella”. The second parameter is a
time unit, e.g. 4 (which represents day 2 PM). The third parameter is a list of the contact groups on each day. Notice
that the third parameter is indexed by day, with day 1 in index spot 0; accordingly, part of your task is to convert the
time unit parameter to the day, and then adjust to the list index. The other part of your task is to search within the
contact groups for that day to find the correct list for the given participant. If the participant didn’t meet with
anyone on that day, then their contact list is empty. As a special case, contacts for day 0 should also be considered
the empty list for every participant. Once you have the correct list, return it.
To help you test your function, there is some code in main() that examines the initial participant on an early day, in
both the AM and PM (which should return the same list). In the specific dataset we’ve used so far, main() will print
Bella's contacts for time unit 3 (day 2) are Bella, Charlie and Edward.
Bella's contacts for time unit 4 (day 2) are Bella, Charlie and Edward.
Section 6: Create the initial vampire knowledge data structure and a way to
pretty print it (1 mark for correctness + 2 marks for pretty-printing)
Our goal will be to identify vampires and humans using logical deduction from data. We have no interest in
guesswork and thus no interest in concluding that someone is probably a vampire (or human); we want certainty.
9 VERSION 1.4.2 AS OF MARCH 9, 2024
Certainty is hard to come by, but hard does not mean impossible. Over the remainder of this assignment, we will
explain some basic logical principles that justify various deductive steps. Our goal in this section is to set up the core
vampire knowledge data structure (“vk structure”) we will need to track the information we’ve learned.
Logical principle 1: at a given point in time t, reality is binary. That is, every individual is either a human or a vampire
at time t: no one can be both a human and a vampire at the same time. On the other hand, humanness is essentially
a state of grace, and thus easy to lose over time (e.g., a human at time t can be a vampire at time t + 1).
However, at a given point in time t, our knowledge about reality is ternary. At time t, every individual has one of
three statuses: definitely human (“H”), definitely vampire (“V”), or unclear (“U”). Here, definitely means with
certainty: there’s no chance that a definite human is “really” a vampire, or vice versa. This means that at a specific
moment in time t, once H or V status is established for an individual, it should never change for that time t for that
individual. Unclear status doesn’t mean the individual is both a human and a vampire (that’s impossible!); it just
means that we aren’t sure. On the other hand, as we make deductive steps, U status might change (to “V” or “H”).
To represent knowledge at time t, we use a dictionary where the keys are the participants’ names and the values are
one of three strings “H”, “V”, or “U”. Complete the function create_initial_vk(participants), where
participants is the list of participants, and which returns such a dictionary structure. Initially we have no data
about who is human and who is a vampire, so everyone’s initial status should be unclear (“U”).
The main() function will create 1 + 2d copies (where d is the number of days) of the initial vk structure to represent
the vampire knowledge at each point in time. Initially, as you’ve created it, everyone’s status is unknown at all times.
Complete the function pretty_print_vampire_knowledge(vk) to print the results of a vk structure. Here is an
example of what the output should look like on the initial vk structure:
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Each row should be indented by two spaces. Again, text in red should be understood to be on a single line. Notice
that format_list(data) helpfully returns the string “(None)” when the given list is empty. Also notice that
individuals are alphabetized within the various lists.
If you do this correctly, main() will use the function pretty_print_vks(), which in turn calls your
pretty_print_section_6(), to print out the initial vk structures for each point in time specified in the data.
Section 7: Create a function that updates a vk structure using the results of the
tests (1 mark for correctness + 2 marks for error reporting)
Logical principle 2: the vampirism tests given each day are conclusive: there’s no chance of a human being mistaken
for a vampire, or vice versa. On the other hand, other than by tests, there’s no direct way to tell who is a vampire
and who is a human. (There are, however, indirect ways via logical deduction, as will be covered below.)
10
VERSION 1.4.2 AS OF MARCH 9, 2024
Complete the function update_vk_with_tests(vk, tests). The first input to the function is a vk
structure of the kind you just created in section 6, that is, a dictionary mapping participants to “H”/“V”/“U”. The
second input is a subpart of the structure you created in section 3: specifically, it is a dictionary matching participant
names with the Boolean results of the tests on a given day: True for vampirism and False for humanism.
The rules for update are as follows. If an individual has unknown “U” status, then we update their status with “V” or
“H” depending on the result of the test. If a definite human (“H”) tests positive for vampirism, we’ve discovered an
error in the data and should output:
Error found in data: humans cannot be vampires; aborting.
On the other hand, if a definite vampire (“V”) tests negative for vampirism, we’ve discovered another kind of error in
the data and should output:
Error found in data: vampires cannot be humans; aborting.
Of course, if a human tests positive for humanness, then they remain human; likewise, if a vampire tests positive for
vampireness then they remain a vampire.
There’s one other possible error we need to check for: if we find that someone has a test, who is not a participant,
we should output:
Error found in data: test subject is not a participant; aborting.
In all cases when we find an error we should then exit (with sys.exit()) after printing the error message.
Lastly, anyone who hasn’t taken a test that day should keep the same H/V/U status that they already have.
Important: Assuming we don’t find an error, you should return the updated dictionary structure.
The main() function will print out the updated vk structure table accordingly. In the case of our example:
Vampire Knowledge Tables
Day 0:
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Day 1 (AM):
Humans: Bella, Jacob and Jessica
Unclear individuals: Alice, Angela, Carlisle, Charlie, Emmett and Renee
Vampire: Edward
Day 1 (PM):
Humans: (None)
11
VERSION 1.4.2 AS OF MARCH 9, 2024
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Day 2 (AM):
Humans: Bella, Charlie and Jacob
Unclear individuals: Angela, Carlisle, Edward, Emmett, Jessica and Renee
Vampire: Alice
Day 2 (PM):
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Day 3 (AM):
Human: Renee
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward and
Jacob
Vampires: Emmett and Jessica
Day 3 (PM):
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Day 4 (AM):
Human: Charlie
Unclear individuals: Alice, Angela, Carlisle, Edward, Emmett, Jacob,
Jessica and Renee
Vampire: Bella
Day 4 (PM):
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
End Vampire Knowledge Tables
Again, lines in red should be understood to be on one line rather than two.
You may notice a few things if you look at the data. First, on each day, the results of the tests are now reflected in the
AM portion. Thus, Bella and Jacob are human at noon on day 1, whereas Edward is a vampire. Second, the PM
portions of the vk structures haven’t been changed at all, since tests are only given in the AM. Third, we do not
“carry” the results of any tests to any other days; each day at this point of the analysis is fresh/independent.
Section 8: Create a function that updates a vk structures by pushing vampire
status forwards in time (2 marks for correctness + 1 mark for error reporting)
Logical principle 3a: vampirism has no cure. Accordingly, once an individual is confirmed to be a vampire at a given
time, they are certain to be a vampire ever afterwards.
12
VERSION 1.4.2 AS OF MARCH 9, 2024
Complete the function update_vk_with_vampires_forward(vk_pre, vk_post). Both inputs to the
functions are vk structures, but the second must occur after the first. Your goal is to take the vampire knowledge in
the former and propagate it to the latter. However, knowledge is precious: don’t accidentally erase definite
knowledge in the post as you go!
In particular, if participant p is a vampire before, then p must also be a vampire after. If this was already known in the
later period (perhaps a vampire was tested twice!), then p’s vampiric status is unchanged. If p’s status was unknown
in the later period, update it to vampire. On the other hand, if p’s status was definitely human, then you’ve
discovered an error in the data and should print out
Error found in data: vampires cannot be humans; aborting.
and then exit with sys.exit(). If p’s status is not a vampire in the earlier vk structure, then you shouldn’t
change anything in the later vk structure. We’re only concerned with pushing vampire status forward in time for
now.
Once you’ve calculated the updated post vk structure, return it. Main will then print out an updated structure, which
should look like this for our test dataset:
Vampire Knowledge Tables
Day 0:
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Edward,
Emmett, Jacob, Jessica and Renee
Vampires: (None)
Day 1 (AM):
Humans: Bella, Jacob and Jessica
Unclear individuals: Alice, Angela, Carlisle, Charlie, Emmett and Renee
Vampire: Edward
Day 1 (PM):
Humans: (None)
Unclear individuals: Alice, Angela, Bella, Carlisle, Charlie, Emmett,
Jacob, Jessica and Renee
Vampire: Edward
Day 2 (AM):
Humans: Bella, Charlie and Jacob
Unclear individuals: Angela, Carlisle, Emmett, Jessica and Renee
Vampires: Alice and Edward
Day 2 (PM):
Humans: (None)
Unclear individuals: Angela, Bella, Carlisle, Charlie, Emmett, Jacob,
Jessica and Renee
Vampires: Alice and Edward
Day 3 (AM):
Human: Renee
Unclear individuals: Angela, Bella, Carlisle, Charlie and Jacob
13
VERSION 1.4.2 AS OF MARCH 9, 2024
Vampires: Alice, Edward, Emmett and Jessica
Day 3 (PM):
Humans: (None)
Unclear individuals: Angela, Bella, Carlisle, Charlie, Jacob and Renee
Vampires: Alice, Edward, Emmett and Jessica
Day 4 (AM):
Human: Charlie
Unclear individuals: Angela, Carlisle, Jacob and Renee
Vampires: Alice, Bella, Edward, Emmett and Jessica
Day 4 (PM):
Humans: (None)
Unclear individuals: Angela, Carlisle, Charlie, Jacob and Renee
Vampires: Alice, Bella, Edward, Emmett and Jessica
End Vampire Knowledge Tables
It’s clear that vampirism is monotone increasing over time, and indeed it seems to be rather infectious: by the end of
the fourth day, at least half of the town is a vampire! On the other hand, we still don’t know much about the
humans involved, except those that test as such, and even then only on the morning of their test.
Section 9: Create a function that updates a vk structures by pushing human
status backwards in time (2 marks for correctness + 1 mark for error reporting)
Logical principle 3b: vampirism has no cure. A bit less obviously than in section 7, we can reuse this idea to deduce
that if an individual is confirmed to be a human at a given time, they are certain to have been a human up until that
point. In other words, while vampirism propagates forwards in time, humanness propagates backwards.
Complete the function update_vk_with_humans_backward(vk_pre, vk_post). Both inputs to the
functions are vk structures, but (as before) the second must occur after the first. Your goal is to take the human
knowledge in the latter and propagate it to the former – that is, information is in a sense flowing backwards in time.
Once again, knowledge is precious: don’t accidentally erase definite knowledge in the pre as you go!
In particular, if participant p is a human after, then p must also be a human before. If this was already known in the
earlier period, then p’s human status is unchanged. If p’s status was unknown in the earlier period, update it to
human. On the other hand, if p’s status was definitely vampire, then you’ve discovered an error in the data and
should print out
Error found in data: humans cannot be vampires; aborting.
and then exit with sys.exit(). If p’s status is not a human in the later vk structure, then you shouldn’t change
anything in the earlier vk structure. We’re only concerned with pushing human status backward in time for now.
Once you’ve calculated the updated pre vk structure, return it. main() will print out an updated structure, e.g.:
Vampire Knowledge Tables
Day 0:
Humans: Bella, Charlie, Jacob, Jessica and Renee
Unclear individuals: Alice, Angela, Carlisle, Edward and Emmett
14
VERSION 1.4.2 AS OF MARCH 9, 2024
Vampires: (None)
Day 1 (AM):
Humans: Bella, Charlie, Jacob, Jessica and Renee
Unclear individuals: Alice, Angela, Carlisle and Emmett
Vampire: Edward
Day 1 (PM):
Humans: Bella, Charlie, Jacob and Renee
Unclear individuals: Alice, Angela, Carlisle, Emmett and Jessica
Vampire: Edward
Day 2 (AM):
Humans: Bella, Charlie, Jacob and Renee
Unclear individuals: Angela, Carlisle, Emmett and Jessica
Vampires: Alice and Edward
Day 2 (PM):
Humans: Charlie and Renee
Unclear individuals: Angela, Bella, Carlisle, Emmett, Jacob and Jessica
Vampires: Alice and Edward
Day 3 (AM):
Humans: Charlie and Renee
Unclear individuals: Angela, Bella, Carlisle and Jacob
Vampires: Alice, Edward, Emmett and Jessica
Day 3 (PM):
Human: Charlie
Unclear individuals: Angela, Bella, Carlisle, Jacob and Renee
Vampires: Alice, Edward, Emmett and Jessica
Day 4 (AM):
Human: Charlie
Unclear individuals: Angela, Carlisle, Jacob and Renee
Vampires: Alice, Bella, Edward, Emmett and Jessica
Day 4 (PM):
Humans: (None)
Unclear individuals: Angela, Carlisle, Charlie, Jacob and Renee
Vampires: Alice, Bella, Edward, Emmett and Jessica
End Vampire Knowledge Tables
Humanism is monotone decreasing over time. We’re beginning to make real progress: we know that at least half the
town was human before the vampires arrived. We can also see that at least two humans (Bella and Jessica) became
vampires during the four days. In the next two sections we will consider how that might have happened.
Section 10: Create a function that updates a vk structures by propagating
human/vampire status forward overnight (1 mark for correctness + 1 mark for
error reporting)
Logical principle 4: everyone is safe at night. Unlike in the most traditional legends, our vampires don’t hunt at night.
This means that anyone who is still human at the end of a day is also human at the beginning of the next day.
15
VERSION 1.4.2 AS OF MARCH 9, 2024
Admittedly, at the moment this doesn’t change any of our knowledge tables, but it will when we combine it with the
more complicated reasoning specified in the next section.
Your task is to write update_vk_overnight(vk_pre, vk_post), which handles this reasoning. As before,
the vk structures are earlier and later, with the special understanding that the gap is overnight (between a PM and
the subsequent AM). There are two error conditions to check: first, if someone was a definite human in the
afternoon then tests for vampirism in the next morning, you should output
Error found in data: humans cannot be vampires; aborting.
Second, if someone was a definite vampire in the afternoon and test as human in the morning, you should output
Error found in data: vampires cannot be humans; aborting.
and then exit. Otherwise, propagate statuses (both human and vampire) forward. Return the new post vk structure.
Section 11: Create a function that updates a vk structures by approximating
contact results (4 marks for correctness + 2 marks for error reporting)
Logical principle 5: vampires are often hungry. Any time a vampire is in contact with a human, there is a chance (but
no guarantee) that they will turn/infect/sire them. Thus, it is dangerous for humans to hang out with vampires,
whether knowingly or not.
Vampires can be very hungry, so they can turn more than one human on an afternoon if they want to. On the other
hand, sometimes vampires resist their thirst and the humans remain safe despite engaging in risky behaviour like
attending the VGP (Vampire Graduation Party).
Vampires are also very sneaky: if they do turn someone, other humans in the contact group won’t know anything has
happened. In other words, they won’t learn that the sire/parent/infector is a vampire, or that the new
vampire/infectee has been turned. The new vampire might or might not feed on their first day.
Your task is to write update_vk_wit