IFB104 Practice Exam Solutions
The expected assignment statement is as follows.
coverage = abs(p – q) + abs(p – q)
You should be able to determine the necessary expression on the right-hand side by substituting
each of the intermediate variables’ expressions in the following assignment statements.
Aside: The code performs a meaningful calculation. It determines the “Manhattan distance”
between two coordinates (p , p ) and (q , q ).
1 2 1 2
There are two “obvious” solutions to this problem (although other more complicated ones are
possible). The first is as follows and draws the diagonal line first and then the circle.
left(180 + 45)
Another valid solution would be to draw the circle first, then the line.
left(90 + 45)
If you are unable to visualise for yourself the effect of executing these sequences of moves you
may want to test them in an appropriate Turtle program.
Once again there are two valid solutions to this question. One possible answer is the following
mystery([‘c’, ‘a’], [‘b’])
To see why this works you need to trace the way the arguments in the function call above are
passed through the code. In this specific example list [‘c’,’a’]is used as the value of
parameter puzzlein function mysteryand list [‘b’]is used as the value of parameter
conundrum. Function mysteryreturns puzzle’s second value, the letter ‘a’, as the first
item in the final list returned, which is the first part of our desired result.
The rest of the list is created by calling function paradoxwith list [‘b’]as the value of
parameter riddleand list [‘c’,’a’]as the value of parameter poser. (Be careful! Note
that the order of the parameters in the calls to functions mysteryand paradoxare reversed.)
Continuing to trace through the code in this way will show that the call to paradoxreturns
list [‘b’,’c’]and the call to mysteryabove returns [‘a’,’b’,’c’]as desired.
However, by observing that parameter conundrumalready has a default value of [‘b’]in
the definition of function mysterywe can solve this problem with an even simpler function
call, as follows.
Either way, you should check your solution by running the code before submitting your final
answer, of course.
To solve this kind of problem you should consider the conditions necessary to reach the desired
assignment statement(s). In this question we want to reach the assignment outcome =
‘Fair’. To do this condition price>1000in the outermost conditional statement must
be true and condition durability<4in the inner statement must be false. This makes the
necessary condition to reach the desired assignment equivalent to the following Boolean
price > 1000 and durability >= 4
Using this knowledge you then need to identify the options that satisfy this condition. In this
case there are two correct answers:
When priceis greater then 1000 and durabilityequals 5
When priceis greater than 1000 and durabilityequals 4
Aside: The code makes a meaningful decision. It favours scenarios with lower prices and
This is a ‘code comprehension’ question which tests your ability to understand program code
by reading it, as an experienced programmer can. Although you can try running the code,
doing so may not necessarily reveal its purpose. (This is why we place so much emphasis on
commenting your code. It’s easy to see what some code does, but it’s much harder to see why!)
To answer this question you need to study the code segment line-by-line to understand its
. It’s clear from the second line that it processes consecutive values from list numbers.
. The fifth line tells us that each number processed has its square root added to variable
. The third and fourth lines tell us that it stops iterating as soon as it encounters a negative
. Finally, variable sum_of_rootsis printed as the result.
Putting all these facts together tells us that the correct answer is:
It prints the sum of the square roots of all the numbers in the list up to the first
negative value, exclusive.
As is usually the case with pattern matching, the aim here is to construct a pattern that includes
the results of interest and excludes all others. Typically this means identifying features of the
text being searched that are unique to the desired results.
. In this case the three results we want are all enclosed in <a href=”…“>…</a>
tags, but this fact on its own is not enough to distinguish them because there are two
other such tags at the bottom of the text.
. However, the hyper-references before the three results of interest all begin with
mailto’ and an email address. This is enough to separate the results we want from
the other hyperlinked words.
One possible solution, therefore, is the following regular expression, expressed as a Python
‘mailto:[[email protected]]+”>([A-Za-z ]+)</a>’
Reading it left-to-right says:
. First match the string ‘mailto:’.
. Then match one or more characters consisting of lower-case letters, digits, the ‘at’
symbol and/or full stops.
. Next match the string ‘“>’.
. The part of the pattern to be returned appears next in round brackets and consists of one
or more letters (upper or lower case) and/or spaces.
. The pattern ends with the string ‘</a>’.
As you have experienced when completing the workshop exercises and assessment items,
getting a regular expression exactly right can be a tricky process, so you should consider using
a regular expression tool to test your solution before submitting your answer.
Comment: This is not the only or shortest regular expression that will do the job, but is arguably
Completing this question simply involves applying the programming skills you have gained
with Python’s tkinter module by completing the relevant workshop exercises and
To complete this particular program you need to (a) associate a new function with the
Duplicate’ button and (b) define this new function which copies text from the text entry box
into the label. The specific code needed to do this is as shown below, with the new parts
highlighted in red.
Function for copying text from the entry box into the label
users_text = data_entry.get()
data_copy[‘text’] = users_text
Create a push button that calls the above function when pushed
Button(main_window, font = big_font, text = ‘ Duplicate ‘,
command = duplicate,
activeforeground = ‘red’).\
grid(row = 2, column = 0, padx = 5, pady = 5)
Observation: It’s impossible to remember the many options and methods associated with the
various Tkinter widgets. You should be prepared to look online for help with programming
the specific Tkinter widgets used in the equivalent final exam question. There are many
tutorials and references for Tkinter online, some better than others, so you should familarise
yourself with a few good ones before the final exam.
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: [email protected] 微信:itcsdx