-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add practice exercise: run-length-encoding #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
exercises/practice/run-length-encoding/.docs/instructions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Instructions | ||
|
||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jimmytty" | ||
], | ||
"files": { | ||
"solution": [ | ||
"run-length-encoding.sql" | ||
], | ||
"test": [ | ||
"run-length-encoding_test.sql" | ||
], | ||
"example": [ | ||
".meta/example.sql" | ||
] | ||
}, | ||
"blurb": "Implement run-length encoding and decoding.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Run-length_encoding" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
UPDATE "run-length-encoding" | ||
SET result = '' | ||
WHERE string = '' | ||
; | ||
|
||
UPDATE "run-length-encoding" | ||
SET result = ( | ||
WITH RECURSIVE rcte (input, chr, next_chr, idx, grp) AS ( | ||
VALUES (string, NULL, NULL, 0, 1) | ||
UNION ALL | ||
SELECT SUBSTR(input, 2), | ||
SUBSTR(input, 1, 1), | ||
SUBSTR(input, 2, 1), | ||
idx + 1, | ||
IIF(chr <> next_chr, grp + 1, grp) | ||
FROM rcte | ||
WHERE input <> '' | ||
) | ||
SELECT GROUP_CONCAT(PRINTF('%s%s', IIF(cnt = 1, '', cnt), chr), '') | ||
FROM ( | ||
SELECT COUNT(*) cnt, chr | ||
FROM rcte | ||
WHERE chr NOT NULL | ||
GROUP BY chr, grp | ||
ORDER BY idx | ||
) | ||
) | ||
WHERE property = 'encode' | ||
AND string <> '' | ||
; | ||
|
||
UPDATE "run-length-encoding" | ||
SET result = ( | ||
WITH RECURSIVE rcte (input, num, chr) AS ( | ||
VALUES (REPLACE(string, ' ', CHAR(7)), NULL, NULL) | ||
UNION ALL | ||
SELECT SUBSTR(LTRIM(input, CAST(input AS INT)), 2), | ||
CAST(input AS INT), | ||
SUBSTR(LTRIM(input, CAST(input AS INT)), 1, 1) | ||
FROM rcte | ||
WHERE input <> '' | ||
) | ||
SELECT REPLACE(GROUP_CONCAT(PRINTF('%.*c', num, chr), ''), CHAR(7), ' ') | ||
FROM rcte | ||
WHERE chr NOT NULL | ||
) | ||
WHERE property = 'decode' | ||
AND string <> '' | ||
; | ||
|
||
UPDATE "run-length-encoding" | ||
SET result = ( | ||
WITH RECURSIVE rcte (input, num, chr) AS ( | ||
VALUES (REPLACE( | ||
( | ||
WITH RECURSIVE rcte (input, chr, next_chr, idx, grp) AS ( | ||
VALUES (string, NULL, NULL, 0, 1) | ||
UNION ALL | ||
SELECT SUBSTR(input, 2), | ||
SUBSTR(input, 1, 1), | ||
SUBSTR(input, 2, 1), | ||
idx + 1, | ||
IIF(chr <> next_chr, grp + 1, grp) | ||
FROM rcte | ||
WHERE input <> '' | ||
) | ||
SELECT GROUP_CONCAT(PRINTF('%s%s', IIF(cnt = 1, '', cnt), chr), '') | ||
FROM ( | ||
SELECT COUNT(*) cnt, chr | ||
FROM rcte | ||
WHERE chr NOT NULL | ||
GROUP BY chr, grp | ||
ORDER BY idx | ||
) | ||
), ' ', CHAR(7)), NULL, NULL) | ||
UNION ALL | ||
SELECT SUBSTR(LTRIM(input, CAST(input AS INT)), 2), | ||
CAST(input AS INT), | ||
SUBSTR(LTRIM(input, CAST(input AS INT)), 1, 1) | ||
FROM rcte | ||
WHERE input <> '' | ||
) | ||
SELECT REPLACE(GROUP_CONCAT(PRINTF('%.*c', num, chr), ''), CHAR(7), ' ') | ||
FROM rcte | ||
WHERE chr NOT NULL | ||
) | ||
WHERE property = 'consistency' | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[ad53b61b-6ffc-422f-81a6-61f7df92a231] | ||
description = "run-length encode a string -> empty string" | ||
|
||
[52012823-b7e6-4277-893c-5b96d42f82de] | ||
description = "run-length encode a string -> single characters only are encoded without count" | ||
|
||
[b7868492-7e3a-415f-8da3-d88f51f80409] | ||
description = "run-length encode a string -> string with no single characters" | ||
|
||
[859b822b-6e9f-44d6-9c46-6091ee6ae358] | ||
description = "run-length encode a string -> single characters mixed with repeated characters" | ||
|
||
[1b34de62-e152-47be-bc88-469746df63b3] | ||
description = "run-length encode a string -> multiple whitespace mixed in string" | ||
|
||
[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a] | ||
description = "run-length encode a string -> lowercase characters" | ||
|
||
[7ec5c390-f03c-4acf-ac29-5f65861cdeb5] | ||
description = "run-length decode a string -> empty string" | ||
|
||
[ad23f455-1ac2-4b0e-87d0-b85b10696098] | ||
description = "run-length decode a string -> single characters only" | ||
|
||
[21e37583-5a20-4a0e-826c-3dee2c375f54] | ||
description = "run-length decode a string -> string with no single characters" | ||
|
||
[1389ad09-c3a8-4813-9324-99363fba429c] | ||
description = "run-length decode a string -> single characters with repeated characters" | ||
|
||
[3f8e3c51-6aca-4670-b86c-a213bf4706b0] | ||
description = "run-length decode a string -> multiple whitespace mixed in string" | ||
|
||
[29f721de-9aad-435f-ba37-7662df4fb551] | ||
description = "run-length decode a string -> lowercase string" | ||
|
||
[2a762efd-8695-4e04-b0d6-9736899fbc16] | ||
description = "encode and then decode -> encode followed by decode gives original string" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DROP TABLE IF EXISTS "run-length-encoding"; | ||
CREATE TABLE "run-length-encoding" ( | ||
property TEXT NOT NULL, | ||
string TEXT NOT NULL, | ||
result TEXT | ||
); | ||
|
||
.mode csv | ||
.import ./data.csv "run-length-encoding" | ||
|
||
UPDATE "run-length-encoding" SET result = NULL; |
33 changes: 33 additions & 0 deletions
33
exercises/practice/run-length-encoding/create_test_table.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
DROP TABLE IF EXISTS tests; | ||
CREATE TABLE IF NOT EXISTS tests ( | ||
-- uuid and description are taken from the test.toml file | ||
uuid TEXT PRIMARY KEY, | ||
description TEXT NOT NULL, | ||
-- The following section is needed by the online test-runner | ||
status TEXT DEFAULT 'fail', | ||
message TEXT, | ||
output TEXT, | ||
test_code TEXT, | ||
task_id INTEGER DEFAULT NULL, | ||
-- Here are columns for the actual tests | ||
property TEXT NOT NULL, | ||
string TEXT NOT NULL, | ||
expected TEXT NOT NULL | ||
); | ||
|
||
INSERT INTO tests (uuid, description, property, string, expected) | ||
VALUES | ||
('ad53b61b-6ffc-422f-81a6-61f7df92a231', 'empty string', 'encode', '', ''), | ||
IsaacG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
('52012823-b7e6-4277-893c-5b96d42f82de', 'single characters only are encoded without count', 'encode', 'XYZ', 'XYZ'), | ||
('b7868492-7e3a-415f-8da3-d88f51f80409', 'string with no single characters', 'encode', 'AABBBCCCC', '2A3B4C'), | ||
('859b822b-6e9f-44d6-9c46-6091ee6ae358', 'single characters mixed with repeated characters', 'encode', 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB', '12WB12W3B24WB'), | ||
('1b34de62-e152-47be-bc88-469746df63b3', 'multiple whitespace mixed in string', 'encode', ' hsqq qww ', '2 hs2q q2w2 '), | ||
('abf176e2-3fbd-40ad-bb2f-2dd6d4df721a', 'lowercase characters', 'encode', 'aabbbcccc', '2a3b4c'), | ||
('7ec5c390-f03c-4acf-ac29-5f65861cdeb5', 'empty string', 'decode', '', ''), | ||
('ad23f455-1ac2-4b0e-87d0-b85b10696098', 'single characters only', 'decode', 'XYZ', 'XYZ'), | ||
('21e37583-5a20-4a0e-826c-3dee2c375f54', 'string with no single characters', 'decode', '2A3B4C', 'AABBBCCCC'), | ||
('1389ad09-c3a8-4813-9324-99363fba429c', 'single characters with repeated characters', 'decode', '12WB12W3B24WB', 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB'), | ||
('3f8e3c51-6aca-4670-b86c-a213bf4706b0', 'multiple whitespace mixed in string', 'decode', '2 hs2q q2w2 ', ' hsqq qww '), | ||
('29f721de-9aad-435f-ba37-7662df4fb551', 'lowercase string', 'decode', '2a3b4c', 'aabbbcccc'), | ||
('2a762efd-8695-4e04-b0d6-9736899fbc16', 'encode followed by decode gives original string', 'consistency', 'zzz ZZ zZ', 'zzz ZZ zZ'); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"encode","", | ||
"encode","XYZ", | ||
"encode","AABBBCCCC", | ||
"encode","WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", | ||
"encode"," hsqq qww ", | ||
"encode","aabbbcccc", | ||
"decode","", | ||
"decode","XYZ", | ||
"decode","2A3B4C", | ||
"decode","12WB12W3B24WB", | ||
"decode","2 hs2q q2w2 ", | ||
"decode","2a3b4c", | ||
"consistency","zzz ZZ zZ", |
8 changes: 8 additions & 0 deletions
8
exercises/practice/run-length-encoding/run-length-encoding.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Schema: | ||
-- CREATE TABLE "run-length-encoding" ( | ||
-- property TEXT NOT NULL, | ||
-- string TEXT NOT NULL, | ||
-- result TEXT | ||
-- ); | ||
-- | ||
-- Task: update the run-length-encoding table and set the result based on the property and string. |
43 changes: 43 additions & 0 deletions
43
exercises/practice/run-length-encoding/run-length-encoding_test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
-- Create database: | ||
.read ./create_fixture.sql | ||
|
||
-- Read user student solution and save any output as markdown in user_output.md: | ||
.mode markdown | ||
.output user_output.md | ||
.read ./run-length-encoding.sql | ||
.output | ||
|
||
-- Create a clean testing environment: | ||
.read ./create_test_table.sql | ||
|
||
-- Comparison of user input and the tests updates the status for each test: | ||
UPDATE tests | ||
IsaacG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SET status = 'pass' | ||
FROM (SELECT property, string, result FROM "run-length-encoding") AS actual | ||
WHERE (actual.property, actual.string, actual.result) = | ||
( tests.property, tests.string, tests.expected); | ||
|
||
-- Update message for failed tests to give helpful information: | ||
UPDATE tests | ||
SET message = ( | ||
'Result for "' | ||
|| PRINTF('property=%s, string=%s', tests.property, tests.string) | ||
|| '"' | ||
|| ' is <' || COALESCE(actual.result, 'NULL') | ||
|| '> but should be <' || tests.expected || '>' | ||
) | ||
FROM (SELECT property, string, result FROM "run-length-encoding") AS actual | ||
WHERE (actual.property, actual.string) = | ||
( tests.property, tests.string) | ||
AND tests.status = 'fail'; | ||
|
||
-- Save results to ./output.json (needed by the online test-runner) | ||
.mode json | ||
.once './output.json' | ||
SELECT description, status, message, output, test_code, task_id | ||
FROM tests; | ||
|
||
-- Display test results in readable form for the student: | ||
.mode table | ||
SELECT description, status, message | ||
FROM tests; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.