-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add practice exercise: rotational-cipher #136
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
29 changes: 29 additions & 0 deletions
29
exercises/practice/rotational-cipher/.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,29 @@ | ||
| # Instructions | ||
|
|
||
| Create an implementation of the rotational cipher, also sometimes called the Caesar cipher. | ||
|
|
||
| The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. | ||
| Using a key of `0` or `26` will always yield the same output due to modular arithmetic. | ||
| The letter is shifted for as many values as the value of the key. | ||
|
|
||
| The general notation for rotational ciphers is `ROT + <key>`. | ||
| The most commonly used rotational cipher is `ROT13`. | ||
|
|
||
| A `ROT13` on the Latin alphabet would be as follows: | ||
|
|
||
| ```text | ||
| Plain: abcdefghijklmnopqrstuvwxyz | ||
| Cipher: nopqrstuvwxyzabcdefghijklm | ||
| ``` | ||
|
|
||
| It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys. | ||
|
|
||
| Ciphertext is written out in the same formatting as the input including spaces and punctuation. | ||
|
|
||
| ## Examples | ||
|
|
||
| - ROT5 `omg` gives `trl` | ||
| - ROT0 `c` gives `c` | ||
| - ROT26 `Cool` gives `Cool` | ||
| - ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` | ||
| - ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.` |
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": [ | ||
| "rotational-cipher.sql" | ||
| ], | ||
| "test": [ | ||
| "rotational-cipher_test.sql" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.sql" | ||
| ] | ||
| }, | ||
| "blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Caesar_cipher" | ||
| } |
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 @@ | ||
| UPDATE "rotational-cipher" | ||
| SET result = ( | ||
| WITH RECURSIVE rcte (text, chr, shift) AS ( | ||
| VALUES(text, NULL, NULL) | ||
| UNION ALL | ||
| SELECT | ||
| SUBSTR(text, 2), | ||
| SUBSTR(text, 1, 1), | ||
| IIF( | ||
| GLOB('[A-Za-z]', SUBSTR(text, 1, 1)), | ||
| UNICODE(SUBSTR(text, 1, 1)) + shift_key, | ||
| UNICODE(SUBSTR(text, 1, 1)) | ||
| ) | ||
| FROM rcte | ||
| WHERE text <> '' | ||
| ) | ||
| SELECT | ||
| GROUP_CONCAT( | ||
| CHAR( | ||
| CASE | ||
| WHEN GLOB('[A-Z]', chr) AND shift > UNICODE('Z') | ||
| THEN shift - UNICODE('Z') + UNICODE('A') - 1 | ||
| WHEN GLOB('[a-z]', chr) AND shift > UNICODE('z') | ||
| THEN shift - UNICODE('z') + UNICODE('a') - 1 | ||
| ELSE shift | ||
| END | ||
| ), | ||
| '' | ||
| ) | ||
| FROM rcte | ||
| WHERE chr NOTNULL | ||
| ) | ||
| ; |
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,40 @@ | ||
| # 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. | ||
|
|
||
| [74e58a38-e484-43f1-9466-877a7515e10f] | ||
| description = "rotate a by 0, same output as input" | ||
|
|
||
| [7ee352c6-e6b0-4930-b903-d09943ecb8f5] | ||
| description = "rotate a by 1" | ||
|
|
||
| [edf0a733-4231-4594-a5ee-46a4009ad764] | ||
| description = "rotate a by 26, same output as input" | ||
|
|
||
| [e3e82cb9-2a5b-403f-9931-e43213879300] | ||
| description = "rotate m by 13" | ||
|
|
||
| [19f9eb78-e2ad-4da4-8fe3-9291d47c1709] | ||
| description = "rotate n by 13 with wrap around alphabet" | ||
|
|
||
| [a116aef4-225b-4da9-884f-e8023ca6408a] | ||
| description = "rotate capital letters" | ||
|
|
||
| [71b541bb-819c-4dc6-a9c3-132ef9bb737b] | ||
| description = "rotate spaces" | ||
|
|
||
| [ef32601d-e9ef-4b29-b2b5-8971392282e6] | ||
| description = "rotate numbers" | ||
|
|
||
| [32dd74f6-db2b-41a6-b02c-82eb4f93e549] | ||
| description = "rotate punctuation" | ||
|
|
||
| [9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9] | ||
| description = "rotate all letters" |
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 "rotational-cipher"; | ||
| CREATE TABLE "rotational-cipher" ( | ||
| text TEXT NOT NULL, | ||
| shift_key INTEGER NOT NULL, | ||
| result TEXT | ||
| ); | ||
|
|
||
| .mode csv | ||
| .import ./data.csv "rotational-cipher" | ||
|
|
||
| UPDATE "rotational-cipher" SET result = NULL; |
29 changes: 29 additions & 0 deletions
29
exercises/practice/rotational-cipher/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,29 @@ | ||
| 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 | ||
| text TEXT NOT NULL, | ||
| shift_key INTEGER NOT NULL, | ||
| expected TEXT NOT NULL | ||
| ); | ||
|
|
||
| INSERT INTO tests (uuid, description, text, shift_key, expected) | ||
| VALUES | ||
| ('74e58a38-e484-43f1-9466-877a7515e10f', 'rotate a by 0, same output as input', 'a', 0, 'a'), | ||
| ('7ee352c6-e6b0-4930-b903-d09943ecb8f5', 'rotate a by 1', 'a', 1, 'b'), | ||
| ('edf0a733-4231-4594-a5ee-46a4009ad764', 'rotate a by 26, same output as input', 'a', 26, 'a'), | ||
| ('e3e82cb9-2a5b-403f-9931-e43213879300', 'rotate m by 13', 'm', 13, 'z'), | ||
| ('19f9eb78-e2ad-4da4-8fe3-9291d47c1709', 'rotate n by 13 with wrap around alphabet', 'n', 13, 'a'), | ||
| ('a116aef4-225b-4da9-884f-e8023ca6408a', 'rotate capital letters', 'OMG', 5, 'TRL'), | ||
| ('71b541bb-819c-4dc6-a9c3-132ef9bb737b', 'rotate spaces', 'O M G', 5, 'T R L'), | ||
| ('ef32601d-e9ef-4b29-b2b5-8971392282e6', 'rotate numbers', 'Testing 1 2 3 testing', 4, 'Xiwxmrk 1 2 3 xiwxmrk'), | ||
| ('32dd74f6-db2b-41a6-b02c-82eb4f93e549', 'rotate punctuation', 'Let''s eat, Grandma!', 21, 'Gzo''n zvo, Bmviyhv!'), | ||
| ('9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9', 'rotate all letters', 'The quick brown fox jumps over the lazy dog.', 13, 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt.'); |
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,10 @@ | ||
| "a",0, | ||
| "a",1, | ||
| "a",26, | ||
| "m",13, | ||
| "n",13, | ||
| "OMG",5, | ||
| "O M G",5, | ||
| "Testing 1 2 3 testing",4, | ||
| "Let's eat, Grandma!",21, | ||
| "The quick brown fox jumps over the lazy dog.",13, |
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 "rotational-cipher" ( | ||
| -- text TEXT NOT NULL, | ||
| -- shift_key INTEGER NOT NULL, | ||
| -- result TEXT | ||
| -- ); | ||
| -- | ||
| -- Task: update the rotational-cipher table and set the result based on text and shift_key. |
40 changes: 40 additions & 0 deletions
40
exercises/practice/rotational-cipher/rotational-cipher_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,40 @@ | ||
| -- 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 ./rotational-cipher.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 | ||
| SET status = 'pass' | ||
| FROM (SELECT text, shift_key, result FROM "rotational-cipher") AS actual | ||
| WHERE (actual.text, actual.shift_key, actual.result) = | ||
| (tests.text, tests.shift_key, tests.expected); | ||
|
|
||
| -- Update message for failed tests to give helpful information: | ||
| UPDATE tests | ||
| SET message = ( | ||
| 'Result for "' | ||
| || PRINTF('text=%s, shift_key=%d', tests.text, tests.shift_key) | ||
| || '"' || ' is <' || COALESCE(actual.result, 'NULL') | ||
| || '> but should be <' || tests.expected || '>' ) | ||
| FROM (SELECT text, shift_key, result FROM "rotational-cipher") AS actual | ||
| WHERE (actual.text, actual.shift_key) = (tests.text, tests.shift_key) | ||
| 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; | ||
IsaacG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- 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.