-1

I'm having an issue with finding the best way to restart a character counter that replaces repeating characters with variables. My code looks like so.


file = rf.read()
    ret = ''
    for k, v in groupby(s):
        x = '0'
        chunk = list(v)
        cnt = len(chunk)


      if k == x and cnt == 2:
            el = 'B'
      elif k == x and cnt == 3:
            el = 'C'
      else:
            el = ''.join(chunk)
        ret += el

So if the number is greater than 3, I wan't the counter to put C for those repeating characters then start counter over again. The numbers can get rather large so using a remainder function is out of the questions. Kinda looking for code that will just replace it with the highest number I have and restart counter like so.


Elif k == x and cnt >= 3:
        "print C for those 3 characters then start count over"

So if x is repeating 12 times the output would look like CCCC, Not sure if finding the repeating characters and dividing would be the best way to do it, because if it lands on a uneven number.

  • 1
    Just to let you know, `Elif` does not equal _`elif`_. – Christian Dean Mar 18 '17 at 21:12
  • 2
    Don't make us stare down your code in order to extract the exact specifications. Give us an unambiguous, concrete example of input and expected output and what rules/specfications led to the output. I have some idea how you might elegantly solve this problem, but won't answer until you provide crystal clear specs. – timgeb Mar 18 '17 at 21:21

1 Answers1

0

How about using string.count(x)
Refer to: Count occurrence of a character in a string

Code could be:

el = ''
x = '0'
file = rf.read()
count = file.count(x) # count x in file
if count >= 3:
    for i in range(0, count, 3): # (start: 0, end: count, step: 3)
        if count- i >= 3: # this makes sure that for example if count = 13 then 4 C's are printed only
            el = el + 'C'
print el
Community
  • 1
  • 1