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

Wordy Exercise #296

Merged
merged 45 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
45 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
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,14 @@
"prerequisites": [],
"difficulty": 2
},
{
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
"slug": "wordy",
"name": "Wordy",
"uuid": "78d31713-0d88-4821-a586-4b36c46eaa44",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "high-scores",
"name": "High Scores",
Expand Down
59 changes: 59 additions & 0 deletions exercises/practice/wordy/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Instructions

Parse and evaluate simple math word problems returning the answer as an integer.

## Iteration 0 — Numbers

Problems with no operations simply evaluate to the number given.

> What is 5?

Evaluates to 5.

## Iteration 1 — Addition

Add two numbers together.

> What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

## Iteration 2 — Subtraction, Multiplication and Division

Now, perform the other three operations.

> What is 7 minus 5?

2

> What is 6 multiplied by 4?

24

> What is 25 divided by 5?

5

## Iteration 3 — Multiple Operations

Handle a set of operations, in sequence.

Since these are verbal word problems, evaluate the expression from left-to-right, _ignoring the typical order of operations._

> What is 5 plus 13 plus 6?

24

> What is 3 plus 2 multiplied by 3?

15 (i.e. not 9)

## Iteration 4 — Errors

The parser should reject:

- Unsupported operations ("What is 52 cubed?")
- Non-math questions ("Who is the President of the United States")
- Word problems with invalid syntax ("What is 1 plus plus 2?")
22 changes: 22 additions & 0 deletions exercises/practice/wordy/.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/wordy.cairo"
],
"example": [
".meta/example.cairo"
],
"invalidator": [
"Scarb.toml"
]
},
"blurb": "Parse and evaluate simple math word problems returning the answer as an integer.",
"source": "Inspired by one of the generated questions in the Extreme Startup game.",
"source_url": "https://github.com/rchatley/extreme_startup"
}
148 changes: 148 additions & 0 deletions exercises/practice/wordy/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#[derive(Drop, PartialEq)]
enum Operator {
Plus,
Minus,
Multiply,
Divide,
Invalid,
}

pub fn answer(question: ByteArray) -> i32 {
let words: Array<ByteArray> = split_question_into_words(question);
assert!(words.len() >= 3, "syntax error");
assert!(words[0] == @"What" && words[1] == @"is", "unknown operation");

let mut result = match parse_int(words[2]) {
Option::Some(s) => s,
Option::None => panic!("syntax error")
};

let mut i = 3;
while i < words.len() {
let op = parse_operator(words.at(i));
i += 1;

0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
if op == Operator::Multiply || op == Operator::Divide {
assert!(i < words.len() && words[i] == @"by", "unknown operation");
i += 1;
} else if op == Operator::Invalid {
assert!(parse_int(words.at(i - 1)) != Option::None, "unknown operation");
}

let num: i32 = match words.get(i) {
Option::Some(s) => {
match parse_int(s.unbox()) {
Option::Some(s) => s,
Option::None => panic!("syntax error"),
}
},
Option::None => panic!("syntax error"),
};

result = match op {
Operator::Plus => result + num,
Operator::Minus => result - num,
Operator::Multiply => result * num,
Operator::Divide => {
assert!(num != 0, "unknown operation");
result / num
},
Operator::Invalid => panic!("unknown operation"),
};

i += 1;
};

result
}


fn split_question_into_words(question: ByteArray) -> Array<ByteArray> {
let mut words: Array<ByteArray> = ArrayTrait::new();
let mut current_word = "";

let mut i = 0;
while i < question.len() {
let char = question[i];
if char == ' ' {
if current_word.len() > 0 {
words.append(current_word);
current_word = "";
}
} else if char == '?' {
if current_word.len() > 0 {
words.append(current_word);
current_word = "";
}
break;
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
} else {
current_word.append_byte(char);
}
i += 1;
};

words
}


fn parse_int(num: @ByteArray) -> Option<i32> {
let mut result: Option<i32> = Option::Some(0);
let mut size = num.len();
let mut i = 0;

let mut is_signed = false;
if num.at(i).unwrap() == '-' {
is_signed = true;
i += 1;
}

while i < size {
let re = char_to_digit(num[i]);
match re {
Option::Some(v) => {
if let Option::Some(max) = result {
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
result = Option::Some(max * 10 + v.into());
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
}
},
Option::None => {
result = Option::None;
break;
}
}
i += 1;
};

if let Option::Some(val) = result {
if (is_signed) {
result = Option::Some(val * -1)
}
}
result
}

fn parse_operator(word: @ByteArray) -> Operator {
if (word == @"plus") {
Operator::Plus
} else if (word == @"minus") {
Operator::Minus
} else if (word == @"multiplied") {
Operator::Multiply
} else if (word == @"divided") {
Operator::Divide
} else {
Operator::Invalid
}
}


// Utility function to convert a char representing a digit into its numerical value (u32 equivalent)
fn char_to_digit(c: u8) -> Option<u8> {
let zero_ascii = '0';
let nine_ascii = '9';

if c >= zero_ascii && c <= nine_ascii {
Option::Some(c - zero_ascii)
} else {
Option::None // Return None for invalid characters
}
}
79 changes: 79 additions & 0 deletions exercises/practice/wordy/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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.

[88bf4b28-0de3-4883-93c7-db1b14aa806e]
description = "just a number"

[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
description = "addition"

[79e49e06-c5ae-40aa-a352-7a3a01f70015]
description = "more addition"

[b345dbe0-f733-44e1-863c-5ae3568f3803]
description = "addition with negative numbers"

[cd070f39-c4cc-45c4-97fb-1be5e5846f87]
description = "large addition"

[0d86474a-cd93-4649-a4fa-f6109a011191]
description = "subtraction"

[30bc8395-5500-4712-a0cf-1d788a529be5]
description = "multiplication"

[34c36b08-8605-4217-bb57-9a01472c427f]
description = "division"

[da6d2ce4-fb94-4d26-8f5f-b078adad0596]
description = "multiple additions"

[7fd74c50-9911-4597-be09-8de7f2fea2bb]
description = "addition and subtraction"

[b120ffd5-bad6-4e22-81c8-5512e8faf905]
description = "multiple subtraction"

[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae]
description = "subtraction then addition"

[312d908c-f68f-42c9-aa75-961623cc033f]
description = "multiple multiplication"

[38e33587-8940-4cc1-bc28-bfd7e3966276]
description = "addition and multiplication"

[3c854f97-9311-46e8-b574-92b60d17d394]
description = "multiple division"

[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
description = "unknown operation"

[8a7e85a8-9e7b-4d46-868f-6d759f4648f8]
description = "Non math question"

[42d78b5f-dbd7-4cdb-8b30-00f794bb24cf]
description = "reject problem missing an operand"

[c2c3cbfc-1a72-42f2-b597-246e617e66f5]
description = "reject problem with no operands or operators"

[4b3df66d-6ed5-4c95-a0a1-d38891fbdab6]
description = "reject two operations in a row"

[6abd7a50-75b4-4665-aa33-2030fd08bab1]
description = "reject two numbers in a row"

[10a56c22-e0aa-405f-b1d2-c642d9c4c9de]
description = "reject postfix notation"

[0035bc63-ac43-4bb5-ad6d-e8651b7d954e]
description = "reject prefix notation"
7 changes: 7 additions & 0 deletions exercises/practice/wordy/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "wordy"
version = "0.1.0"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.8.2"
3 changes: 3 additions & 0 deletions exercises/practice/wordy/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn answer(question: ByteArray) -> i32 {
panic!("implement `answer`")
}
Loading
Loading