-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
transpose exercise #315
base: main
Are you sure you want to change the base?
transpose exercise #315
Changes from 75 commits
91d8a54
e90852c
ac52fe1
e865f05
96d9bca
52de5af
74f359c
c96e3f4
009cf95
8a8a8cf
978ac07
70ebe7e
8a4a4e7
e3544cb
49d20ee
321923e
89d0d01
9456bba
d031586
2262bd4
d77ac92
100e118
98ddc2c
df51470
ecdd0b6
b8adcde
63bddac
b1a4c1f
066787c
9ac273e
0727ecc
69dab2f
3b851a6
eca178d
35e9e18
2b54929
26ff5d6
925e6af
008fd0e
281fd43
9f03b7f
df78767
2470417
0c7ed6b
3219156
4a876f0
62f50c7
75d5220
deb3b7f
7abdaab
4d8892c
77b5c4e
2b7d363
12fae98
ac4b1ab
44213ae
4dd9516
8ae1404
435ff6a
05a2367
401954a
cdbd0fe
2ef1886
5f31d61
55de0ef
9947334
3b6f7c6
86c7638
8e6ff95
068a20b
1bc2263
3c90f2d
0df06b2
1f63916
4cd5596
13dfe94
1551365
f497c86
5ed367c
c32d87f
d4e956e
a3f3857
df62909
3de1993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It's not a trivial exercise, let's increase the difficulty |
||||||
}, | ||||||
{ | ||||||
"slug": "isbn-verifier", | ||||||
"name": "ISBN Verifier", | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
pub fn transpose(input: Array<ByteArray>) -> Array<ByteArray> { | ||
let mut output: Array<ByteArray> = 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It must be possible to avoid having a Find a way to refactor this into something more readable. You can use Go implementation as inspiration, it is very clear and concise |
||
.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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "transpose" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
[dev-dependencies] | ||
cairo_test = "2.8.2" | ||
0xNeshi marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn transpose(input: Array<ByteArray>) -> Array<ByteArray> { | ||
panic!("implement `transpose`") | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,174 @@ | ||||||
use transpose::transpose as transpose_matrix; | ||||||
|
||||||
#[test] | ||||||
fn empty_string() { | ||||||
let mut input: Array<ByteArray> = array![]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
no need for |
||||||
let expected = array![]; | ||||||
|
||||||
let output = transpose_matrix(input); | ||||||
assert_eq!(output, expected); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
#[ignore] | ||||||
fn two_characters_in_a_row() { | ||||||
let mut input: Array<ByteArray> = 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<ByteArray> = array!["A", "1"]; | ||||||
let expected = array!["A1"]; | ||||||
|
||||||
let output = transpose_matrix(input); | ||||||
assert_eq!(output, expected); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
#[ignore] | ||||||
fn simple() { | ||||||
let mut input: Array<ByteArray> = array!["ABC", "123"]; | ||||||
let expected = array!["A1", "B2", "C3"]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's format these row values into rows, and column values into columns. You can use #[cairofmt::skip]
let input: Array<ByteArray> = array![
"ABC",
"123",
];
#[cairofmt::skip]
let expected = array![
"A1",
"B2",
"C3",
]; Note: apply this change to all tests where the formatter didn't already format the values like this |
||||||
|
||||||
let output = transpose_matrix(input); | ||||||
assert_eq!(output, expected); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
#[ignore] | ||||||
fn single_line() { | ||||||
let mut input: Array<ByteArray> = 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<ByteArray> = 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<ByteArray> = 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<ByteArray> = array![ | ||||||
"The longest line.", "A long line.", "A longer line.", "A line." | ||||||
]; | ||||||
let expected = array![ | ||||||
"TAAA", | ||||||
"h ", | ||||||
"elll", | ||||||
" ooi", | ||||||
"lnnn", | ||||||
"ogge", | ||||||
"n e.", | ||||||
"glr ", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not pad to the right, i.e. add padding spaces to the rightmost columns (as per the problem description). Copy/paste the actual expected output from the canonical data |
||||||
"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<ByteArray> = 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<ByteArray> = 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<ByteArray> = 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<ByteArray> = array!["11", "2", "3333", "444", "555555", "66666"]; | ||||||
let expected = array!["123456", "1 3456", " 3456", " 3 56", " 56", " 5 "]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same as before, no padding to the right is allowed (see canonical data) |
||||||
|
||||||
let output = transpose_matrix(input); | ||||||
assert_eq!(output, expected); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already added
word-count
, see https://github.com/exercism/cairo/pull/304/filesDelete these files from the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for this error. I had issues which prompted this error along the line. I'd delete it from the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have deleted these files from the PR. Please, let me know if there are other changes to make.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem at all, it happens!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The files are still in the PR 😅