Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. how are you
  2. Hello
  3.  
  4. 1. Run-Length Encoding:
  5.  
  6. Input: string, consisting of symbols A-Z:
  7. AAAABBBCCXYZDDDDEEEFFFAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB
  8.  
  9. You should write a function RLE, which will return string:
  10. A4B3C2XYZD4E3F3A6B28
  11.  
  12. Explanation:
  13. If a symbol occurs 1 time, it remains unchanged
  14. If a symbol is repeated more than 1 time, the number of repetitions is added to it
  15. If input string is not correct: throw exception
  16.  
  17. --------------------------------
  18. def string_encoder(str):
  19. if len(str)<2:
  20. return str
  21. current char = str[0]
  22. char_arr_out = []
  23. current_count =1
  24. for char is str[1,-1]:
  25. if char < 'A' OR char > 'Z':
  26. # raise Exception()
  27. raise Exception()
  28. if char == current:
  29. count+=1
  30. else:
  31. append(char_arr_out,current)
  32. if(count > 1):
  33. append(char_arr_out,str(count))
  34. count=1
  35. current = char
  36. return char_arr_out
  37.  
  38.  
  39. string_encoder('AF')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement