{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": ["# Run Length Encoding\n", "\n", "Implement run-length encoding and decoding.\n", "\n", "Run-length encoding (RLE) is a simple form of data compression, where runs\n", "(consecutive data elements) are replaced by just one data value and count.\n", "\n", "For example we can represent the original 53 characters with only 13.\n", "\n", "```text\n", "\"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB\" -> \"12WB12W3B24WB\"\n", "```\n", "\n", "RLE allows the original data to be perfectly reconstructed from\n", "the compressed data, which makes it a lossless data compression.\n", "\n", "```text\n", "\"AABCCCDEEEE\" -> \"2AB3CD4E\" -> \"AABCCCDEEEE\"\n", "```\n", "\n", "For simplicity, you can assume that the unencoded string will only contain\n", "the letters A through Z (either lower or upper case) and whitespace. This way\n", "data to be encoded will never contain any numbers and numbers inside data to\n", "be decoded always represent the count for the following character.\n", "\n", "## Source\n", "\n", "Wikipedia [https://en.wikipedia.org/wiki/Run-length_encoding](https://en.wikipedia.org/wiki/Run-length_encoding)\n", "\n", "## Version compatibility\n", "This exercise has been tested on Julia versions >=1.0.\n", "\n", "## Submitting Incomplete Solutions\n", "It's possible to submit an incomplete solution so you can see how others have completed the exercise."] }, { "cell_type": "markdown", "metadata": {}, "source": ["## Your solution"] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# submit\n", "function encode(s)\n", "end\n", "\n", "\n", "\n", "function decode(s)\n", "end"] }, { "cell_type": "markdown", "metadata": {}, "source": ["## Test suite"] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["using Test\n", "\n", "# include(\"run-length-encoding.jl\")\n", "\n", "\n", "# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0\n", "# Encode and decode the strings under the given specifications.\n", "\n", "@testset \"encode strings\" begin\n", " @test encode(\"\") == \"\"\n", " @test encode(\"XYZ\") == \"XYZ\"\n", " @test encode(\"AABBBCCCC\") == \"2A3B4C\"\n", " @test encode(\"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB\") == \"12WB12W3B24WB\"\n", " @test encode(\"aabbbcccc\") == \"2a3b4c\"\n", " @test encode(\" hsqq qww \") == \"2 hs2q q2w2 \"\n", "end\n", "\n", "@testset \"decode strings\" begin\n", " @test decode(\"\") == \"\"\n", " @test decode(\"XYZ\") == \"XYZ\"\n", " @test decode(\"2A3B4C\") == \"AABBBCCCC\"\n", " @test decode(\"12WB12W3B24WB\") == \"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB\"\n", " @test decode(\"2a3b4c\") == \"aabbbcccc\"\n", " @test decode(\"2 hs2q q2w2 \") == \" hsqq qww \"\n", "end\n", "\n", "@testset \"encode then decode\" begin\n", " @test decode(encode(\"zzz ZZ zZ\")) == \"zzz ZZ zZ\"\n", "end"] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare submission\n", "To submit your exercise, you need to save your solution in a file called `run-length-encoding.jl` before using the CLI.\n", "You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with `# submit` to the file `run-length-encoding.jl`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# using Pkg; Pkg.add(\"Exercism\")\n", "# using Exercism\n", "# Exercism.create_submission(\"run-length-encoding\")" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.3.0", "language": "julia", "name": "julia-1.3" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.3.0" } }, "nbformat": 4, "nbformat_minor": 2 }