Python solve problem with string operation

giacomo boffi pecore at pascolo.net
Thu Jan 16 19:17:12 EST 2014


Nac Temha <naccttemha at gmail.com> writes:

> Hi everyone,
>
> I want to do operation with chars in the given string. Actually I want to
> grouping the same chars.
>
> For example;
>
> input : "344111133311222223377"
> operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
> output: "34131237"
>
>
>
> How can I do without list, regular expression. just using string operations. 
> Using an effective methods of python for this problem.


% cat a.py
def f(s,n):
    if s[n+1] == s[n]:
        return s[:n]+s[n+1:], n
    return s, n+1

i = "344111133311222223377"
n = 0

while n+1 != len(i):
    i, n = f(i, n)

print i
% python a.py
34131237
% 

-- 
your instructor is a mean person



More information about the Python-list mailing list