encode repeated letters in a string with number












3















A string 'abc' must become 'a1b1c1'.
String 'aaabcca' - 'a3b1c2a1'



I wrote python function, but it fails to add the last letter and 'abc' is only 'a1b1'.



string = "aaabbcc"
coded = ''
if len(string) == 0:
print('')
else:
count = 1 #start with the first char, not zero!
prev = string[0]
for i in range(1,len(string)):
current = string[i]
if current == prev:
count +=1
else:
coded += prev
coded += str(count)
count = 1
prev = current
print("coded string: " + coded)
print(coded)









share|improve this question


















  • 1





    hint: itertoools.groupby

    – Chris_Rands
    Nov 20 '18 at 20:44











  • Counter?

    – Jaba
    Nov 20 '18 at 20:44











  • input letters may be unsorted, right? wwaaadvc

    – RomanPerekhrest
    Nov 20 '18 at 20:45













  • @RomanPerekhrest, yes

    – ERJAN
    Nov 20 '18 at 20:46
















3















A string 'abc' must become 'a1b1c1'.
String 'aaabcca' - 'a3b1c2a1'



I wrote python function, but it fails to add the last letter and 'abc' is only 'a1b1'.



string = "aaabbcc"
coded = ''
if len(string) == 0:
print('')
else:
count = 1 #start with the first char, not zero!
prev = string[0]
for i in range(1,len(string)):
current = string[i]
if current == prev:
count +=1
else:
coded += prev
coded += str(count)
count = 1
prev = current
print("coded string: " + coded)
print(coded)









share|improve this question


















  • 1





    hint: itertoools.groupby

    – Chris_Rands
    Nov 20 '18 at 20:44











  • Counter?

    – Jaba
    Nov 20 '18 at 20:44











  • input letters may be unsorted, right? wwaaadvc

    – RomanPerekhrest
    Nov 20 '18 at 20:45













  • @RomanPerekhrest, yes

    – ERJAN
    Nov 20 '18 at 20:46














3












3








3








A string 'abc' must become 'a1b1c1'.
String 'aaabcca' - 'a3b1c2a1'



I wrote python function, but it fails to add the last letter and 'abc' is only 'a1b1'.



string = "aaabbcc"
coded = ''
if len(string) == 0:
print('')
else:
count = 1 #start with the first char, not zero!
prev = string[0]
for i in range(1,len(string)):
current = string[i]
if current == prev:
count +=1
else:
coded += prev
coded += str(count)
count = 1
prev = current
print("coded string: " + coded)
print(coded)









share|improve this question














A string 'abc' must become 'a1b1c1'.
String 'aaabcca' - 'a3b1c2a1'



I wrote python function, but it fails to add the last letter and 'abc' is only 'a1b1'.



string = "aaabbcc"
coded = ''
if len(string) == 0:
print('')
else:
count = 1 #start with the first char, not zero!
prev = string[0]
for i in range(1,len(string)):
current = string[i]
if current == prev:
count +=1
else:
coded += prev
coded += str(count)
count = 1
prev = current
print("coded string: " + coded)
print(coded)






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 20:42









ERJANERJAN

9,290123468




9,290123468








  • 1





    hint: itertoools.groupby

    – Chris_Rands
    Nov 20 '18 at 20:44











  • Counter?

    – Jaba
    Nov 20 '18 at 20:44











  • input letters may be unsorted, right? wwaaadvc

    – RomanPerekhrest
    Nov 20 '18 at 20:45













  • @RomanPerekhrest, yes

    – ERJAN
    Nov 20 '18 at 20:46














  • 1





    hint: itertoools.groupby

    – Chris_Rands
    Nov 20 '18 at 20:44











  • Counter?

    – Jaba
    Nov 20 '18 at 20:44











  • input letters may be unsorted, right? wwaaadvc

    – RomanPerekhrest
    Nov 20 '18 at 20:45













  • @RomanPerekhrest, yes

    – ERJAN
    Nov 20 '18 at 20:46








1




1





hint: itertoools.groupby

– Chris_Rands
Nov 20 '18 at 20:44





hint: itertoools.groupby

– Chris_Rands
Nov 20 '18 at 20:44













Counter?

– Jaba
Nov 20 '18 at 20:44





Counter?

– Jaba
Nov 20 '18 at 20:44













input letters may be unsorted, right? wwaaadvc

– RomanPerekhrest
Nov 20 '18 at 20:45







input letters may be unsorted, right? wwaaadvc

