Stedents Question

Geoff Gerrietts ggerrietts at yahoo.com
Mon Mar 4 17:11:57 EST 2002


Quoting Michael Janssen (Janssen at rz.uni-frankfurt.de):
> Tony K wrote:
> > 
> > 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.

I love these little challenges :)

This one's not much shorter than Michael's but I think it'll run
faster.

import string
def abbc(instr):
    subs = []
    str = instr
    while str:
        start = string.find(str,"ab")
        if start == -1: break
        for i in range(start+2,len(str)):
            if str[i] == "b": continue
            if str[i] == "c":
                end = i+1
                break
            start = end = i
            break
        sub = str[start:end]
        if sub: subs.append(sub)
        str = str[end:]
    print subs


If your python supports string methods, cut that back to:

def abbc(instr):
    subs = []
    str = instr
    while str:
        start = str.find("ab")
        if start == -1: break
        for i in range(start+2,len(str)):
            if str[i] == "b": continue
            if str[i] == "c":
                end = i+1
                break
            start = end = i
            break
        sub = str[start:end]
        if sub: subs.append(sub)
        str = str[end:]
    print subs


And now as I prepare to send this to the list (after mistakenly
sending it to the poster I replied to last time, oops), I'm betting it
would be even quicker to use find twice:

def abbc(instr):
    subs = []
    str = instr
    while str:
        start = str.find("ab")
        finish = str.find("c",start)
        if (start == -1) or (finish == -1): break
        if (str[start:finish] == "a" + ("b"*(finish-start-1))):
            finish = finish + 1
            subs.append(str[start:finish])
        else:
            finish = start + 1
        str  = str[finish:]
    print subs

Maybe I'll try testing this hypothesis later....

--G.

-- 
Geoff Gerrietts <ggerrietts at yahoo.com>
-rw-rw-rw-:  permissions of the beast




More information about the Python-list mailing list