0

Can you make this shorter ?(help me pls)

https://code.sololearn.com/cQSw6uf4kCrP/?ref=app

20th May 2021, 1:27 PM
TCorn
TCorn - avatar
9 Respostas
+ 5
visph, this an exercise from codecoach called "security" Lành Nguyễn , what you have to do is check for 2 general patterns: ▪︎$x..xT ▪︎Tx..x$ but there for you have to remove all "x" from the string or replace them by an empty string "" now you only have to check if these pattern occures in the input string by using the membership operator : ▪︎$T ▪︎T$ if one of these patterns is in the string -> ALARM, otherwise -> quiet
20th May 2021, 1:53 PM
Lothar
Lothar - avatar
+ 3
visph - NO, it is not regular expression, it's very basic python... these are the first 3 lines of the code: inp = input().upper() inp = inp.replace('X','') if '$T' in inp or 'T
#x27; in inp: ... (add the output here) ...
20th May 2021, 2:02 PM
Lothar
Lothar - avatar
+ 2
The original Code Coach problem has only one "
quot; in the input string. Are you challenging yourself to extend the problem with more than one "
quot;?
20th May 2021, 2:29 PM
Brian
Brian - avatar
+ 1
what's the purpose of your code? what's expected as input?
20th May 2021, 1:41 PM
visph
visph - avatar
+ 1
Would this be short enough for you? You only need to compare their relative positions in the input string. n = input() s = n.find("
quot;) t = n.find("G") k = n.find("T") while t >= 0: if (s<t<k) or (s>t>k): ans = "quiet" break t = n.find("G", t + 1) else: ans = "ALARM" print(ans);
20th May 2021, 1:48 PM
Brian
Brian - avatar
+ 1
Lành Nguyễn what Lothar suggest is to use "regular expression" pattern ;)
20th May 2021, 1:57 PM
visph
visph - avatar
+ 1
Lothar :D my bad: word 'pattern' and use of $ make me guess that by reading too fast and having in head the possibility to use regexp ^^
20th May 2021, 2:05 PM
visph
visph - avatar
+ 1
Brian . But when I run your code I write "xxGx$xGxTxx$xx" when run it print"quiet" while between T and $ has no Guard
20th May 2021, 2:24 PM
TCorn
TCorn - avatar
0
I took a note of it on the top(my English is bad ,forgive me)
20th May 2021, 1:46 PM
TCorn
TCorn - avatar