I cannot solve security | Sololearn: Learn to code for FREE!

+1

I cannot solve security

I failed test cases 3, 5, 6. Please help My code: https://code.sololearn.com/cfhNc3RxRLb7/?ref=app You are in charge of security at a casino, and there is a thief who is trying to steal the casino's money! Look over the security diagrams to make sure that you always have a guard between the thief and the money! There is one money location, one thief, and any number of guards on each floor of the casino. Task: Evaluate a given floor of the casino to determine if there is a guard between the money and the thief, if there is not, you will sound an alarm. Input Format: A string of characters that includes $ (money), T (thief), and G (guard), that represents the layout of the casino floor. Space on the casino floor that is not occupied by either money, the thief, or a guard is represented by the character x. Output Format: A string that says 'ALARM' if the money is in danger or 'quiet' if the money is safe. Sample Input: xxxxxGxx$xxxT Sample Output: ALARM

8/11/2021 3:48:41 AM

____

8 Answers

New Answer

+3

____ your program assumes that there always would be a guard or thief after encountering money, and gives out no output when there isn't. Example of an input where your program fails is : TG$ where according to question the output should be "quiet" but your program gives out no output. In this case, a simple fix is to print "quiet" if the control reaches the end of your program 👇 https://code.sololearn.com/cOScXTnUQ0l8/?ref=app

+2

____ Here's what I have done: #include <stdio.h> int main() { char a[40]; fgets(a, 40, stdin); int x=0, y=0; for (int i=0;a[i];i++) { if (a[i] == '$') x++; else if (a[i] == 'T') y++; else if (a[i] == 'G') x = y = 0; if (x && y) { puts("ALARM"); return 0; } } puts("quiet"); return 0; } // Here, I've used 'x' and 'y' as flags // Hope this helps

+1

Calvin Thomas Thank you for the code, but I want to know why my code failed if possible.

+1

____ I've mentioned it in the comments.

+1

Arsenic Calvin Thomas Thank you so much. It's now working.

+1

import re casino = 'T$xxxxxxxxGxx' mod = re.sub('x','',casino) check, check2, check3 = '$GT', 'TG$', 'T$G' Here's what I have done 100XP if (check not in mod or check2 not in mod) and (check in mod): print('quiet') else: print('ALARM')

0

Calvin Thomas I updated the code. Though 2 cases are passed, #3 still failed.

-2

string = input() situation = string.replace("x", "") if "T$" in situation or "$T" in situation: print("ALARM") else: print("quiet") #try this shortcut