Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how are you
- Hello
- 1. Run-Length Encoding:
- Input: string, consisting of symbols A-Z:
- AAAABBBCCXYZDDDDEEEFFFAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB
- You should write a function RLE, which will return string:
- A4B3C2XYZD4E3F3A6B28
- Explanation:
- If a symbol occurs 1 time, it remains unchanged
- If a symbol is repeated more than 1 time, the number of repetitions is added to it
- If input string is not correct: throw exception
- --------------------------------
- def string_encoder(str):
- if len(str)<2:
- return str
- current char = str[0]
- char_arr_out = []
- current_count =1
- for char is str[1,-1]:
- if char < 'A' OR char > 'Z':
- # raise Exception()
- raise Exception()
- if char == current:
- count+=1
- else:
- append(char_arr_out,current)
- if(count > 1):
- append(char_arr_out,str(count))
- count=1
- current = char
- return char_arr_out
- string_encoder('AF')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement