Skip to content
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

Open
wants to merge 84 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
91d8a54
Added introduction for strings
Ephraim-nonso Jul 10, 2024
e90852c
Added blurb for strings
Ephraim-nonso Jul 10, 2024
ac52fe1
Changed to review
Ephraim-nonso Jul 10, 2024
e865f05
One change
Ephraim-nonso Jul 10, 2024
96d9bca
Change wordings for clarity
Ephraim-nonso Jul 10, 2024
52de5af
Enums Data Type
Ephraim-nonso Jul 14, 2024
74f359c
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 14, 2024
c96e3f4
Update:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
009cf95
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 16, 2024
8a8a8cf
Update:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
978ac07
New:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
70ebe7e
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 16, 2024
8a4a4e7
Reorder sentences in Enum Traits section
Jul 16, 2024
e3544cb
Match basics concept
Ephraim-nonso Jul 30, 2024
49d20ee
Merge branch 'main' into main
Ephraim-nonso Jul 30, 2024
321923e
reorder enum & match basics
Ephraim-nonso Jul 30, 2024
89d0d01
resolve enumn clash
Ephraim-nonso Jul 30, 2024
9456bba
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 30, 2024
d031586
amend to requested changes
Ephraim-nonso Aug 1, 2024
2262bd4
Merge remote-tracking branch 'refs/remotes/origin/main'
Ephraim-nonso Aug 1, 2024
d77ac92
minor changes
Ephraim-nonso Aug 2, 2024
100e118
Merge branch 'exercism:main' into main
Ephraim-nonso Aug 2, 2024
98ddc2c
nit: code update
Ephraim-nonso Aug 6, 2024
df51470
Merge branch 'exercism:main' into main
Ephraim-nonso Aug 6, 2024
ecdd0b6
Apply suggestions from code review
Aug 7, 2024
b8adcde
traits concept
Ephraim-nonso Aug 26, 2024
63bddac
Merge remote-tracking branch 'refs/remotes/origin/main'
Ephraim-nonso Aug 26, 2024
b1a4c1f
Merge branch 'main' into main
Ephraim-nonso Aug 26, 2024
066787c
correction to traits concept
Ephraim-nonso Sep 16, 2024
9ac273e
Merge branch 'exercism:main' into main
Ephraim-nonso Sep 16, 2024
0727ecc
MD047 lint: fixed
Ephraim-nonso Sep 17, 2024
69dab2f
few correction on traits
Ephraim-nonso Sep 20, 2024
3b851a6
Merge branch 'exercism:main' into main
Ephraim-nonso Sep 25, 2024
eca178d
wordy exercise
Ephraim-nonso Oct 14, 2024
35e9e18
pass checks
Ephraim-nonso Oct 14, 2024
2b54929
Merge branch 'main' into main
Ephraim-nonso Oct 14, 2024
26ff5d6
Wordy: update fmt
Ephraim-nonso Oct 17, 2024
925e6af
Wordy: update fmt
Ephraim-nonso Oct 17, 2024
008fd0e
wordy: formatted
Ephraim-nonso Oct 17, 2024
281fd43
wordy: formatted
Ephraim-nonso Oct 17, 2024
9f03b7f
wordy: formatted
Ephraim-nonso Oct 17, 2024
df78767
wordy: error handling.
Ephraim-nonso Oct 18, 2024
2470417
Update exercises/practice/wordy/src/lib.cairo
0xNeshi Oct 18, 2024
0c7ed6b
Update difficulty to 4
0xNeshi Oct 18, 2024
3219156
rename max->num in parse_int
0xNeshi Oct 18, 2024
4a876f0
Exercises: Word count
Ephraim-nonso Nov 7, 2024
62f50c7
Exercises: Word count
Ephraim-nonso Nov 7, 2024
75d5220
Merge branch 'main' into main
Ephraim-nonso Nov 7, 2024
deb3b7f
word count:changes requested
Ephraim-nonso Nov 11, 2024
7abdaab
word-count: new changes
Ephraim-nonso Nov 11, 2024
4d8892c
word-count: new changes
Ephraim-nonso Nov 11, 2024
77b5c4e
word-count: new changes
Ephraim-nonso Nov 11, 2024
2b7d363
word-count: new changes
Ephraim-nonso Nov 11, 2024
12fae98
word-count: new changes
Ephraim-nonso Nov 11, 2024
ac4b1ab
word-count: new changes
Ephraim-nonso Nov 11, 2024
44213ae
redesign the split function
Ephraim-nonso Nov 15, 2024
4dd9516
build passes
Ephraim-nonso Nov 16, 2024
8ae1404
Merge branch 'main' into main
Ephraim-nonso Nov 16, 2024
435ff6a
remove dup and update
Ephraim-nonso Nov 17, 2024
05a2367
merge new
Ephraim-nonso Nov 17, 2024
401954a
formatted: new
Ephraim-nonso Nov 17, 2024
cdbd0fe
remove dup
Ephraim-nonso Nov 17, 2024
2ef1886
Merge branch 'exercism:main' into main
Ephraim-nonso Nov 17, 2024
5f31d61
assert_unordered->assert_unordered_eq + verify both are subsets of ea…
0xNeshi Nov 18, 2024
55de0ef
Check if prev. and next are alphanumeric
0xNeshi Nov 18, 2024
9947334
transpose exercise
Ephraim-nonso Dec 9, 2024
3b6f7c6
transpose exercise
Ephraim-nonso Dec 9, 2024
86c7638
Wordy: update fmt
Ephraim-nonso Oct 17, 2024
8e6ff95
resolve lost work
Ephraim-nonso Dec 9, 2024
068a20b
resolve issues
Ephraim-nonso Dec 9, 2024
1bc2263
resolve issues in config
Ephraim-nonso Dec 9, 2024
3c90f2d
format config
Ephraim-nonso Dec 9, 2024
0df06b2
make test passes
Ephraim-nonso Dec 9, 2024
1f63916
make test passes
Ephraim-nonso Dec 9, 2024
4cd5596
Merge branch 'main' of https://github.com/Ephraim-nonso/cairo
Ephraim-nonso Dec 9, 2024
13dfe94
Sync scarb version
0xNeshi Dec 16, 2024
1551365
Delete exercises/practice/word-count/tests/word_count.cairo
Ephraim-nonso Dec 20, 2024
f497c86
Delete exercises/practice/word-count/src/lib.cairo
Ephraim-nonso Dec 20, 2024
5ed367c
Delete exercises/practice/word-count/Scarb.toml
Ephraim-nonso Dec 20, 2024
c32d87f
Delete exercises/practice/word-count/.meta/tests.toml
Ephraim-nonso Dec 20, 2024
d4e956e
Delete exercises/practice/word-count/.meta/example.cairo
Ephraim-nonso Dec 20, 2024
a3f3857
Delete exercises/practice/word-count/.docs/instructions.md
Ephraim-nonso Dec 20, 2024
df62909
Delete exercises/practice/word-count/.meta/config.json
Ephraim-nonso Dec 20, 2024
3de1993
Delete exercises/practice/word-count/.docs/introduction.md
Ephraim-nonso Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "word-count",
Copy link
Contributor

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/files
Delete these files from the PR.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor

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 😅

