⌨️
Run-Length Encoding
Week 34
You are creating some monitoring software - it can be used for your websites, IoT devices, sensors etc. Some of these devices have low memory, but their status is checked at millisecond intervals - you want to log their all statuses, but don't have enough space on them to store data for each interval separately
Instead, you remember back to your school textbooks which mentioned that run-length encoding (RLE) works well for simple, usually digitally-created images that have long consecutive patterns of identical pixel values. Classic game images with a low colour palette are great examples of such images. Likewise, you realise that your websites, sensors etc will likely have large periods of consecutive statuses - i.e. TTTTTTTTTTTTTTTTTTTFFFFFFFFFFFTTTTTTTTTTT (e.g. the above scenario represents T = initially working, then F where something went wrong, then T again when someone fixed it). It is very unlikely statuses will toggle frequently like TFTFTFTF etc - run-length encoding will hence work very well in this situation
The RLE values to be calculated is simply the count for the current character, then the character itself - below you can see some example strings and how they should be encoded according to RLE:
Your program should only output the RLE version (e.g. like the "19T11F11T" above) and NOT the original string for the following string (note: this is a single line string - it gets wrapped in the textarea, but just copy it wholly - don't include any line breaks):
Paste your answer below:
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 | At minimum, will need a few important variables for this - the current consecutive count, the previous character, current character and the RLE result |
| 3 | Try with short strings first - think what you can initialise the current count to be with at the start so you can the correct count for the first character |
| 4 | We can loop through the string character by character - if the current character is equal to the previous character, we can increment the consecutive count - else, we should append the current count and previous character to the RLE string, then reset the current consecutive to 1 (not 0 - since assume we have "aaab" - once we have reached 'b', the count for 'b' should now be 1, not 0) |
| 5 | You will also have to make sure you output the ending characters too - they won't trigger an if statement that checks for a change between the current and previous character, so you can either have another condition in the if statement that appends to the RLE string if we are at the end of the string, or you can just do it once the loop ends |