Emad Elsaid
· @emad-elsaid
·
about 3 years
Now you’ll need a sister function for decompression :D
ahmadabdelhalim · @ahmadabdelhalim · about 3 years
a function that accepts a string as an argument, and returns a new string where streaks of consecutive characters are compressed. For example “aaabbc” is compressed to “3a2bc”.
def compress_str(str)new_str = ""i = 0while i < str.lengthchar = str[i]count = 0while char == str[i]count += 1i += 1endif count > 1new_str += count.to_s + charelsenew_str += charendendnew_strend
Now you’ll need a sister function for decompression :D