Initial upload

This commit is contained in:
2022-08-24 14:28:45 +02:00
parent c67653ddee
commit 57bc7b0289
370 changed files with 18479 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"run-length-encoding","id":"bd55331dd6c8411595b7f396026666c9","url":"https://exercism.io/my/solutions/bd55331dd6c8411595b7f396026666c9","handle":"halfdan","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,34 @@
# Run Length Encoding
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.
```text
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
```
RLE allows the original data to be perfectly reconstructed from
the compressed data, which makes it a lossless data compression.
```text
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
```
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.
## Source
Wikipedia [https://en.wikipedia.org/wiki/Run-length_encoding](https://en.wikipedia.org/wiki/Run-length_encoding)
## Version compatibility
This exercise has been tested on Julia versions >=1.0.
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@@ -0,0 +1,68 @@
{
"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
}

View File

@@ -0,0 +1,34 @@
function encode(s)
ch = nothing
cnt = 1
result = ""
for c s
if ch nothing
ch = c
continue
end
if ch == c
cnt += 1
else
result *= cnt > 1 ? "$cnt$ch" : "$ch"
ch = c
cnt = 1
end
end
if ch nothing
result *= cnt > 1 ? "$cnt$ch" : "$ch"
end
result
end
function decode(s)
items = collect(eachmatch(r"(\d+)?(.)", s))
result = ""
for item items
cnt = parse(Int, (item[1] nothing) ? "1" : item[1])
result *= item[2]^cnt
end
result
end

View File

@@ -0,0 +1,29 @@
using Test
include("run-length-encoding.jl")
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
# Encode and decode the strings under the given specifications.
@testset "encode strings" begin
@test encode("") == ""
@test encode("XYZ") == "XYZ"
@test encode("AABBBCCCC") == "2A3B4C"
@test encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") == "12WB12W3B24WB"
@test encode("aabbbcccc") == "2a3b4c"
@test encode(" hsqq qww ") == "2 hs2q q2w2 "
end
@testset "decode strings" begin
@test decode("") == ""
@test decode("XYZ") == "XYZ"
@test decode("2A3B4C") == "AABBBCCCC"
@test decode("12WB12W3B24WB") == "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
@test decode("2a3b4c") == "aabbbcccc"
@test decode("2 hs2q q2w2 ") == " hsqq qww "
end
@testset "encode then decode" begin
@test decode(encode("zzz ZZ zZ")) == "zzz ZZ zZ"
end