From e8d913fafc920c1d7e8e7fb5bddce3c3b8a2a9fa Mon Sep 17 00:00:00 2001 From: eNascimento178 <85421753+eNascimento178@users.noreply.github.com> Date: Mon, 8 Jul 2024 17:32:04 -0300 Subject: [PATCH] add crypto-square exercise --- config.json | 8 ++ .../crypto-square/.docs/instructions.md | 71 +++++++++++++++ .../practice/crypto-square/.meta/config.json | 19 ++++ .../practice/crypto-square/.meta/example.ijs | 6 ++ .../practice/crypto-square/.meta/tests.toml | 34 +++++++ .../practice/crypto-square/crypto-square.ijs | 3 + exercises/practice/crypto-square/test.ijs | 89 +++++++++++++++++++ 7 files changed, 230 insertions(+) create mode 100644 exercises/practice/crypto-square/.docs/instructions.md create mode 100644 exercises/practice/crypto-square/.meta/config.json create mode 100644 exercises/practice/crypto-square/.meta/example.ijs create mode 100644 exercises/practice/crypto-square/.meta/tests.toml create mode 100644 exercises/practice/crypto-square/crypto-square.ijs create mode 100644 exercises/practice/crypto-square/test.ijs diff --git a/config.json b/config.json index b3039eb..d592830 100644 --- a/config.json +++ b/config.json @@ -385,6 +385,14 @@ "practices": [], "prerequisites": [], "difficulty": 8 + }, + { + "slug": "crypto-square", + "name": "Crypto Square", + "uuid": "5723544b-74d3-4483-b67e-5e7b513c8867", + "practices": [], + "prerequisites": [], + "difficulty": 4 } ] }, diff --git a/exercises/practice/crypto-square/.docs/instructions.md b/exercises/practice/crypto-square/.docs/instructions.md new file mode 100644 index 0000000..6c3826e --- /dev/null +++ b/exercises/practice/crypto-square/.docs/instructions.md @@ -0,0 +1,71 @@ +# Instructions + +Implement the classic method for composing secret messages called a square code. + +Given an English text, output the encoded version of that text. + +First, the input is normalized: the spaces and punctuation are removed from the English text and the message is down-cased. + +Then, the normalized characters are broken into rows. +These rows can be regarded as forming a rectangle when printed with intervening newlines. + +For example, the sentence + +```text +"If man was meant to stay on the ground, god would have given us roots." +``` + +is normalized to: + +```text +"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots" +``` + +The plaintext should be organized into a rectangle as square as possible. +The size of the rectangle should be decided by the length of the message. + +If `c` is the number of columns and `r` is the number of rows, then for the rectangle `r` x `c` find the smallest possible integer `c` such that: + +- `r * c >= length of message`, +- and `c >= r`, +- and `c - r <= 1`. + +Our normalized text is 54 characters long, dictating a rectangle with `c = 8` and `r = 7`: + +```text +"ifmanwas" +"meanttos" +"tayonthe" +"groundgo" +"dwouldha" +"vegivenu" +"sroots " +``` + +The coded message is obtained by reading down the columns going left to right. + +The message above is coded as: + +```text +"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau" +``` + +Output the encoded text in chunks that fill perfect rectangles `(r X c)`, with `c` chunks of `r` length, separated by spaces. +For phrases that are `n` characters short of the perfect rectangle, pad each of the last `n` chunks with a single trailing space. + +```text +"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau " +``` + +Notice that were we to stack these, we could visually decode the ciphertext back in to the original message: + +```text +"imtgdvs" +"fearwer" +"mayoogo" +"anouuio" +"ntnnlvt" +"wttddes" +"aohghn " +"sseoau " +``` diff --git a/exercises/practice/crypto-square/.meta/config.json b/exercises/practice/crypto-square/.meta/config.json new file mode 100644 index 0000000..7159f61 --- /dev/null +++ b/exercises/practice/crypto-square/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "eNascimento178" + ], + "files": { + "solution": [ + "crypto-square.ijs" + ], + "test": [ + "test.ijs" + ], + "example": [ + ".meta/example.ijs" + ] + }, + "blurb": "Implement the classic method for composing secret messages called a square code.", + "source": "J Dalbey's Programming Practice problems", + "source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" +} diff --git a/exercises/practice/crypto-square/.meta/example.ijs b/exercises/practice/crypto-square/.meta/example.ijs new file mode 100644 index 0000000..7567f87 --- /dev/null +++ b/exercises/practice/crypto-square/.meta/example.ijs @@ -0,0 +1,6 @@ +normalize=: ('[^0-9a-zA-Z]';'')&rxrplc@tolower +size=:{{ + if. 0 = y do. 0 0 return. end. + /:~ (, (([: <./ ] #~ y <: *) _1 0 1&+)) >.@%: y +}} +ciphertext=: ' ' joinstring (<"1)@:|:@:($!.' '~ size@#)@normalize diff --git a/exercises/practice/crypto-square/.meta/tests.toml b/exercises/practice/crypto-square/.meta/tests.toml new file mode 100644 index 0000000..085d142 --- /dev/null +++ b/exercises/practice/crypto-square/.meta/tests.toml @@ -0,0 +1,34 @@ +# 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. + +[407c3837-9aa7-4111-ab63-ec54b58e8e9f] +description = "empty plaintext results in an empty ciphertext" + +[aad04a25-b8bb-4304-888b-581bea8e0040] +description = "normalization results in empty plaintext" + +[64131d65-6fd9-4f58-bdd8-4a2370fb481d] +description = "Lowercase" + +[63a4b0ed-1e3c-41ea-a999-f6f26ba447d6] +description = "Remove spaces" + +[1b5348a1-7893-44c1-8197-42d48d18756c] +description = "Remove punctuation" + +[8574a1d3-4a08-4cec-a7c7-de93a164f41a] +description = "9 character plaintext results in 3 chunks of 3 characters" + +[a65d3fa1-9e09-43f9-bcec-7a672aec3eae] +description = "8 character plaintext results in 3 chunks, the last one with a trailing space" + +[fbcb0c6d-4c39-4a31-83f6-c473baa6af80] +description = "54 character plaintext results in 7 chunks, the last two with trailing spaces" diff --git a/exercises/practice/crypto-square/crypto-square.ijs b/exercises/practice/crypto-square/crypto-square.ijs new file mode 100644 index 0000000..1310960 --- /dev/null +++ b/exercises/practice/crypto-square/crypto-square.ijs @@ -0,0 +1,3 @@ +require 'general/unittest' + +ciphertext =: 'You need to implement this verb.'13!:8 (55) diff --git a/exercises/practice/crypto-square/test.ijs b/exercises/practice/crypto-square/test.ijs new file mode 100644 index 0000000..c118a86 --- /dev/null +++ b/exercises/practice/crypto-square/test.ijs @@ -0,0 +1,89 @@ +load 'crypto-square.ijs' + + +before_all=: monad define + (]Description =: (3 : 'descriptions=: i.0')`(3 : 'descriptions=: descriptions , < y'))@.0 '' + (]Order =: (3 : 'order=: i.0')`(3 : 'order=: order , < y'))@.0 '' + (]Task =: (3 : 'tasks=: i.0')`(3 : 'tasks=: tasks , < y'))@.0 '' +) + + +crypto_square_test_01_ignore=: 0 +test_crypto_square_test_01 =: monad define + Description@.1 ('empty plaintext results in an empty ciphertext') + Order@.1 (1) + + NB. plaintext=. 0$0 + NB. expected=. 0$0 + assert (0$0) -: ciphertext 0$0 +) + +crypto_square_test_02_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_02 =: monad define + Description@.1 ('normalization results in empty plaintext') + Order@.1 (2) + + NB. plaintext=. '... --- ...' + NB. expected=. 0$0 + assert (0$0) -: ciphertext '... --- ...' +) + +crypto_square_test_03_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_03 =: monad define + Description@.1 ('Lowercase') + Order@.1 (3) + + NB. plaintext=. 'A' + NB. expected=. 'a' + assert (,'a') -: ciphertext 'A' +) + +crypto_square_test_04_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_04 =: monad define + Description@.1 ('Remove spaces') + Order@.1 (4) + + NB. plaintext=. ' b ' + NB. expected=. 'b' + assert (,'b') -: ciphertext ' b ' +) + +crypto_square_test_05_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_05 =: monad define + Description@.1 ('Remove punctuation') + Order@.1 (5) + + NB. plaintext=. '@1,%!' + NB. expected=. '1' + assert (,'1') -: ciphertext '@1,%!' +) + +crypto_square_test_06_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_06 =: monad define + Description@.1 ('9 character plaintext results in 3 chunks of 3 characters') + Order@.1 (6) + + NB. plaintext=. 'This is fun!' + NB. expected=. 'tsf hiu isn' + assert 'tsf hiu isn' -: ciphertext 'This is fun!' +) + +crypto_square_test_07_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_07 =: monad define + Description@.1 ('8 character plaintext results in 3 chunks, the last one with a trailing space') + Order@.1 (7) + + NB. plaintext=. 'Chill out.' + NB. expected=. 'clu hlt io ' + assert 'clu hlt io ' -: ciphertext 'Chill out.' +) + +crypto_square_test_08_ignore=: 1 NB. Change this value to 0 to run this test +test_crypto_square_test_08 =: monad define + Description@.1 ('54 character plaintext results in 7 chunks, the last two with trailing spaces') + Order@.1 (8) + + NB. plaintext=. 'If man was meant to stay on the ground, god would have given us roots.' + NB. expected=. 'imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ' + assert 'imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ' -: ciphertext 'If man was meant to stay on the ground, god would have given us roots.' +)