Python代写 | IFB104 Practice Exam Solutions

本次Python代写主要是完成有关图形界面、模式匹配、异常等的习题
IFB104 Practice Exam Solutions  

Question 1  
The expected assignment statement is as follows.  
coverage = abs(p[0] q[0]) + abs(p[1] q[1])  
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  
Question 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.  
dot(10)  
left(180 + 45)  
forward(200)  
left(90)  
circle(100)  
Another valid solution would be to draw the circle first, then the line.  
dot(10)  
left(90 + 45)  
circle(100)  
left(90)  
forward(200)  
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.  
Question 3  
Once again there are two valid solutions to this question. One possible answer is the following  
function call.  
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.  
mystery([‘c’, ‘a’])  
Either way, you should check your solution by running the code before submitting your final  
answer, of course.  
Question 4  
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  
expression.  
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  
higher durability.  
Question 5  
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  
behaviour:  
1
2
. 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  
sum_of_roots.  
3
4
. The third and fourth lines tell us that it stops iterating as soon as it encounters a negative  
number.  
. 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.  

Question 6  
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.  
1
. 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.  
2
. 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  
string.  
‘mailto:[[email protected]]+”>([A-Za-z ]+)</a>’  
Reading it left-to-right says:  
1
2
. First match the stringmailto:’.  
. Then match one or more characters consisting of lower-case letters, digits, the ‘at’  
symbol and/or full stops.  
3
4
. 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.  
5
. 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  
the clearest.  
Question 7  
Completing this question simply involves applying the programming skills you have gained  
with Python’s tkinter module by completing the relevant workshop exercises and  
assessment items.  
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  
def duplicate():  
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.