Advertisement
davide1409

RLE

Dec 8th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. def RLE(s):
  2. #@param s: str
  3. #@return str
  4.   new_str = ''
  5.   k = 1
  6.   jump = 1
  7.   lung = len(s)
  8.  
  9.   for i in range(lung):
  10.     if i+1<lung:
  11.       if s[i+1] == ' ':
  12.         jump+=1
  13.  
  14.       else:
  15.         if s[i+1] == s[i+1-jump]:
  16.           k+=1
  17.    
  18.         else:
  19.           if k>2:
  20.             new_str+=str(k)+s[i+1-jump]
  21.             k = 1
  22.           else:
  23.             new_str+=k*s[i+1-jump]
  24.             k = 1
  25.    
  26.         jump = 1  
  27.    
  28.     else:
  29.           if k>2:
  30.             new_str+=str(k)+s[i+1-jump]
  31.             k = 1
  32.           else:
  33.             new_str+=k*s[i+1-jump]
  34.             k = 1
  35.                      
  36.                      
  37.   return new_str
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement