+ 10

Help in XYZ problem in python?

Edit: Without regex Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not. E.g: xyz_there('abcxyz') ā†’ True xyz_there('abc.xyz') ā†’ False xyz_there('xyz.abc') ā†’ True My mindless attempt: It does't pass all conditions https://code.sololearn.com/cu05NPBlYUql/?ref=app

30th May 2020, 9:44 AM
Tricker
47 Respostas
30th May 2020, 12:32 PM
Valmob101
Valmob101 - avatar
30th May 2020, 10:53 AM
Russ
Russ - avatar
+ 7
Russ, thanks a lot for your hint!!! Now it works.
30th May 2020, 6:44 PM
Lothar
Lothar - avatar
+ 6
Russ ohh i never thought of making it shorter. Nice!!
30th May 2020, 1:01 PM
Valmob101
Valmob101 - avatar
+ 6
[Edited / corrected] - Here is my version: test = ['.xyz.', 'abcxyz', 'abc.xyz', 'xyz.abc','.xyzabc', 'abc.xyzxyz', '.xyz.xyz', 'abc.xyzxyz.xyz'] for i in test: print(f'{i} -> OK') if (i.count('xyz') - i.count('.xyz') > 0) else print(f'{i} -> NOT OK') print() https://code.sololearn.com/ca8NngqIpDeg/?ref=app
30th May 2020, 1:40 PM
Lothar
Lothar - avatar
+ 6
Bikash Rouniyar, You posted a question here in this thread. The better way would be if you post it as a separate thread. Thanks!
30th May 2020, 8:25 PM
Lothar
Lothar - avatar
30th May 2020, 11:07 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
PythonPip I suspect one of the failing tests might be from values like: ".xyzxyz" "x.xyzxyz" "xyz.xyz" "x.xyz.xyz.xyzxyz" Both ".xyz" and "xyz" without a preceeding "." exists. So the solution should also return True for these instances.
30th May 2020, 12:53 PM
David Carroll
David Carroll - avatar
+ 4
How about a little state machine FSM States: -1,0,1,2 Start state 0 Accepted state 3 0->0 for each char except x and. O->1 for x 0->-1 for . 1->2 for y 1->-1for . 1->0 for others 2->-1 for . 2->3 for z 2->0 for others -1 -> -1 for . -1 ->0 for others
31st May 2020, 11:13 AM
Oma Falk
Oma Falk - avatar
+ 3
Lothar I think change "== 1" to "> 0" and your code is good šŸ‘
30th May 2020, 2:45 PM
Russ
Russ - avatar
+ 3
Cassandra If only '.x' is removed then the letters before '.x' will create problem if it's 'x'. Consider this string: "x.xyz" If ".x" would be removed then then string will read as "xyz", which will return True instead of False.
31st May 2020, 11:54 AM
Valmob101
Valmob101 - avatar
+ 2
Kuba Siekierzyński It needs to catch "xyzabc" as true šŸ˜‰
30th May 2020, 11:09 AM
Russ
Russ - avatar
+ 2
Russ Right, your way is more efficient šŸ‘šŸ»
30th May 2020, 11:37 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
šŠš¢š¢š›šØ š†š”ššš²ššš„ šŸ˜…šŸ˜… It's an online website test. That won't allow this.
30th May 2020, 12:08 PM
Tricker
+ 2
PythonPip I thought ".xyz" should return False? Code Crasher Your code produces this: djd.xyzxyz True djdj.xyzjen True - should be False jdjxyz.xyz True djdjhexyz False - should be True abcxyz False - should be True xyzabc False - should be True I think Valmob101 's solution is the cleanest. def xyz_there(string): return "xyz" in string.replace(".xyz", "")
30th May 2020, 12:48 PM
Russ
Russ - avatar
+ 2
Code Crasher the question says that, if there is xyz in string it should return True but if there is no xyz but there's .xyz then False. means it should contain xyz to return True even if there are many .xyz Did you understand or did i made you more confused?šŸ˜…šŸ˜…
30th May 2020, 2:29 PM
Tricker
+ 2
Code Crasher If there is at least 1 instance of "xyz" appearing where it is not preceded by a ".", it should return True. Else, it should return False.
30th May 2020, 2:44 PM
Russ
Russ - avatar
+ 2
On the 'Discuss' tab, you should see a green circle at the bottom right. Hit that to ask a new question.
30th May 2020, 7:41 PM
Russ
Russ - avatar
+ 2
Moye Nkem Onyeka Not here, no. What you need to do is post the challenge to your own page, then post a link to it in this thread: https://www.sololearn.com/Discuss/1270852/?ref=app.
1st Jun 2020, 12:24 PM
Russ
Russ - avatar
+ 1
PythonPip Not "cleanly" I guess. But doable. Check updated code above.
30th May 2020, 11:38 AM
Russ
Russ - avatar