I want to create an RLE compression and decompression method for a CS project I have to do. The problem is not so much the logic, but the know-how of C++. All I currently have learned in school is Python so I tried doing this project in Python and it was really easy...
I plan to become well read in C++ but tonight there is not enough time. I hope to get help because if I can understand the equivalent C++ code I might feel much more comfortable with the language.
Can someone help me convert the following python code into C++ equivalent code?
# compresses data using RLE compression
def compress(data):
letter_counter = 1
new_data = []
for i in range(len(data)):
try:
# if the element is the same as the next one
if data[i] == data[i + 1]:
letter_counter += 1
# if different element than previous index
else:
new_data.append(str(letter_counter) + data[i])
letter_counter = 1 # reset the letter count
except IndexError: # when the index is out of range (no more elements)
new_data.append(str(letter_counter) + data[i])
return new_data
data = ['w', 'w', 'w', 'w', 'w', 'b', 'b', 'b', 'c', 'd', 'e', 'e']
data2 = ['w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w']
return compress(data) # prints ['5w', '3b', '1c', '1d', '2e']
return compress(data2) # prints ['14w']