+ 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
47 Respostas
+ 10
Try this one:
https://code.sololearn.com/cAzxk02W6fUI/?ref=app
+ 7
Russ, thanks a lot for your hint!!! Now it works.
+ 6
Russ ohh i never thought of making it shorter. Nice!!
+ 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
+ 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!
+ 5
Russ Good one :) My attempt:
https://code.sololearn.com/c4iVsRm45zrl/?ref=app
+ 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.
+ 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
+ 3
Lothar I think change "== 1" to "> 0" and your code is good š
+ 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.
+ 2
Kuba SiekierzyÅski It needs to catch "xyzabc" as true š
+ 2
Russ Right, your way is more efficient šš»
+ 2
šš¢š¢ššØ šš”šš²šš„ š
š
It's an online website test. That won't allow this.
+ 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", "")
+ 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?š
š
+ 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.
+ 2
On the 'Discuss' tab, you should see a green circle at the bottom right. Hit that to ask a new question.
+ 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.
+ 1
PythonPip Not "cleanly" I guess. But doable. Check updated code above.