"name": "Word Count",
"uuid": "5fded933-439a-4faa-bfb6-18ec7b7c8469",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
Expand Down Expand Up @@ -852,6 +860,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "transpose",
"name": "Transpose",
"uuid": "59b5d30b-62c1-4465-a44d-485c45f4aac2",
"practices": [],
"prerequisites": [],
"difficulty": 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"difficulty": 1
"difficulty": 4

It's not a trivial exercise, let's increase the difficulty

},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
Expand Down
61 changes: 61 additions & 0 deletions exercises/practice/transpose/.docs/instructions.md
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
22 changes: 22 additions & 0 deletions exercises/practice/transpose/.meta/config.json
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/"
}
35 changes: 35 additions & 0 deletions exercises/practice/transpose/.meta/example.cairo
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must be possible to avoid having a loop + for + break + continue combination to perform this operation, makes it very hard to follow what is going on.

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
}
46 changes: 46 additions & 0 deletions exercises/practice/transpose/.meta/tests.toml
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"
7 changes: 7 additions & 0 deletions exercises/practice/transpose/Scarb.toml
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
3 changes: 3 additions & 0 deletions exercises/practice/transpose/src/lib.cairo
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`")
}
174 changes: 174 additions & 0 deletions exercises/practice/transpose/tests/transpose.cairo
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![];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut input: Array<ByteArray> = array![];
let input: Array<ByteArray> = array![];

no need for input to be mutable, address this in all test cases

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"];
Copy link
Contributor

Choose a reason for hiding this comment

The 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] to tell the formatter to not touch this then.
This is similar to what we've done for minesweeper.

#[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 ",
Copy link
Contributor

Choose a reason for hiding this comment

The 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 "];
Copy link
Contributor

@0xNeshi 0xNeshi Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let expected = array!["123456", "1 3456", " 3456", " 3 56", " 56", " 5 "];
let expected = array!["123456", "1 3456", " 3456", " 3 56", " 56", " 5"];

Same as before, no padding to the right is allowed (see canonical data)


let output = transpose_matrix(input);
assert_eq!(output, expected);
}
Loading
Loading