title: Run length encoding in Python
body: <p>I'm trying to write a simple python algorithm to solve this problem. Can you please help me figure out why my code is not working:</p>
  
  <p><strong>Problem:</strong></p>
  
  <blockquote>
    <p>If any character is repeated more than 4 times, the entire set of
    repeated characters should be replaced with a slash '/', followed by a
    2-digit number which is the length of this run of repeated characters,
    and the character. For example, "aaaaa" would be encoded as "/05a".
    Runs of 4 or less characters should not be replaced since performing
    the encoding would not decrease the length of the string.</p>
  </blockquote>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>def runLengthEncode (plainText):
      res=''
      a=''
      for i in plainText:
          if a.count(i)&gt;0:
              a+=i
          else:
              if len(a)&gt;4:
                  res+="/" + str(len(a)) + a[0][:1]
              else:
                  res+=a
                  a=i
      return(res)
  </code></pre>