diff --git a/config.json b/config.json index 75aa6aad..73ccb357 100644 --- a/config.json +++ b/config.json @@ -820,6 +820,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "word-count", + "name": "Word Count", + "uuid": "5fded933-439a-4faa-bfb6-18ec7b7c8469", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "binary-search-tree", "name": "Binary Search Tree", @@ -852,6 +860,14 @@ "prerequisites": [], "difficulty": 6 }, + { + "slug": "transpose", + "name": "Transpose", + "uuid": "59b5d30b-62c1-4465-a44d-485c45f4aac2", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "isbn-verifier", "name": "ISBN Verifier", diff --git a/exercises/practice/transpose/.docs/instructions.md b/exercises/practice/transpose/.docs/instructions.md new file mode 100644 index 00000000..6033af74 --- /dev/null +++ b/exercises/practice/transpose/.docs/instructions.md @@ -0,0 +1,61 @@ +# Instructions + +Given an input text output it transposed. + +Roughly explained, the transpose of a matrix: + +```text +ABC +DEF +``` + +is given by: + +```text +AD +BE +CF +``` + +Rows become columns and columns become rows. +See [transpose][]. + +If the input has rows of different lengths, this is to be solved as follows: + +- Pad to the left with spaces. +- Don't pad to the right. + +Therefore, transposing this matrix: + +```text +ABC +DE +``` + +results in: + +```text +AD +BE +C +``` + +And transposing: + +```text +AB +DEF +``` + +results in: + +```text +AD +BE + F +``` + +In general, all characters from the input should also be present in the transposed output. +That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s). + +[transpose]: https://en.wikipedia.org/wiki/Transpose diff --git a/exercises/practice/transpose/.meta/config.json b/exercises/practice/transpose/.meta/config.json new file mode 100644 index 00000000..6b7c6481 --- /dev/null +++ b/exercises/practice/transpose/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "Ephraim-nonso" + ], + "files": { + "solution": [ + "src/lib.cairo" + ], + "test": [ + "tests/transpose.cairo" + ], + "example": [ + ".meta/example.cairo" + ], + "invalidator": [ + "Scarb.toml" + ] + }, + "blurb": "Take input text and output it transposed.", + "source": "Reddit r/dailyprogrammer challenge #270 [Easy].", + "source_url": "https://web.archive.org/web/20230630051421/https://old.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text/" +} diff --git a/exercises/practice/transpose/.meta/example.cairo b/exercises/practice/transpose/.meta/example.cairo new file mode 100644 index 00000000..964aca9e --- /dev/null +++ b/exercises/practice/transpose/.meta/example.cairo @@ -0,0 +1,35 @@ +pub fn transpose(input: Array) -> Array { + let mut output: Array = ArrayTrait::new(); + + let mut max_length = 0; + for line in input.clone() { + if line.len() > max_length { + max_length = line.len(); + } + }; + + let mut i = 0; + loop { + let mut temp: ByteArray = ""; + for line in input + .clone() { + while i < max_length { + match line.at(i) { + Option::Some(char) => { temp.append_byte(char); }, + Option::None => { temp.append_byte(' '); }, + } + break; + }; + if temp.len() == input.len() { + output.append(temp.clone()); + i += 1; + } + continue; + }; + if i == max_length { + break; + } + }; + + output +} diff --git a/exercises/practice/transpose/.meta/tests.toml b/exercises/practice/transpose/.meta/tests.toml new file mode 100644 index 00000000..32e366fb --- /dev/null +++ b/exercises/practice/transpose/.meta/tests.toml @@ -0,0 +1,46 @@ +# 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. + +[404b7262-c050-4df0-a2a2-0cb06cd6a821] +description = "empty string" + +[a89ce8a3-c940-4703-a688-3ea39412fbcb] +description = "two characters in a row" + +[855bb6ae-4180-457c-abd0-ce489803ce98] +description = "two characters in a column" + +[5ceda1c0-f940-441c-a244-0ced197769c8] +description = "simple" + +[a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f] +description = "single line" + +[0dc2ec0b-549d-4047-aeeb-8029fec8d5c5] +description = "first line longer than second line" + +[984e2ec3-b3d3-4b53-8bd6-96f5ef404102] +description = "second line longer than first line" + +[eccd3784-45f0-4a3f-865a-360cb323d314] +description = "mixed line length" + +[85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d] +description = "square" + +[b9257625-7a53-4748-8863-e08e9d27071d] +description = "rectangle" + +[b80badc9-057e-4543-bd07-ce1296a1ea2c] +description = "triangle" + +[76acfd50-5596-4d05-89f1-5116328a7dd9] +description = "jagged triangle" diff --git a/exercises/practice/transpose/Scarb.toml b/exercises/practice/transpose/Scarb.toml new file mode 100644 index 00000000..2f6d0627 --- /dev/null +++ b/exercises/practice/transpose/Scarb.toml @@ -0,0 +1,7 @@ +[package] +name = "transpose" +version = "0.1.0" +edition = "2024_07" + +[dev-dependencies] +cairo_test = "2.9.2" diff --git a/exercises/practice/transpose/src/lib.cairo b/exercises/practice/transpose/src/lib.cairo new file mode 100644 index 00000000..16834eaf --- /dev/null +++ b/exercises/practice/transpose/src/lib.cairo @@ -0,0 +1,3 @@ +pub fn transpose(input: Array) -> Array { + panic!("implement `transpose`") +} diff --git a/exercises/practice/transpose/tests/transpose.cairo b/exercises/practice/transpose/tests/transpose.cairo new file mode 100644 index 00000000..e5d07532 --- /dev/null +++ b/exercises/practice/transpose/tests/transpose.cairo @@ -0,0 +1,174 @@ +use transpose::transpose as transpose_matrix; + +#[test] +fn empty_string() { + let mut input: Array = array![]; + let expected = array![]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn two_characters_in_a_row() { + let mut input: Array = array!["A1"]; + let expected = array!["A", "1"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn two_characters_in_a_column() { + let mut input: Array = array!["A", "1"]; + let expected = array!["A1"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn simple() { + let mut input: Array = array!["ABC", "123"]; + let expected = array!["A1", "B2", "C3"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn single_line() { + let mut input: Array = array!["Single line."]; + let expected = array!["S", "i", "n", "g", "l", "e", " ", "l", "i", "n", "e", "."]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn first_line_longer_than_second_line() { + let mut input: Array = array!["The fourth line.", "The fifth line."]; + let expected = array![ + "TT", + "hh", + "ee", + " ", + "ff", + "oi", + "uf", + "rt", + "th", + "h ", + " l", + "li", + "in", + "ne", + "e.", + ". " + ]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn second_line_longer_than_first_line() { + let mut input: Array = array!["The first line.", "The second line."]; + let expected = array![ + "TT", + "hh", + "ee", + " ", + "fs", + "ie", + "rc", + "so", + "tn", + " d", + "l ", + "il", + "ni", + "en", + ".e", + " ." + ]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn mixed_line_length() { + let mut input: Array = array![ + "The longest line.", "A long line.", "A longer line.", "A line." + ]; + let expected = array![ + "TAAA", + "h ", + "elll", + " ooi", + "lnnn", + "ogge", + "n e.", + "glr ", + "ei ", + "snl ", + "tei ", + " .n ", + "l e ", + "i . ", + "n ", + "e ", + ". " + ]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn square() { + let mut input: Array = array!["HEART", "EMBER", "ABUSE", "RESIN", "TREND"]; + let expected = array!["HEART", "EMBER", "ABUSE", "RESIN", "TREND"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn rectangle() { + let mut input: Array = array!["FRACTURE", "OUTLINED", "BLOOMING", "SEPTETTE"]; + let expected = array!["FOBS", "RULE", "ATOP", "CLOT", "TIME", "UNIT", "RENT", "EDGE"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn triangle() { + let mut input: Array = array!["T", "EE", "AAA", "SSSS", "EEEEE", "RRRRRR"]; + let expected = array!["TEASER", " EASER", " ASER", " SER", " ER", " R"]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn jagged_triangle() { + let mut input: Array = array!["11", "2", "3333", "444", "555555", "66666"]; + let expected = array!["123456", "1 3456", " 3456", " 3 56", " 56", " 5 "]; + + let output = transpose_matrix(input); + assert_eq!(output, expected); +}