# exercise 4 of the 27-1-2021 exam

A
Alessandro Vecchi (1870 points)
1 10 18
Good afternoon all,

I have a question about the exercise 4 of the exam. I spent the last 2 hours (literally, since I strangely managed to do the first three ones in the first half) of the exam doing it and I still don't understand some results.

I will bring as an example the test_ex4_2 since I spent so muuuuch time with it, but with the 4 and 5 the reasoning is the same.

The dictionary is:

grammar = {
"\$B": ['\$Y', '€y', ' ', '\$I', '€n', ' ', '\$b'],
"\$b": ['\$Y', '"Stop!" But ', '\$I', '\$G'],
"\$Y": ['You ', '@s'],
"\$I": ['I ', '@s'],
"@s": ['say: '],
"€y": ['€Q', 'Yes.', '€Q'],
"€n": ['€Q', 'No.', '€Q'],
"!Y": ["\$Y", "€y"],
"\$G": ['€Q', '\$g', ' ', '\$g', ' ', '\$g', '€Q'],
"\$g": ['Go!'],
"€Q": ['"'], }

and the

key = "\$B"

I'm going to focus only on the number of concatenation, since they were my only problem since the beginning.

I'm counting by hand, since I don't get the exact result even by counting by hand.

First step is to count concatenations on "\$Y" which are:

1.'You'

2.'@s', hence 'say: '

Therefore the count on "\$Y" is 2.

With the same reasoning I get:

- '€y' is 3

-  ' ' is 1

- '\$I' is 2

-  '€n' is 3

- ' ' is 1

- '\$b' is '€y'+stop+'\$I' +'\$G'=2+1+2+7=12

Finally the result is 2+3+1+2+3+1+12 which is 24.

But the expected result should be 22. How? Why?

Sorry to bother but I need to understand where am I wrong.

I have been thinking about it since Wednesday and I can't stop doing it :-) .
140 views
closed

by (2.8k points)
selected

Dear Mr Vecchi,

I am glad to read about how fast you were at solving most of the exercises. Let me try with a step-by-step “debugging” for the remaining one. I know it is quite a brute-force approach but I hope it helps us clarify the issue a bit.

Replacing \$Y with ['You ', '@s']. Count update: 1 → 2
Replacing @s with ['say: ']. Count update: 2 → 3
Replacing €y with ['€Q', 'Yes.', '€Q']. Count update: 3 → 4
Replacing €Q with ['"']. Count update: 4 → 5
Replacing €Q with ['"']. Count update: 5 → 6
Replacing \$I with ['I ', '@s']. Count update: 6 → 7
Replacing @s with ['say: ']. Count update: 7 → 8
Replacing €n with ['€Q', 'No.', '€Q']. Count update: 8 → 9
Replacing €Q with ['"']. Count update: 9 → 10
Replacing €Q with ['"']. Count update: 10 → 11
Replacing \$b with ['\$Y', '"Stop!" But ', '\$I', '\$G']. Count update: 11 → 12
Replacing \$Y with ['You ', '@s']. Count update: 12 → 13
Replacing @s with ['say: ']. Count update: 13 → 14
Replacing \$I with ['I ', '@s']. Count update: 14 → 15
Replacing @s with ['say: ']. Count update: 15 → 16
Replacing \$G with ['€Q', '\$g', ' ', '\$g', ' ', '\$g', '€Q']. Count update: 16 → 17
Replacing €Q with ['"']. Count update: 17 → 18
Replacing \$g with ['Go!']. Count update: 18 → 19
Replacing \$g with ['Go!']. Count update: 19 → 20
Replacing \$g with ['Go!']. Count update: 20 → 21
Replacing €Q with ['"']. Count update: 21 → 22

Best,
CDC

A
Alessandro Vecchi (1870 points)
1 10 18
by (1.9k points)
edited
Oh thanks. It seems that I didn't understand the text. I thought that the count was the number of concatenations. In fact the issue derives from this. '\$G' and '\$B' have two characters each that are not present as keys in the dictionary. My program counts how many concatenations I do, not how many replacements. Here there are the two elements that should not be present.

I wasn't able to modify the first solution that I came out with and I was wondering why indeed.

Here it is my solution (very similar to the professor Sterbini's one):

#encrypted code

As I said I didn't understand the text well, so it's my fault.

In my defense, I can only say that it's not very clear how should we manage the counter, even after understanding the problem I find the text misunderstandable. (At least I'm not as dumb as I thought)

However, thank you so much for making me understand the mistake.
andrea.sterbini (173640 points)
516 941 1795
by (174k points)
Please do not paste code in Q2A
A
Alessandro Vecchi (1870 points)
1 10 18
by (1.9k points)
Oh sorry I thought it was allowed since the exam was done.