+ 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.

7th Jan 2020, 12:59 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
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.
7th Jan 2020, 3:08 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
+ 1
med gazzeh your solution is great, but I like Alexandr 's better
8th Jan 2020, 11:00 PM
Gabriel Pedroza
Gabriel Pedroza - avatar
+ 1
Oh, nice that works too :)
8th Jan 2020, 11:33 PM
MedG
MedG - avatar
+ 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")
9th Jan 2020, 12:28 AM
Hoà Xuân Nông
Hoà Xuân Nông - avatar
0
Alexandr , unfortunately I don't get it
7th Jan 2020, 2:18 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
0
Alexandr now I get it!! However, could you provide me with problem in my code?
7th Jan 2020, 2:25 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
0
Alexandr shouldn't the native 'for' iteration check it (because it is always from left to right)?
7th Jan 2020, 2:33 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
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.
7th Jan 2020, 2:36 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
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.
7th Jan 2020, 2:39 AM
Gabriel Pedroza
Gabriel Pedroza - avatar
0
On the second line whats the use of that empty string
7th Jan 2020, 1:42 PM
CHIDERA AHAMEFULE
CHIDERA AHAMEFULE - avatar
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 $
7th Jan 2020, 2:46 PM
Gabriel Pedroza
Gabriel Pedroza - avatar
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.
8th Jan 2020, 10:51 PM
MedG
MedG - avatar
0
I can't find alexandr solution in comments can you plz post it below so I can check it?
8th Jan 2020, 11:04 PM
MedG
MedG - avatar
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 "T
quot;, because these would be the only "ALARM" possibilities. If there wasn't, then the code would print 'quiet'.
8th Jan 2020, 11:21 PM
Gabriel Pedroza
Gabriel Pedroza - avatar
0
Hello
8th Jan 2020, 11:48 PM
igor play MGC e SHS