+ 1
Why my 3rd test case is getting failed in security problem
floorCheck = input().lower() flag = 0 guard = 'g' n = 0 listofGuard = [] if guard in floorCheck: for chars in floorCheck: if chars == guard: i = floorCheck.index(chars) if n > i: listofGuard.append(floorCheck.index(chars, n)) n = n + 1 else: listofGuard.append(floorCheck.index(chars, i)) n = i + 1 for grd in listofGuard: if (floorCheck.index('
#x27;) < grd) and (grd < floorCheck.index('t')): flag = 0 else: flag = 1 else: flag = 1 if flag == 1: print('ALARM') else: print('quiet')7 Answers
0
A faster and shorter solution to this problem is first removing all the X's in the input string, then finding the index of $, then checking if the character immediately before and after that index (of $) is T. If so output ALARM otherwise quiet.
Solution:
floors = input().replace("x", "")
money_location = floors.index("quot;)
print("ALARM" if "T" in floors[money_location -1:money_location+1] else "quiet")
+ 5
Emeh Matthew ,
the code provided by you seems to have 2 issues:
(1) start index
if we have an input that looks (after removing the 'x') like:
'$TG',
we have an issue with the start index of the slice. '#x27; has the index of 0 in the string. if we use this start index and subtract -1, we get a slice index of -1. this is a valid index, but not for the first index, but for the last.
the resulting slice is an empty string.
(2) end index
another issue is the end index. taking the same sample as above, means that we have the index position from '#x27; in the string of 0 and add + 1 here. this would be slice index 1, but the end index itself is not included in the slice result. we have to add +2.
so both issues prevent the code from running properly for some cases.
+ 1
Save the code in playground and share link here...
+ 1
ARUN VS There may be more number of guards like $GTG . It failing in this case...
+ 1
Ok let me check
+ 1
Lothar
My solution doesn't actually fail any test case. It seems there wasn't any test case that could catch my sneaky move š. I was just tryna avoid long lines of code for a simple problem. However, what you said is true. Index errors could arise from my solution. Here's my correction:
floors = input().replace("x", "")
money_location = floors.index("quot;)
if money_location - 1 >= 0:
left_char = floors[money_location - 1]
print("ALARM" if left_char == "T" else "quiet")
elif money_location + 1 < len(floors):
right_char = floors[money_location + 1]
print("ALARM" if right_char == "T" else "quiet")