Skip to content

Commit 5cffdef

Browse files
daveyarwooddevonestes
authored andcommitted
Make run-length-encoding exercise expected values consistent with README (#293)
* omit '1' on single-letter runs (fixes #292) * use test cases from canonical-data.json * replace incorrect example in doc string * update example solution to work with lowercase letters and whitespace
1 parent cef648e commit 5cffdef

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

exercises/run-length-encoding/example.exs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ defmodule RunLengthEncoder do
22

33
@spec encode(String.t) :: String.t
44
def encode(string) do
5-
Regex.scan(~r/([A-Z])\1*/, string)
6-
|> Enum.map_join(fn([run, c]) -> "#{String.length(run)}#{c}" end)
5+
Regex.scan(~r/([a-zA-Z ])\1*/, string)
6+
|> Enum.map_join(fn([run, c]) ->
7+
if String.match?(run, ~r/\s+/) do
8+
run
9+
else
10+
times = String.length(run)
11+
number = if times == 1 do "" else times end
12+
"#{number}#{c}"
13+
end
14+
end)
715
end
816

917
@spec decode(String.t) :: String.t
1018
def decode(string) do
11-
Regex.scan(~r/(\d+)(.)/, string)
12-
|> Enum.map_join(fn [_,n,c] -> String.duplicate(c, String.to_integer(n)) end)
19+
Regex.scan(~r/(\d*)(.)/, string)
20+
|> Enum.map_join(fn [_,n,c] ->
21+
times = if n == "" do 1 else String.to_integer(n) end
22+
String.duplicate(c, times)
23+
end)
1324
end
1425
end

exercises/run-length-encoding/rle.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
defmodule RunLengthEncoder do
22
@doc """
33
Generates a string where consecutive elements are represented as a data value and count.
4-
"HORSE" => "1H1O1R1S1E"
4+
"AABBBCCCC" => "2A3B4C"
55
For this example, assume all input are strings, that are all uppercase letters.
66
It should also be able to reconstruct the data into its original form.
7-
"1H1O1R1S1E" => "HORSE"
7+
"2A3B4C" => "AABBBCCCC"
88
"""
99
@spec encode(String.t) :: String.t
1010
def encode(string) do

exercises/run-length-encoding/rle_test.exs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,49 @@ ExUnit.configure exclude: :pending, trace: true
88
defmodule RunLengthEncoderTest do
99
use ExUnit.Case
1010

11-
test "empty string returns empty" do
11+
test "encode empty string" do
1212
assert RunLengthEncoder.encode("") === ""
1313
end
1414

1515
@tag :pending
16-
test "simple string gets encoded" do
17-
assert RunLengthEncoder.encode("AAA") === "3A"
16+
test "encode single characters only" do
17+
assert RunLengthEncoder.encode("XYZ") === "XYZ"
1818
end
1919

2020
@tag :pending
21-
test "more complicated string" do
22-
assert RunLengthEncoder.encode("HORSE") == "1H1O1R1S1E"
21+
test "decode empty string" do
22+
assert RunLengthEncoder.decode("") === ""
2323
end
2424

2525
@tag :pending
26-
test "an even more complex string" do
27-
assert RunLengthEncoder.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") === "12W1B12W3B24W1B"
26+
test "decode single characters only" do
27+
assert RunLengthEncoder.decode("XYZ") === "XYZ"
2828
end
2929

3030
@tag :pending
31-
test "it decodes an encoded simple string" do
32-
assert RunLengthEncoder.decode("3A") === "AAA"
31+
test "encode simple" do
32+
assert RunLengthEncoder.encode("AABBBCCCC") == "2A3B4C"
3333
end
3434

3535
@tag :pending
36-
test "it decodes a more complicated string" do
37-
assert RunLengthEncoder.decode("12W1B12W3B24W1B") === "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
36+
test "decode simple" do
37+
assert RunLengthEncoder.decode("2A3B4C") == "AABBBCCCC"
38+
end
39+
40+
@tag :pending
41+
test "encode with single values" do
42+
assert RunLengthEncoder.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") === "12WB12W3B24WB"
43+
end
44+
45+
@tag :pending
46+
test "decode with single values" do
47+
assert RunLengthEncoder.decode("12WB12W3B24WB") === "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
48+
end
49+
50+
@tag :pending
51+
test "decode(encode(...)) combination" do
52+
original = "zzz ZZ zZ"
53+
encoded = RunLengthEncoder.encode(original)
54+
assert RunLengthEncoder.decode(encoded) === original
3855
end
3956
end

0 commit comments

Comments
 (0)