1

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
3
  • 1
    Your code returns True for the string "abc". Commented May 27, 2013 at 17:44
  • @OmriBarel I linked to the wrong page just now , please have a look at it again Commented May 27, 2013 at 17:45
  • I was only referring to your code. It returns True for "abc", I'm not sure that's what you want. Commented May 27, 2013 at 17:48

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

One way to see if there's an xyz which isn't part of .xyz is to count the number of xyzs, count the number of .xyzs, 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.