0

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']
Ehkz
  • 23
  • 1
  • 4
  • 4
    If becoming well versed in C++ is important to you - you will make the time. SO isn't a code translation service. Why not have a stab at it yourself when you find the time, and when you have problems, ask a specific question, involving specific issues? – Jon Clements Mar 16 '13 at 04:53
  • Honest to goodness, if I had the time in the last 3 weeks of this semester, I wouldn't be here. I know this website isn't a code translation service, but I'm desperately looking for help... – Ehkz Mar 16 '13 at 04:56
  • Okay - well good luck - if it's any consolation - I've given you a better Python solution ;) – Jon Clements Mar 16 '13 at 04:57
  • What specific things do you not understand? Do you have any knowledge of C++ at all? Jon Clemments is right, but if you narrow it down the question is answerable. – ApproachingDarknessFish Mar 16 '13 at 04:58
  • Of course - simple googling will give you something like: http://rosettacode.org/wiki/Run-length_encoding – Jon Clements Mar 16 '13 at 05:05
  • I guess all I don't quite understand is how to create an array with variable data, and how to append it to a new array the same way I could do with new_data.append(str(letter_counter) + data[i]) – Ehkz Mar 16 '13 at 05:07
  • std::vector and push_back is one way - but my C++ is rusty – Jon Clements Mar 16 '13 at 05:10
  • possible duplicate of [Use Cython as Python to C Converter](http://stackoverflow.com/questions/7112812/use-cython-as-python-to-c-converter) – jxh Mar 16 '13 at 14:36
  • Possible duplicate of [Convert Python program to C/C++ code?](https://stackoverflow.com/q/4650243/608639) – jww Nov 21 '19 at 13:39

2 Answers2

1
#include<iostream>
#include<string>
#include<sstream>

using namespace std;

string compress(string data) {
    int letter_counter = 1, i=0;
    ostringstream new_data;
    for (i; i<data.length()-1; i++) {
        if (data[i] == data[i+1]) {
            letter_counter += 1;
        } else {
            new_data<<letter_counter;
            new_data<<data[i];
            letter_counter = 1;
        }
    }
    new_data<<letter_counter;
    new_data<<data[i];

    return new_data.str();
}

int main() {
    string data = string("wwwwwbbbcdee");
    string data2 = string("wwwwwwwwwwwwww");
    cout << compress(data) <<endl;
    cout << compress(data2) <<endl;
}

Here's a simple way of doing it with C++. It treats the character arrays as strings, and uses stringstream to easily insert numbers into the string.

At the end of the day though, the other posters are right. The best way to learn C++ is to try yourself and ask others when you run into problems. www.cplusplus.com is a great resource.

lexxonnet
  • 136
  • 1
  • 2
  • If only a couple hours earlier haha, thank you for doing exactly what I asked, and keeping it in a way that a python programmer of my caliber could easily understand. I didn't know about sstream which would have made a big difference on my approach to this. Also, I will heed all of your advice because I know it's right and I've been doing just that to learn what programming I have learned so far. I just didn't have a lot of time on this project and no one to ask for help. – Ehkz Mar 16 '13 at 10:10
  • You're welcome :) Pity it wasn't on time. Just wandered on to the site looking for something else, stumbled upon this question and thought I'd take a crack at it. – lexxonnet Mar 16 '13 at 10:40
0

Just on a side note - your Python could be improved massively:

from itertools import groupby
print ['{}{}'.format(len(list(g)), k) for k, g in groupby(data)]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • Thanks, haha. Sadly, I've never used itertools (mostly because I'm learning how to not use shortcuts before I learn the shortcuts). – Ehkz Mar 16 '13 at 05:00