Reputation: 651
I am practicing my python coding on this website. This is the problem
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.
xyz_there('abcxyz') → True
xyz_there('abc.xyz') → False
xyz_there('xyz.abc') → True
This is my code , for some unknown reason , i dont pass all the testcases. I have problems debugging it
def xyz_there(str):
counter = str.count(".xyz")
if ( counter > 0 ):
return False
else:
return True
Upvotes: 1
Views: 1206
Reputation: 142136
It appears the website doesn't allow import
, so the simplest is probably:
'xyz' in test.replace('.xyz', '')
Otherwise, you can use a regular expression with a negative look behind assertion:
import re
tests = ['abcxyz', 'abc.xyz', 'xyz.abc']
for test in tests:
res = re.search(r'(?<!\.)xyz', test)
print test, bool(res)
#abcxyz True
#abc.xyz False
#xyz.abc True
Upvotes: 4
Reputation: 353019
One way to see if there's an xyz
which isn't part of .xyz
is to count the number of xyz
s, count the number of .xyz
s, and then see if there are more of the first than the second. For example:
def xyz_there(s):
return s.count("xyz") > s.count(".xyz")
which passes all the tests on the website.
Upvotes: 3