substitution

Wyrmskull lordkrandel at gmail.com
Tue Jan 19 19:42:02 CET 2010



Peter Otten wrote:

def replace_many(s, pairs):
    if len(pairs):
        a, b = pairs[0]
        rest = pairs[1:]
        return b.join(replace_many(t, rest) for t in s.split(a))
    else:
        return s

-------------

Proves wrong, this way x -> y -> z. 
You call replace_many again on the central part of the split
Specifics indicate that x -> y in the end.
Your flowing pythonicity (if len(x):) gave me lots of inspiration.
I bet this will win the prize ;)

-------------

def mySubst(reps,string):
    if not(len(reps)):
        return string
    a,b,c = string.partition(reps[0][0])
    if b:
        return mySubst(reps,a) + reps[0][1] + mySubst (reps,c)
    else:
        return mySubst(reps[1:],string)

print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ), 'foobarquxfoo')

-------
Wyrmskull <lordkrandel at gmail.com>



More information about the Python-list mailing list