– RomanPerekhrest
Nov 20 '18 at 20:45















@RomanPerekhrest, yes

– ERJAN
Nov 20 '18 at 20:46





@RomanPerekhrest, yes

– ERJAN
Nov 20 '18 at 20:46












4 Answers
4






active

oldest

votes


















1














You forget to explicitly add the very last iteration.



string = "aaabb"
coded = ''
if len(string) == 0:
print('')
else:
count = 1 #start with the first char, not zero!
prev = string[0]
for i in range(1,len(string)):
current = string[i]
if current == prev:
count +=1
else:
coded += prev
coded += str(count)
count = 1
prev = current
coded += prev # these two
coded += str(count) # lines

print(coded)


I would prefer a less complicated loop, though:



string = "aaabbcc"
coded = ''
while string:
i = 0
while i < len(string) and string[0] == string[i]:
i += 1
coded += string[0]+str(i)
string = string[i:]

print(coded)





share|improve this answer


























  • how can you write "while len(string)" ?

    – ERJAN
    Nov 20 '18 at 20:57






  • 1





    @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

    – usr2564301
    Nov 20 '18 at 20:58











  • python does not allow this? as condition? len(string) = True? can it be a condtion?

    – ERJAN
    Nov 20 '18 at 21:00






  • 1





    never imagined such a while cond...

    – ERJAN
    Nov 20 '18 at 21:00






  • 1





    while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

    – usr2564301
    Nov 20 '18 at 21:01



















3














Use itertools.groupby.



>>> from itertools import groupby
>>> s = 'aaabcca'
>>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
'a3b1c2a1'


Details on what groupby produces:



