Is there an easy way to get the number of repeating character in a word?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I'm trying to get how many any character repeats in a word. The repetitions must be sequential.



For example, the method with input "loooooveee" should return 6 (4 times 'o', 2 times 'e').



I'm trying to implement string level functions and I can do it this way but, is there an easy way to do this? Regex, or some other sort of things?



So far I tried this:



def measure_normalized_emphasis(text):
char = text[-1]
emphasis_size = 0
for i in range(1, len(text)):
if text[-i] == char:
emphasis_size += 1
else:
char = text[i - 1]

return emphasis_size


And it returns 8 with "loooooveee".










share|improve this question



























    up vote
    6
    down vote

    favorite












    I'm trying to get how many any character repeats in a word. The repetitions must be sequential.



    For example, the method with input "loooooveee" should return 6 (4 times 'o', 2 times 'e').



    I'm trying to implement string level functions and I can do it this way but, is there an easy way to do this? Regex, or some other sort of things?



    So far I tried this:



    def measure_normalized_emphasis(text):
    char = text[-1]
    emphasis_size = 0
    for i in range(1, len(text)):
    if text[-i] == char:
    emphasis_size += 1
    else:
    char = text[i - 1]

    return emphasis_size


    And it returns 8 with "loooooveee".










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I'm trying to get how many any character repeats in a word. The repetitions must be sequential.



      For example, the method with input "loooooveee" should return 6 (4 times 'o', 2 times 'e').



      I'm trying to implement string level functions and I can do it this way but, is there an easy way to do this? Regex, or some other sort of things?



      So far I tried this:



      def measure_normalized_emphasis(text):
      char = text[-1]
      emphasis_size = 0
      for i in range(1, len(text)):
      if text[-i] == char:
      emphasis_size += 1
      else:
      char = text[i - 1]

      return emphasis_size


      And it returns 8 with "loooooveee".










      share|improve this question















      I'm trying to get how many any character repeats in a word. The repetitions must be sequential.



      For example, the method with input "loooooveee" should return 6 (4 times 'o', 2 times 'e').



      I'm trying to implement string level functions and I can do it this way but, is there an easy way to do this? Regex, or some other sort of things?



      So far I tried this:



      def measure_normalized_emphasis(text):
      char = text[-1]
      emphasis_size = 0
      for i in range(1, len(text)):
      if text[-i] == char:
      emphasis_size += 1
      else:
      char = text[i - 1]

      return emphasis_size


      And it returns 8 with "loooooveee".







      python regex string counter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Jan

      23.7k52347




      23.7k52347










      asked 2 hours ago









      emremrah

      478




      478






















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          Original question: order of repetition does not matter



          You can subtract the number of unique letters by the number of total letters. set applied to a string will return a unique collection of letters.



          x = "loooooveee"
          res = len(x) - len(set(x)) # 6


          Or you can use collections.Counter, subtract 1 from each value, then sum:



          from collections import Counter

          c = Counter("loooooveee")

          res = sum(i-1 for i in c.values()) # 6


          New question: repetitions must be sequential



          You can use itertools.groupby to group sequential identical characters:



          from itertools import groupby

          g = groupby("aooooaooaoo")

          res = sum(sum(1 for _ in j) - 1 for i, j in g) # 5





          share|improve this answer





























            up vote
            2
            down vote













            You could use a regular expression if you want:



            import re

            rx = re.compile(r'(w)1+')

            repeating = sum([m.span()[1] - m.span()[0] - 1 for m in rx.finditer("loooooveee")])
            print(repeating)


            This correctly yields 6 and makes use of the .span() function.




            The expression is

            (w)1+


            which captures a word character (one of a-zA-Z0-9_) and tries to repeat it as often as possible.

            See a demo on regex101.com for the repeating pattern.




            If you want to match any character (that is, not only word characters), change your expression to:

            (.)1+


            See another demo on regex101.com.






            share|improve this answer






















            • Very clean solution! Thank you so much.
              – emremrah
              1 hour ago










            • @emremrah: You're welcome.
              – Jan
              1 hour ago

















            up vote
            0
            down vote













            try this:



            word=input('something:')

            sum = 0

            chars=set(list(word)) #get the set of unique characters

            for item in chars: #iterate over the set and output the count for each item
            if word.count(char)>1:
            sum+=word.count(char)
            print('|'.format(item,str(word.count(char)))

            print('Total:'+str(sum))


            EDIT:



            added total count of repetitions






            share|improve this answer





























              up vote
              0
              down vote













              i'm not giving you a better solution, many have done it.
              I'll just correct the one you gave.



              def mne(text):
              char = text[0]
              emphasis_size = 0
              for i in range(1,len(text)):
              print(i, text[i], char, emphasis_size)
              if text[i] == char:
              emphasis_size += 1
              else:
              char = text[i]

              return emphasis_size


              is giving me:



              >>>1 o l 0
              2 o o 0
              3 o o 1
              4 o o 2
              5 o o 3
              6 v o 4
              7 e v 4
              8 e e 4
              9 e e 5
              6


              Which is what you wanted. no need for going backward, no need of [i-1]. just go forward and use too indices in the list (i and i-1)






              share|improve this answer



























                up vote
                0
                down vote













                Since it doesn't matter where the repetition is occurring or which characters are being repeated, you can make use of the set data structure provided in Python. It will discard the duplicate occurrences of any character or an object.



                Therefore, the solution would look something like this:



                def measure_normalized_emphasis(text):
                return len(text) - len(set(text))


                This will give you the exact result.



                Also, make sure to look out for some edge cases, which you should as it is a good practice.






                share|improve this answer








                New contributor




                Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.

















                • I think you missed the "The repetitions must be sequential" part of the question.
                  – Muhammad Ahmad
                  2 hours ago











                • @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                  – Dhruv Joshi
                  1 hour ago

















                up vote
                0
                down vote













                I think your code is comparing the wrong things



                You start by finding the last character:



                char = text[-1]


                Then you compare this to itself:



                for i in range(1, len(text)):
                if text[-i] == char: #<-- surely this is test[-1] to begin with?


                Why not just run through the characters:



                def measure_normalized_emphasis(text):
                char = text[0]
                emphasis_size = 0
                for i in range(1, len(text)):
                if text[i] == char:
                emphasis_size += 1
                else:
                char = text[i]

                return emphasis_size


                This seems to work.






                share|improve this answer




















                  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',
                  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%2f53206677%2fis-there-an-easy-way-to-get-the-number-of-repeating-character-in-a-word%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  10
                  down vote



                  accepted










                  Original question: order of repetition does not matter



                  You can subtract the number of unique letters by the number of total letters. set applied to a string will return a unique collection of letters.



                  x = "loooooveee"
                  res = len(x) - len(set(x)) # 6


                  Or you can use collections.Counter, subtract 1 from each value, then sum:



                  from collections import Counter

                  c = Counter("loooooveee")

                  res = sum(i-1 for i in c.values()) # 6


                  New question: repetitions must be sequential



                  You can use itertools.groupby to group sequential identical characters:



                  from itertools import groupby

                  g = groupby("aooooaooaoo")

                  res = sum(sum(1 for _ in j) - 1 for i, j in g) # 5





                  share|improve this answer


























                    up vote
                    10
                    down vote



                    accepted










                    Original question: order of repetition does not matter



                    You can subtract the number of unique letters by the number of total letters. set applied to a string will return a unique collection of letters.



                    x = "loooooveee"
                    res = len(x) - len(set(x)) # 6


                    Or you can use collections.Counter, subtract 1 from each value, then sum:



                    from collections import Counter

                    c = Counter("loooooveee")

                    res = sum(i-1 for i in c.values()) # 6


                    New question: repetitions must be sequential



                    You can use itertools.groupby to group sequential identical characters:



                    from itertools import groupby

                    g = groupby("aooooaooaoo")

                    res = sum(sum(1 for _ in j) - 1 for i, j in g) # 5





                    share|improve this answer
























                      up vote
                      10
                      down vote



                      accepted







                      up vote
                      10
                      down vote



                      accepted






                      Original question: order of repetition does not matter



                      You can subtract the number of unique letters by the number of total letters. set applied to a string will return a unique collection of letters.



                      x = "loooooveee"
                      res = len(x) - len(set(x)) # 6


                      Or you can use collections.Counter, subtract 1 from each value, then sum:



                      from collections import Counter

                      c = Counter("loooooveee")

                      res = sum(i-1 for i in c.values()) # 6


                      New question: repetitions must be sequential



                      You can use itertools.groupby to group sequential identical characters:



                      from itertools import groupby

                      g = groupby("aooooaooaoo")

                      res = sum(sum(1 for _ in j) - 1 for i, j in g) # 5





                      share|improve this answer














                      Original question: order of repetition does not matter



                      You can subtract the number of unique letters by the number of total letters. set applied to a string will return a unique collection of letters.



                      x = "loooooveee"
                      res = len(x) - len(set(x)) # 6


                      Or you can use collections.Counter, subtract 1 from each value, then sum:



                      from collections import Counter

                      c = Counter("loooooveee")

                      res = sum(i-1 for i in c.values()) # 6


                      New question: repetitions must be sequential



                      You can use itertools.groupby to group sequential identical characters:



                      from itertools import groupby

                      g = groupby("aooooaooaoo")

                      res = sum(sum(1 for _ in j) - 1 for i, j in g) # 5






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 1 hour ago

























                      answered 2 hours ago









                      jpp

                      79.1k184593




                      79.1k184593






















                          up vote
                          2
                          down vote













                          You could use a regular expression if you want:



                          import re

                          rx = re.compile(r'(w)1+')

                          repeating = sum([m.span()[1] - m.span()[0] - 1 for m in rx.finditer("loooooveee")])
                          print(repeating)


                          This correctly yields 6 and makes use of the .span() function.




                          The expression is

                          (w)1+


                          which captures a word character (one of a-zA-Z0-9_) and tries to repeat it as often as possible.

                          See a demo on regex101.com for the repeating pattern.




                          If you want to match any character (that is, not only word characters), change your expression to:

                          (.)1+


                          See another demo on regex101.com.






                          share|improve this answer






















                          • Very clean solution! Thank you so much.
                            – emremrah
                            1 hour ago










                          • @emremrah: You're welcome.
                            – Jan
                            1 hour ago














                          up vote
                          2
                          down vote













                          You could use a regular expression if you want:



                          import re

                          rx = re.compile(r'(w)1+')

                          repeating = sum([m.span()[1] - m.span()[0] - 1 for m in rx.finditer("loooooveee")])
                          print(repeating)


                          This correctly yields 6 and makes use of the .span() function.




                          The expression is

                          (w)1+


                          which captures a word character (one of a-zA-Z0-9_) and tries to repeat it as often as possible.

                          See a demo on regex101.com for the repeating pattern.




                          If you want to match any character (that is, not only word characters), change your expression to:

                          (.)1+


                          See another demo on regex101.com.






                          share|improve this answer






















                          • Very clean solution! Thank you so much.
                            – emremrah
                            1 hour ago










                          • @emremrah: You're welcome.
                            – Jan
                            1 hour ago












                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          You could use a regular expression if you want:



                          import re

                          rx = re.compile(r'(w)1+')

                          repeating = sum([m.span()[1] - m.span()[0] - 1 for m in rx.finditer("loooooveee")])
                          print(repeating)


                          This correctly yields 6 and makes use of the .span() function.




                          The expression is

                          (w)1+


                          which captures a word character (one of a-zA-Z0-9_) and tries to repeat it as often as possible.

                          See a demo on regex101.com for the repeating pattern.




                          If you want to match any character (that is, not only word characters), change your expression to:

                          (.)1+


                          See another demo on regex101.com.






                          share|improve this answer














                          You could use a regular expression if you want:



                          import re

                          rx = re.compile(r'(w)1+')

                          repeating = sum([m.span()[1] - m.span()[0] - 1 for m in rx.finditer("loooooveee")])
                          print(repeating)


                          This correctly yields 6 and makes use of the .span() function.




                          The expression is

                          (w)1+


                          which captures a word character (one of a-zA-Z0-9_) and tries to repeat it as often as possible.

                          See a demo on regex101.com for the repeating pattern.




                          If you want to match any character (that is, not only word characters), change your expression to:

                          (.)1+


                          See another demo on regex101.com.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 2 hours ago

























                          answered 2 hours ago









                          Jan

                          23.7k52347




                          23.7k52347











                          • Very clean solution! Thank you so much.
                            – emremrah
                            1 hour ago










                          • @emremrah: You're welcome.
                            – Jan
                            1 hour ago
















                          • Very clean solution! Thank you so much.
                            – emremrah
                            1 hour ago










                          • @emremrah: You're welcome.
                            – Jan
                            1 hour ago















                          Very clean solution! Thank you so much.
                          – emremrah
                          1 hour ago




                          Very clean solution! Thank you so much.
                          – emremrah
                          1 hour ago












                          @emremrah: You're welcome.
                          – Jan
                          1 hour ago




                          @emremrah: You're welcome.
                          – Jan
                          1 hour ago










                          up vote
                          0
                          down vote













                          try this:



                          word=input('something:')

                          sum = 0

                          chars=set(list(word)) #get the set of unique characters

                          for item in chars: #iterate over the set and output the count for each item
                          if word.count(char)>1:
                          sum+=word.count(char)
                          print('|'.format(item,str(word.count(char)))

                          print('Total:'+str(sum))


                          EDIT:



                          added total count of repetitions






                          share|improve this answer


























                            up vote
                            0
                            down vote













                            try this:



                            word=input('something:')

                            sum = 0

                            chars=set(list(word)) #get the set of unique characters

                            for item in chars: #iterate over the set and output the count for each item
                            if word.count(char)>1:
                            sum+=word.count(char)
                            print('|'.format(item,str(word.count(char)))

                            print('Total:'+str(sum))


                            EDIT:



                            added total count of repetitions






                            share|improve this answer
























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              try this:



                              word=input('something:')

                              sum = 0

                              chars=set(list(word)) #get the set of unique characters

                              for item in chars: #iterate over the set and output the count for each item
                              if word.count(char)>1:
                              sum+=word.count(char)
                              print('|'.format(item,str(word.count(char)))

                              print('Total:'+str(sum))


                              EDIT:



                              added total count of repetitions






                              share|improve this answer














                              try this:



                              word=input('something:')

                              sum = 0

                              chars=set(list(word)) #get the set of unique characters

                              for item in chars: #iterate over the set and output the count for each item
                              if word.count(char)>1:
                              sum+=word.count(char)
                              print('|'.format(item,str(word.count(char)))

                              print('Total:'+str(sum))


                              EDIT:



                              added total count of repetitions







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 2 hours ago

























                              answered 2 hours ago









                              vencaslac

                              520114




                              520114




















                                  up vote
                                  0
                                  down vote













                                  i'm not giving you a better solution, many have done it.
                                  I'll just correct the one you gave.



                                  def mne(text):
                                  char = text[0]
                                  emphasis_size = 0
                                  for i in range(1,len(text)):
                                  print(i, text[i], char, emphasis_size)
                                  if text[i] == char:
                                  emphasis_size += 1
                                  else:
                                  char = text[i]

                                  return emphasis_size


                                  is giving me:



                                  >>>1 o l 0
                                  2 o o 0
                                  3 o o 1
                                  4 o o 2
                                  5 o o 3
                                  6 v o 4
                                  7 e v 4
                                  8 e e 4
                                  9 e e 5
                                  6


                                  Which is what you wanted. no need for going backward, no need of [i-1]. just go forward and use too indices in the list (i and i-1)






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    i'm not giving you a better solution, many have done it.
                                    I'll just correct the one you gave.



                                    def mne(text):
                                    char = text[0]
                                    emphasis_size = 0
                                    for i in range(1,len(text)):
                                    print(i, text[i], char, emphasis_size)
                                    if text[i] == char:
                                    emphasis_size += 1
                                    else:
                                    char = text[i]

                                    return emphasis_size


                                    is giving me:



                                    >>>1 o l 0
                                    2 o o 0
                                    3 o o 1
                                    4 o o 2
                                    5 o o 3
                                    6 v o 4
                                    7 e v 4
                                    8 e e 4
                                    9 e e 5
                                    6


                                    Which is what you wanted. no need for going backward, no need of [i-1]. just go forward and use too indices in the list (i and i-1)






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      i'm not giving you a better solution, many have done it.
                                      I'll just correct the one you gave.



                                      def mne(text):
                                      char = text[0]
                                      emphasis_size = 0
                                      for i in range(1,len(text)):
                                      print(i, text[i], char, emphasis_size)
                                      if text[i] == char:
                                      emphasis_size += 1
                                      else:
                                      char = text[i]

                                      return emphasis_size


                                      is giving me:



                                      >>>1 o l 0
                                      2 o o 0
                                      3 o o 1
                                      4 o o 2
                                      5 o o 3
                                      6 v o 4
                                      7 e v 4
                                      8 e e 4
                                      9 e e 5
                                      6


                                      Which is what you wanted. no need for going backward, no need of [i-1]. just go forward and use too indices in the list (i and i-1)






                                      share|improve this answer












                                      i'm not giving you a better solution, many have done it.
                                      I'll just correct the one you gave.



                                      def mne(text):
                                      char = text[0]
                                      emphasis_size = 0
                                      for i in range(1,len(text)):
                                      print(i, text[i], char, emphasis_size)
                                      if text[i] == char:
                                      emphasis_size += 1
                                      else:
                                      char = text[i]

                                      return emphasis_size


                                      is giving me:



                                      >>>1 o l 0
                                      2 o o 0
                                      3 o o 1
                                      4 o o 2
                                      5 o o 3
                                      6 v o 4
                                      7 e v 4
                                      8 e e 4
                                      9 e e 5
                                      6


                                      Which is what you wanted. no need for going backward, no need of [i-1]. just go forward and use too indices in the list (i and i-1)







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 hours ago









                                      Alexis

                                      789113




                                      789113




















                                          up vote
                                          0
                                          down vote













                                          Since it doesn't matter where the repetition is occurring or which characters are being repeated, you can make use of the set data structure provided in Python. It will discard the duplicate occurrences of any character or an object.



                                          Therefore, the solution would look something like this:



                                          def measure_normalized_emphasis(text):
                                          return len(text) - len(set(text))


                                          This will give you the exact result.



                                          Also, make sure to look out for some edge cases, which you should as it is a good practice.






                                          share|improve this answer








                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.

















                                          • I think you missed the "The repetitions must be sequential" part of the question.
                                            – Muhammad Ahmad
                                            2 hours ago











                                          • @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                            – Dhruv Joshi
                                            1 hour ago














                                          up vote
                                          0
                                          down vote













                                          Since it doesn't matter where the repetition is occurring or which characters are being repeated, you can make use of the set data structure provided in Python. It will discard the duplicate occurrences of any character or an object.



                                          Therefore, the solution would look something like this:



                                          def measure_normalized_emphasis(text):
                                          return len(text) - len(set(text))


                                          This will give you the exact result.



                                          Also, make sure to look out for some edge cases, which you should as it is a good practice.






                                          share|improve this answer








                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.

















                                          • I think you missed the "The repetitions must be sequential" part of the question.
                                            – Muhammad Ahmad
                                            2 hours ago











                                          • @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                            – Dhruv Joshi
                                            1 hour ago












                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          Since it doesn't matter where the repetition is occurring or which characters are being repeated, you can make use of the set data structure provided in Python. It will discard the duplicate occurrences of any character or an object.



                                          Therefore, the solution would look something like this:



                                          def measure_normalized_emphasis(text):
                                          return len(text) - len(set(text))


                                          This will give you the exact result.



                                          Also, make sure to look out for some edge cases, which you should as it is a good practice.






                                          share|improve this answer








                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          Since it doesn't matter where the repetition is occurring or which characters are being repeated, you can make use of the set data structure provided in Python. It will discard the duplicate occurrences of any character or an object.



                                          Therefore, the solution would look something like this:



                                          def measure_normalized_emphasis(text):
                                          return len(text) - len(set(text))


                                          This will give you the exact result.



                                          Also, make sure to look out for some edge cases, which you should as it is a good practice.







                                          share|improve this answer








                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          share|improve this answer



                                          share|improve this answer






                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          answered 2 hours ago









                                          Dhruv Joshi

                                          161




                                          161




                                          New contributor




                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.





                                          New contributor





                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.






                                          Dhruv Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.











                                          • I think you missed the "The repetitions must be sequential" part of the question.
                                            – Muhammad Ahmad
                                            2 hours ago











                                          • @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                            – Dhruv Joshi
                                            1 hour ago
















                                          • I think you missed the "The repetitions must be sequential" part of the question.
                                            – Muhammad Ahmad
                                            2 hours ago











                                          • @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                            – Dhruv Joshi
                                            1 hour ago















                                          I think you missed the "The repetitions must be sequential" part of the question.
                                          – Muhammad Ahmad
                                          2 hours ago





                                          I think you missed the "The repetitions must be sequential" part of the question.
                                          – Muhammad Ahmad
                                          2 hours ago













                                          @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                          – Dhruv Joshi
                                          1 hour ago




                                          @MuhammadAhmad Ahh. The question got edited while I was answering. However, it seems that jpp has already given a clear explanation.
                                          – Dhruv Joshi
                                          1 hour ago










                                          up vote
                                          0
                                          down vote













                                          I think your code is comparing the wrong things



                                          You start by finding the last character:



                                          char = text[-1]


                                          Then you compare this to itself:



                                          for i in range(1, len(text)):
                                          if text[-i] == char: #<-- surely this is test[-1] to begin with?


                                          Why not just run through the characters:



                                          def measure_normalized_emphasis(text):
                                          char = text[0]
                                          emphasis_size = 0
                                          for i in range(1, len(text)):
                                          if text[i] == char:
                                          emphasis_size += 1
                                          else:
                                          char = text[i]

                                          return emphasis_size


                                          This seems to work.






                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            I think your code is comparing the wrong things



                                            You start by finding the last character:



                                            char = text[-1]


                                            Then you compare this to itself:



                                            for i in range(1, len(text)):
                                            if text[-i] == char: #<-- surely this is test[-1] to begin with?


                                            Why not just run through the characters:



                                            def measure_normalized_emphasis(text):
                                            char = text[0]
                                            emphasis_size = 0
                                            for i in range(1, len(text)):
                                            if text[i] == char:
                                            emphasis_size += 1
                                            else:
                                            char = text[i]

                                            return emphasis_size


                                            This seems to work.






                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              I think your code is comparing the wrong things



                                              You start by finding the last character:



                                              char = text[-1]


                                              Then you compare this to itself:



                                              for i in range(1, len(text)):
                                              if text[-i] == char: #<-- surely this is test[-1] to begin with?


                                              Why not just run through the characters:



                                              def measure_normalized_emphasis(text):
                                              char = text[0]
                                              emphasis_size = 0
                                              for i in range(1, len(text)):
                                              if text[i] == char:
                                              emphasis_size += 1
                                              else:
                                              char = text[i]

                                              return emphasis_size


                                              This seems to work.






                                              share|improve this answer












                                              I think your code is comparing the wrong things



                                              You start by finding the last character:



                                              char = text[-1]


                                              Then you compare this to itself:



                                              for i in range(1, len(text)):
                                              if text[-i] == char: #<-- surely this is test[-1] to begin with?


                                              Why not just run through the characters:



                                              def measure_normalized_emphasis(text):
                                              char = text[0]
                                              emphasis_size = 0
                                              for i in range(1, len(text)):
                                              if text[i] == char:
                                              emphasis_size += 1
                                              else:
                                              char = text[i]

                                              return emphasis_size


                                              This seems to work.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 2 hours ago









                                              doctorlove

                                              14.3k22850




                                              14.3k22850



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206677%2fis-there-an-easy-way-to-get-the-number-of-repeating-character-in-a-word%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  List of Gilmore Girls characters

                                                  Confectionery