Avatar of amirci

amirci's solution

to Run Length Encoding in the Swift Track

Published at Oct 09 2019 · 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.


Go through the project setup instructions for Xcode using Swift:


Notably from the source directory:

swift test runs tests
swift package generate-xcodeproj creates an Xcode project


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
@testable import RunLengthEncodingTests



import XCTest
@testable import RunLengthEncoding

class RunLengthEncodingTests: XCTestCase {
    func testEncodeSimple() {
        XCTAssertEqual(RunLengthEncoding.encode("AABBBCCCC"), "2A3B4C")

    func testDecodeSimple() {
        XCTAssertEqual(RunLengthEncoding.decode("2A3B4C"), "AABBBCCCC")

    func testEncodeWithSingleValues() {

    func testDecodeWithSingleValues() {

    func testDecodeEncodeCombination() {
        XCTAssertEqual(RunLengthEncoding.decode(RunLengthEncoding.encode("zzz ZZ  zZ")), "zzz ZZ  zZ")

    func testEncodeUnicode() {
        XCTAssertEqual(RunLengthEncoding.encode("⏰⚽⚽⚽⭐⭐⏰"), "⏰3⚽2⭐⏰")

    func testDecodeUnicode() {
        XCTAssertEqual(RunLengthEncoding.decode("⏰3⚽2⭐⏰"), "⏰⚽⚽⚽⭐⭐⏰")

    static var allTests: [(String, (RunLengthEncodingTests) -> () throws -> Void)] {
        return [
            ("testEncodeSimple", testEncodeSimple),
            ("testDecodeSimple", testDecodeSimple),
            ("testEncodeWithSingleValues", testEncodeWithSingleValues),
            ("testDecodeWithSingleValues", testDecodeWithSingleValues),
            ("testDecodeEncodeCombination", testDecodeEncodeCombination),
            ("testEncodeUnicode", testEncodeUnicode),
            ("testDecodeUnicode", testDecodeUnicode),
//Solution goes in Sources
import Foundation

extension String {
    func partitionBy(_ pred: (String.Element) -> String.Element) -> [String] {

        var rest = self
        var result: [String] = []
        while !rest.isEmpty  {
            let fst = rest.first!
            let idx = rest.firstIndex { $0 != fst }
            if idx == nil {
                rest = ""
            } else {
                let n = rest.distance(from: rest.startIndex, to: idx!)
                rest = String(rest.dropFirst(n))
        return result

struct RunLengthEncoding {
    private static func scanSize(_ scanner: Scanner) -> Int {
        var size = 1
        return size
    public static func encode(_ source: String) -> String {

        return source
            .partitionBy { $0 }
            .map { $0.count == 1 ? "\($0.first!)" : "\($0.count)\($0.first!)" }
    public static func decode(_ source: String) -> String {
        var result: String = ""
        let scanner = Scanner(string: source)
        scanner.charactersToBeSkipped = CharacterSet()
        while !scanner.isAtEnd {

            let size = scanSize(scanner)
            let idx = source.index(source.startIndex, offsetBy: scanner.scanLocation)
            let c = source[idx]
            scanner.scanLocation += 1
            let expanded = String(repeating: c, count: size)
        return result

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?