1

How to replace the pattern in the string with

     decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

 The first pattern "(++info++)" needs to replaced with (++info a++)
 The second pattern "(++info++)" needs to replaced with (++info b++)
 The third pattern "(++info++)" needs to replaced with (++info c++)
 If there many more then it should be replaced accordingly
5
  • There are some good O(N) answers here, why accept one of the O(N^2) ones? Commented Aug 20, 2010 at 6:51
  • @gnibbler Would you mind tagging the O(N) ones? I'm not quite used to looking at real examples of that sort of thing. Does N here refer to the count of '++info++' elements? Commented Aug 20, 2010 at 7:13
  • @aaron, The O(N^2) answers are the ones that are making copies of the entire string for each (++info++). In this case it is mostly the ones using the 1 argument for replace() Commented Aug 20, 2010 at 7:28
  • I could be wrong, but I sense that these strings will be fairly short. Commented Aug 20, 2010 at 7:38
  • @gnibbler. Thanks. I see that now. I think my mistake was not knowing the string manipulation facilities as well as I should. Thanks again. Commented Aug 20, 2010 at 7:55

8 Answers 8

4

This should be simple enough:

for character in range(ord('a'), ord('z')):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str

It has the added benefit of stopping at 'z'. If you want to wrap around:

import itertools

for character in itertools.cycle(range(ord('a'), ord('z'))):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str

And just for fun, a one-liner, and O(n):

dstr = "".join(x + "(++info {0}++)".format(chr(y)) for x, y in zip(dstr.split("(++info++)"), range(ord('a'), ord('z'))))[:-len("(++info a++)")]
Sign up to request clarification or add additional context in comments.

Comments

3
import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
s = decoded_str.replace('++info++', '++info %s++')
s % tuple(i for i in string.ascii_lowercase[:s.count('%s')])

Comments

2

Here is a rather ugly yet pragmatic solution:

import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
letters = list(string.lowercase)
token = "(++info++)"
rep_token = "(++info %s++)"

i = 0
while (token in decoded_str):
    decoded_str = decoded_str.replace(token, rep_token % letters[i], 1)
    i += 1

print decoded_str

Comments

1
>>> import re
>>> rx = re.compile(r'\(\+\+info\+\+\)')
>>> s = "Name(++info++)Age(++info++)Adress of the emp(++info++)"
>>> atoz = iter("abcdefghijklmnopqrstuvwxyz")
>>> rx.sub(lambda m: '(++info ' + next(atoz) + '++)', s)
'Name(++info a++)Age(++info b++)Adress of the emp(++info c++)'

Comments

1

Here's a quick hack to do it:

string=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

def doit(s):
    import string
    allTheLetters = list(string.lowercase)
    i=0
    s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    while (s2!=s):
        s=s2
        i=i+1
        s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    return s

Note that performance is probably not very great.

Comments

1
import re, string

decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

sub_func=('(++info %s++)'%c for c in '.'+string.ascii_lowercase).send
sub_func(None)
print re.sub('\(\+\+info\+\+\)', sub_func, decoded_str)

Comments

1
from itertools import izip
import string
decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"
parts = iter(decoded_str.split("(++info++)"))
first_part = next(parts)
tags = iter(string.ascii_lowercase)
encoded_str=first_part+"".join("(++info %s++)%s"%x for x in izip(tags, parts))
print encoded_str

1 Comment

Thanks for the example. It thought me one new thing and reminded me of about five I've forgotten.
0
decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

import re
for i, f in enumerate(re.findall(r"\(\+\+info\+\+\)",decoded_str)):
    decoded_str = re.sub(r"\(\+\+info\+\+\)","(++info %s++)"%chr(97+i),decoded_str,1)
print decoded_str

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.