🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of jaishreedhage

jaishreedhage's solution

to Run Length Encoding in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Test suite

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.


RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.


For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/run-length-encoding/RunLengthEncoding.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#import <XCTest/XCTest.h>

#if __has_include("RunLengthEncodingExample.h")
# import "RunLengthEncodingExample.h"
# else
# import "RunLengthEncoding.h"

@interface RunLengthEncodingTest : XCTestCase


@implementation RunLengthEncodingTest

- (void)testEncodeSimple {
    XCTAssertEqualObjects([RunLengthEncoding encode:@"AABBBCCCC"], @"2A3B4C");

- (void)testDecodeSimple {
    XCTAssertEqualObjects([RunLengthEncoding decode:@"2A3B4C"], @"AABBBCCCC");

- (void)testEncodeWithSingleValues {

- (void)testDecodeWithSingleValues {

- (void)testDecodeEncodeCombination {
    XCTAssertEqualObjects([RunLengthEncoding decode:[RunLengthEncoding encode:@"zzz ZZ  zZ"]], @"zzz ZZ  zZ");

- (void)testEncodeUnicode {
    XCTAssertEqualObjects([RunLengthEncoding encode:@"⏰⚽⚽⚽⭐⭐⏰"], @"⏰3⚽2⭐⏰");

- (void)testDecodeUnicode {
    XCTAssertEqualObjects([RunLengthEncoding decode:@"⏰3⚽2⭐⏰"], @"⏰⚽⚽⚽⭐⭐⏰");



//  RunLengthEncoding.h
//  RunLengthEncoding
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.

#import <Foundation/Foundation.h>

@interface RunLengthEncoding : NSObject

+(NSString *)encode : (NSString *)string;
+(NSString *)decode : (NSString *)string;



//  RunLengthEncoding.m
//  RunLengthEncoding
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.

#import "RunLengthEncoding.h"

@implementation RunLengthEncoding

+(NSString *)encode:(NSString *)string{
    NSMutableString *encodedString = [NSMutableString string];
    for(int i=0;i<string.length;i++){
        int j = i;
        while(j<string.length && [string characterAtIndex:i] == [string characterAtIndex:j] )
            [encodedString appendFormat:@"%d",(j-i)];
        [encodedString appendFormat:@"%c",[string characterAtIndex:i]];
        i = j-1;
    return encodedString;

+(NSString *)decode:(NSString *)string{
    NSMutableString *decodeString = [NSMutableString string];
    for(int i=0;i<string.length;i++){
        int j = i;
        unichar c = [string characterAtIndex:i];
        if(c >= '0' && c<='9'){
            while(j<string.length && [string characterAtIndex:j]>='0' && [string characterAtIndex:j]<='9'){
            NSInteger value = [[string substringWithRange:NSMakeRange(i, j-i)] integerValue];
            for(int k=0;k<value;k++){
                [decodeString appendFormat:@"%c",[string characterAtIndex:j]];
            [decodeString appendFormat:@"%c",c];
    return decodeString;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?