In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 3rd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #11 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
The sound of gunfire echoes through the hallway as Trinity sprints through a narrow alley, her boots clattering on the floor. An Agent is hot on her heels.
"Operator, I need an exit! Find me a booth, Tank, NOW!" she shouts.
Tank's voice crackles back.
"I'm trying, Trinity, but most of the connections are not working! Something’s wrong..."
The map of the current situation of Trinity in the alley is described by a string where 'T' is Trinity's location, 'A' is an agent location, 'B' is a booth location and '.' is an empty place. The following string exemplifies one possible situation:
.B.A...B..T..A
The distance between two locations is simply the number of positions between the respective characters in the string. For instance, the distance from the blue agent (A) to the red booth (B) is 2, while Trinity (T) is at a distance of 3 from the green booth (B).
Trinity needs to reach any booth before an agent catches her, so she needs to find a booth that is closer to her than any agent. Can you help her?
Write a function find_exit(s) that receives a string s containing the alley map as before described, always containing exactly one occurrence of T (Trinity) and zero or more occurrences of the characters 'B' (booths), 'A' (agents) and '.' (empty spaces). The function should do the following:
Exception with the description "404 booth not found!".Exception with the description "No possible escape...".The following limits are guaranteed in all the test cases that will be given to your program:
2 ≤ |map| ≤ 100 |
Length of map string |
| Example Function Calls | Example Output |
maps = [".B.A...B..T..A",
".B.B.T.B.B.",
"..A..T..A..",
"AB..T...B...AAAAA"]
for s in maps:
try:
distance = find_exit(s)
except Exception as error:
print("Caught exception:", error)
else:
print(distance)
|
3 2 Caught exception: 404 booth not found! Caught exception: No possible escape... |
Explanation of example function calls:
".B.A...B..T..A"), the answer is 3 (Trinity has a booth at distance 3 that she can reach before any agent)
".B.B.T.B.B."), the answer is 2 (there are no agents and the closest booths are at distance 2)
"..A..T..A.."), an Exception "404 booth not found!" is generated (no booths on the map)
"AB..T...B...AAAAA"), an Exception "No possible escape..." is generated (no booth closer to Trinity than to any agent)