Stedents Question

Michael Janssen Janssen at rz.uni-frankfurt.de
Sat Mar 2 12:47:10 EST 2002


Tony K wrote:

> The question below is a tutorial question that stumps me.  Any suggestions
> would be appreciated.
> Tony
> 
> Write a Python function findABBC which takes a single string as parameter
> and returns all substrings thereof which match the regexp pattern ab+c.
> You are NOT allowed to use the re module.

def findABBC(str):
    a=0 
    b=0
    while str:
        for n in str:
            if n=="a":
                a=1
                break
            if  a==1 and n=="b":
                b=b+1
                break
            if a==1 and n=="c":
                print "a"+"b"*b+"c"
                a, b = 0, 0
                break
            if n!=[a-c]:
                a, b = 0, 0
                break
        str=str[1:]

produces every ab+c-string in str. But there must be something easier. 
Something smaller ;-)

Michael



More information about the Python-list mailing list