+ 2
What is broken in my "security" code?
The code is here: https://code.sololearn.com/cLhEvyg9kIY1/?ref=app It gets test 1 and 3 right, but doesn't get the same result with the rest. I suspect it reads Thur guard before it actually is, but by looking at it I can't notice why.
15 odpowiedzi
0
I found the problem. You see, even if the code had not gone through a single letter of the 'new_floor' string, it would enter the if/else statements to check if there was a alarm. A ELSE STATEMENT. That meant that even at the beginning, it would just drop at the print('quiet').
I fixed it using elif instead of else:
elif (thief == 1 and guard == 1 and money == 0) or (thief == 0 and guard == 1 and money == 1):
print('quiet')
Because of the natural iteration of 'for', it would never see money and a thief and a guard before my elif catches it.
It worked on 1 and 3 (even with the else structure) because these had "quiet" as an answer.
Anyways, Alexandr code is much more concise.
+ 1
med gazzeh your solution is great, but I like Alexandr 's better
+ 1
Oh, nice that works too :)
+ 1
I jusst find "G" betwen "quot; and "T", and it solved all test.
python:
s=input()
m=s.find("quot;)
t=s.find("T")
g=s.find("G")
if (m>g and g>t) or (m<g and g<t):
print("quiet")
else:
print("ALARM")
0
Alexandr , unfortunately I don't get it
0
Alexandr now I get it!! However, could you provide me with problem in my code?
0
Alexandr shouldn't the native 'for' iteration check it (because it is always from left to right)?
0
Alexandr Yes, because when it removes a 'G', the next one becomes the 0, so by reading always from 1 it should jump a single 'G', if there is one.
0
Alexandr , I am glad you provided me a better way to program it, put I want to know where is the error so that I can learn from it.
0
On the second line whats the use of that empty string
0
CHIDERA AHAMEFULE the replace method requires to change something to another. So I replaced every 'x' with a empty space, therefore leaving only G, T or $
0
I think it would be better if you simply get rid of 'x' chars ( as you already did) and then just check the difference between the indexes of '#x27; and 'T'.
For exemple : 'xxGxxGxxTxxx$xxG' gives 'GGT$G' thus | index('T') - index('#x27;) | = 1 thus the alarm should be raised, otherwise not.
0
I can't find alexandr solution in comments can you plz post it below so I can check it?
0
med gazzeh ugh, looks like he removed it, sorry. After you got rid of 'x's, Alexandr suggested checking if there was a "$T" or "Tquot;, because these would be the only "ALARM" possibilities. If there wasn't, then the code would print 'quiet'.
0
Hello