>>> groups = groupby(s)
>>> [(char, list(group)) for char, group in groups]
[('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]





share|improve this answer































    3














    Some regex magic:



    import re

    s = 'aaawbbbccddddd'
    counts = re.sub(r'(.)1*', lambda m: m.group(1) + str(len(m.group())), s)
    print(counts)


    The output:



    a3w1b3c2d5




    Details:



    regex pattern:





    • (.) - capturing a character .(any char) into the 1st captured group


    • 1* - matches zero or more consecutive 1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)


    replacement:





    • m.group(1) - contains the 1st matched group value


    • str(len(m.group())) - get length of the entire character sequence matched






    share|improve this answer





















    • 1





      "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

      – usr2564301
      Nov 20 '18 at 21:03











    • @usr2564301, no problem )

      – RomanPerekhrest
      Nov 20 '18 at 21:05











    • @RomanPerekhrest, can u elaborate on this expression in sub()?

      – ERJAN
      Nov 20 '18 at 21:17











    • @ERJAN, yes, see my update

      – RomanPerekhrest
      Nov 20 '18 at 21:30



















    1














    If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code



    string = "aaabbcc"
    coded = ''

    if len(string) == 0:
    print('')

    else:
    count = 0
    prev = string[0]
    for i in range(1,len(string)):
    current = string[i]
    count +=1

    if current != prev:
    coded += prev
    coded += str(count)
    count = 0

    prev = current

    coded += current
    coded += str(count+1)

    print(coded) # -> a3b2c2





    share|improve this answer


























    • Fixed, thanks for letting me know.

      – Filip Młynarski
      Nov 20 '18 at 21:09











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401193%2fencode-repeated-letters-in-a-string-with-number%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You forget to explicitly add the very last iteration.



    string = "aaabb"
    coded = ''
    if len(string) == 0:
    print('')
    else:
    count = 1 #start with the first char, not zero!
    prev = string[0]
    for i in range(1,len(string)):
    current = string[i]
    if current == prev:
    count +=1
    else:
    coded += prev
    coded += str(count)
    count = 1
    prev = current
    coded += prev # these two
    coded += str(count) # lines

    print(coded)


    I would prefer a less complicated loop, though:



    string = "aaabbcc"
    coded = ''
    while string:
    i = 0
    while i < len(string) and string[0] == string[i]:
    i += 1
    coded += string[0]+str(i)
    string = string[i:]

    print(coded)





    share|improve this answer


























    • how can you write "while len(string)" ?

      – ERJAN
      Nov 20 '18 at 20:57






    • 1





      @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

      – usr2564301
      Nov 20 '18 at 20:58











    • python does not allow this? as condition? len(string) = True? can it be a condtion?

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      never imagined such a while cond...

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

      – usr2564301
      Nov 20 '18 at 21:01
















    1














    You forget to explicitly add the very last iteration.



    string = "aaabb"
    coded = ''
    if len(string) == 0:
    print('')
    else:
    count = 1 #start with the first char, not zero!
    prev = string[0]
    for i in range(1,len(string)):
    current = string[i]
    if current == prev:
    count +=1
    else:
    coded += prev
    coded += str(count)
    count = 1
    prev = current
    coded += prev # these two
    coded += str(count) # lines

    print(coded)


    I would prefer a less complicated loop, though:



    string = "aaabbcc"
    coded = ''
    while string:
    i = 0
    while i < len(string) and string[0] == string[i]:
    i += 1
    coded += string[0]+str(i)
    string = string[i:]

    print(coded)





    share|improve this answer


























    • how can you write "while len(string)" ?

      – ERJAN
      Nov 20 '18 at 20:57






    • 1





      @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

      – usr2564301
      Nov 20 '18 at 20:58











    • python does not allow this? as condition? len(string) = True? can it be a condtion?

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      never imagined such a while cond...

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

      – usr2564301
      Nov 20 '18 at 21:01














    1












    1








    1







    You forget to explicitly add the very last iteration.



    string = "aaabb"
    coded = ''
    if len(string) == 0:
    print('')
    else:
    count = 1 #start with the first char, not zero!
    prev = string[0]
    for i in range(1,len(string)):
    current = string[i]
    if current == prev:
    count +=1
    else:
    coded += prev
    coded += str(count)
    count = 1
    prev = current
    coded += prev # these two
    coded += str(count) # lines

    print(coded)


    I would prefer a less complicated loop, though:



    string = "aaabbcc"
    coded = ''
    while string:
    i = 0
    while i < len(string) and string[0] == string[i]:
    i += 1
    coded += string[0]+str(i)
    string = string[i:]

    print(coded)





    share|improve this answer















    You forget to explicitly add the very last iteration.



    string = "aaabb"
    coded = ''
    if len(string) == 0:
    print('')
    else:
    count = 1 #start with the first char, not zero!
    prev = string[0]
    for i in range(1,len(string)):
    current = string[i]
    if current == prev:
    count +=1
    else:
    coded += prev
    coded += str(count)
    count = 1
    prev = current
    coded += prev # these two
    coded += str(count) # lines

    print(coded)


    I would prefer a less complicated loop, though:



    string = "aaabbcc"
    coded = ''
    while string:
    i = 0
    while i < len(string) and string[0] == string[i]:
    i += 1
    coded += string[0]+str(i)
    string = string[i:]

    print(coded)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 21:01

























    answered Nov 20 '18 at 20:52









    usr2564301usr2564301

    17.7k73370




    17.7k73370













    • how can you write "while len(string)" ?

      – ERJAN
      Nov 20 '18 at 20:57






    • 1





      @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

      – usr2564301
      Nov 20 '18 at 20:58











    • python does not allow this? as condition? len(string) = True? can it be a condtion?

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      never imagined such a while cond...

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

      – usr2564301
      Nov 20 '18 at 21:01



















    • how can you write "while len(string)" ?

      – ERJAN
      Nov 20 '18 at 20:57






    • 1





      @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

      – usr2564301
      Nov 20 '18 at 20:58











    • python does not allow this? as condition? len(string) = True? can it be a condtion?

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      never imagined such a while cond...

      – ERJAN
      Nov 20 '18 at 21:00






    • 1





      while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

      – usr2564301
      Nov 20 '18 at 21:01

















    how can you write "while len(string)" ?

    – ERJAN
    Nov 20 '18 at 20:57





    how can you write "while len(string)" ?

    – ERJAN
    Nov 20 '18 at 20:57




    1




    1





    @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

    – usr2564301
    Nov 20 '18 at 20:58





    @ERJAN: uhm. Using my keyboard? Do you see something wrong with it? I did test my code.

    – usr2564301
    Nov 20 '18 at 20:58













    python does not allow this? as condition? len(string) = True? can it be a condtion?

    – ERJAN
    Nov 20 '18 at 21:00





    python does not allow this? as condition? len(string) = True? can it be a condtion?

    – ERJAN
    Nov 20 '18 at 21:00




    1




    1





    never imagined such a while cond...

    – ERJAN
    Nov 20 '18 at 21:00





    never imagined such a while cond...

    – ERJAN
    Nov 20 '18 at 21:00




    1




    1





    while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

    – usr2564301
    Nov 20 '18 at 21:01





    while "tests the expression and, if it is true, executes the first suite". len(string) is a number ... ah – you don't need len(string) 😊 That's my C shining through I'm afraid.

    – usr2564301
    Nov 20 '18 at 21:01













    3














    Use itertools.groupby.



    >>> from itertools import groupby
    >>> s = 'aaabcca'
    >>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
    'a3b1c2a1'


    Details on what groupby produces:



    >>> groups = groupby(s)
    >>> [(char, list(group)) for char, group in groups]
    [('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]





    share|improve this answer




























      3














      Use itertools.groupby.



      >>> from itertools import groupby
      >>> s = 'aaabcca'
      >>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
      'a3b1c2a1'


      Details on what groupby produces:



      >>> groups = groupby(s)
      >>> [(char, list(group)) for char, group in groups]
      [('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]





      share|improve this answer


























        3












        3








        3







        Use itertools.groupby.



        >>> from itertools import groupby
        >>> s = 'aaabcca'
        >>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
        'a3b1c2a1'


        Details on what groupby produces:



        >>> groups = groupby(s)
        >>> [(char, list(group)) for char, group in groups]
        [('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]





        share|improve this answer













        Use itertools.groupby.



        >>> from itertools import groupby
        >>> s = 'aaabcca'
        >>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
        'a3b1c2a1'


        Details on what groupby produces:



        >>> groups = groupby(s)
        >>> [(char, list(group)) for char, group in groups]
        [('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 20:46









        timgebtimgeb

        50.7k116393




        50.7k116393























            3














            Some regex magic:



            import re

            s = 'aaawbbbccddddd'
            counts = re.sub(r'(.)1*', lambda m: m.group(1) + str(len(m.group())), s)
            print(counts)


            The output:



            a3w1b3c2d5




            Details:



            regex pattern:





            • (.) - capturing a character .(any char) into the 1st captured group


            • 1* - matches zero or more consecutive 1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)


            replacement:





            • m.group(1) - contains the 1st matched group value


            • str(len(m.group())) - get length of the entire character sequence matched






            share|improve this answer





















            • 1





              "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

              – usr2564301
              Nov 20 '18 at 21:03











            • @usr2564301, no problem )

              – RomanPerekhrest
              Nov 20 '18 at 21:05











            • @RomanPerekhrest, can u elaborate on this expression in sub()?

              – ERJAN
              Nov 20 '18 at 21:17











            • @ERJAN, yes, see my update

              – RomanPerekhrest
              Nov 20 '18 at 21:30
















            3














            Some regex magic:



            import re

            s = 'aaawbbbccddddd'
            counts = re.sub(r'(.)1*', lambda m: m.group(1) + str(len(m.group())), s)
            print(counts)


            The output:



            a3w1b3c2d5




            Details:



            regex pattern:





            • (.) - capturing a character .(any char) into the 1st captured group


            • 1* - matches zero or more consecutive 1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)


            replacement:





            • m.group(1) - contains the 1st matched group value


            • str(len(m.group())) - get length of the entire character sequence matched






            share|improve this answer





















            • 1





              "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

              – usr2564301
              Nov 20 '18 at 21:03











            • @usr2564301, no problem )

              – RomanPerekhrest
              Nov 20 '18 at 21:05











            • @RomanPerekhrest, can u elaborate on this expression in sub()?

              – ERJAN
              Nov 20 '18 at 21:17











            • @ERJAN, yes, see my update

              – RomanPerekhrest
              Nov 20 '18 at 21:30














            3












            3








            3







            Some regex magic:



            import re

            s = 'aaawbbbccddddd'
            counts = re.sub(r'(.)1*', lambda m: m.group(1) + str(len(m.group())), s)
            print(counts)


            The output:



            a3w1b3c2d5




            Details:



            regex pattern:





            • (.) - capturing a character .(any char) into the 1st captured group


            • 1* - matches zero or more consecutive 1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)


            replacement:





            • m.group(1) - contains the 1st matched group value


            • str(len(m.group())) - get length of the entire character sequence matched






            share|improve this answer















            Some regex magic:



            import re

            s = 'aaawbbbccddddd'
            counts = re.sub(r'(.)1*', lambda m: m.group(1) + str(len(m.group())), s)
            print(counts)


            The output:



            a3w1b3c2d5




            Details:



            regex pattern:





            • (.) - capturing a character .(any char) into the 1st captured group


            • 1* - matches zero or more consecutive 1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)


            replacement:





            • m.group(1) - contains the 1st matched group value


            • str(len(m.group())) - get length of the entire character sequence matched







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 8:40

























            answered Nov 20 '18 at 21:01









            RomanPerekhrestRomanPerekhrest

            55.6k32253




            55.6k32253








            • 1





              "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

              – usr2564301
              Nov 20 '18 at 21:03











            • @usr2564301, no problem )

              – RomanPerekhrest
              Nov 20 '18 at 21:05











            • @RomanPerekhrest, can u elaborate on this expression in sub()?

              – ERJAN
              Nov 20 '18 at 21:17











            • @ERJAN, yes, see my update

              – RomanPerekhrest
              Nov 20 '18 at 21:30














            • 1





              "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

              – usr2564301
              Nov 20 '18 at 21:03











            • @usr2564301, no problem )

              – RomanPerekhrest
              Nov 20 '18 at 21:05











            • @RomanPerekhrest, can u elaborate on this expression in sub()?

              – ERJAN
              Nov 20 '18 at 21:17











            • @ERJAN, yes, see my update

              – RomanPerekhrest
              Nov 20 '18 at 21:30








            1




            1





            "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

            – usr2564301
            Nov 20 '18 at 21:03





            "Daily vote limit reached" – damn. I'll be back in a day. Wicked.

            – usr2564301
            Nov 20 '18 at 21:03













            @usr2564301, no problem )

            – RomanPerekhrest
            Nov 20 '18 at 21:05





            @usr2564301, no problem )

            – RomanPerekhrest
            Nov 20 '18 at 21:05













            @RomanPerekhrest, can u elaborate on this expression in sub()?

            – ERJAN
            Nov 20 '18 at 21:17





            @RomanPerekhrest, can u elaborate on this expression in sub()?

            – ERJAN
            Nov 20 '18 at 21:17













            @ERJAN, yes, see my update

            – RomanPerekhrest
            Nov 20 '18 at 21:30





            @ERJAN, yes, see my update

            – RomanPerekhrest
            Nov 20 '18 at 21:30











            1














            If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code



            string = "aaabbcc"
            coded = ''

            if len(string) == 0:
            print('')

            else:
            count = 0
            prev = string[0]
            for i in range(1,len(string)):
            current = string[i]
            count +=1

            if current != prev:
            coded += prev
            coded += str(count)
            count = 0

            prev = current

            coded += current
            coded += str(count+1)

            print(coded) # -> a3b2c2





            share|improve this answer


























            • Fixed, thanks for letting me know.

              – Filip Młynarski
              Nov 20 '18 at 21:09
















            1














            If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code



            string = "aaabbcc"
            coded = ''

            if len(string) == 0:
            print('')

            else:
            count = 0
            prev = string[0]
            for i in range(1,len(string)):
            current = string[i]
            count +=1

            if current != prev:
            coded += prev
            coded += str(count)
            count = 0

            prev = current

            coded += current
            coded += str(count+1)

            print(coded) # -> a3b2c2





            share|improve this answer


























            • Fixed, thanks for letting me know.

              – Filip Młynarski
              Nov 20 '18 at 21:09














            1












            1








            1







            If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code



            string = "aaabbcc"
            coded = ''

            if len(string) == 0:
            print('')

            else:
            count = 0
            prev = string[0]
            for i in range(1,len(string)):
            current = string[i]
            count +=1

            if current != prev:
            coded += prev
            coded += str(count)
            count = 0

            prev = current

            coded += current
            coded += str(count+1)

            print(coded) # -> a3b2c2





            share|improve this answer















            If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code



            string = "aaabbcc"
            coded = ''

            if len(string) == 0:
            print('')

            else:
            count = 0
            prev = string[0]
            for i in range(1,len(string)):
            current = string[i]
            count +=1

            if current != prev:
            coded += prev
            coded += str(count)
            count = 0

            prev = current

            coded += current
            coded += str(count+1)

            print(coded) # -> a3b2c2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 21:08

























            answered Nov 20 '18 at 20:53









            Filip MłynarskiFilip Młynarski

            1,7591413




            1,7591413













            • Fixed, thanks for letting me know.

              – Filip Młynarski
              Nov 20 '18 at 21:09



















            • Fixed, thanks for letting me know.

              – Filip Młynarski
              Nov 20 '18 at 21:09

















            Fixed, thanks for letting me know.

            – Filip Młynarski
            Nov 20 '18 at 21:09





            Fixed, thanks for letting me know.

            – Filip Młynarski
            Nov 20 '18 at 21:09


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401193%2fencode-repeated-letters-in-a-string-with-number%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith