diff --git a/config.json b/config.json index d990286..c0df134 100644 --- a/config.json +++ b/config.json @@ -819,6 +819,14 @@ "prerequisites": [], "difficulty": 6 }, + { + "slug": "wordy", + "name": "Wordy", + "uuid": "4fc10c1f-e3b9-4772-9720-6cb3f3413eb7", + "practices": [], + "prerequisites": [], + "difficulty": 6 + }, { "slug": "book-store", "name": "Book Store", diff --git a/exercises/practice/wordy/.docs/instructions.md b/exercises/practice/wordy/.docs/instructions.md new file mode 100644 index 0000000..aafb9ee --- /dev/null +++ b/exercises/practice/wordy/.docs/instructions.md @@ -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?") diff --git a/exercises/practice/wordy/.meta/config.json b/exercises/practice/wordy/.meta/config.json new file mode 100644 index 0000000..9cbccaf --- /dev/null +++ b/exercises/practice/wordy/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "source/wordy.d" + ], + "test": [ + "source/wordy.d" + ], + "example": [ + "example/wordy.d" + ] + }, + "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" +} diff --git a/exercises/practice/wordy/.meta/tests.toml b/exercises/practice/wordy/.meta/tests.toml new file mode 100644 index 0000000..a0a83ed --- /dev/null +++ b/exercises/practice/wordy/.meta/tests.toml @@ -0,0 +1,91 @@ +# 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" + +[18983214-1dfc-4ebd-ac77-c110dde699ce] +description = "just a zero" + +[607c08ee-2241-4288-916d-dae5455c87e6] +description = "just a negative number" + +[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0] +description = "addition" + +[bb9f2082-171c-46ad-ad4e-c3f72087c1b5] +description = "addition with a left hand zero" + +[6fa05f17-405a-4742-80ae-5d1a8edb0d5d] +description = "addition with a right hand zero" + +[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" diff --git a/exercises/practice/wordy/dub.sdl b/exercises/practice/wordy/dub.sdl new file mode 100644 index 0000000..2a489ec --- /dev/null +++ b/exercises/practice/wordy/dub.sdl @@ -0,0 +1,2 @@ +name "wordy" +buildRequirements "disallowDeprecations" diff --git a/exercises/practice/wordy/example/wordy.d b/exercises/practice/wordy/example/wordy.d new file mode 100644 index 0000000..5f3c9a5 --- /dev/null +++ b/exercises/practice/wordy/example/wordy.d @@ -0,0 +1,82 @@ +module wordy; + +import std.array; +import std.conv; + +pure long answer(immutable string question) +{ + if (question.length == 0 || question[$ - 1] != '?') + { + throw new Exception("syntax error"); + } + + auto words = question[0 .. $ - 1].split(); + + if (words.length < 3 || words[0] != "What" || words[1] != "is") + { + throw new Exception("unsupported question"); + } + + long first = to!long(words[2]); + char operation = '+'; + + size_t index = 3; + while (index + 1 < words.length) + { + if (words[index] == "plus") + { + operation = '+'; + } + else if (words[index] == "minus") + { + operation = '-'; + } + else if (words[index] == "multiplied" && words[index + 1] == "by") + { + operation = '*'; + index++; + } + else if (words[index] == "divided" && words[index + 1] == "by") + { + operation = '/'; + index++; + } + else + { + throw new Exception("unsupported question"); + } + + index++; + if (index == words.length) + { + throw new Exception("syntax error"); + } + + long second = to!long(words[index]); + index++; + + if (operation == '+') + { + first += second; + } + else if (operation == '-') + { + first -= second; + } + else if (operation == '*') + { + first *= second; + } + else if (operation == '/') + { + first /= second; + } + } + + if (index != words.length) + { + throw new Exception("syntax error"); + } + + return first; +} diff --git a/exercises/practice/wordy/source/wordy.d b/exercises/practice/wordy/source/wordy.d new file mode 100644 index 0000000..61ca263 --- /dev/null +++ b/exercises/practice/wordy/source/wordy.d @@ -0,0 +1,190 @@ +module wordy; + +pure long answer(immutable string question) +{ + // implement this function +} + +unittest +{ + import std.exception : assertThrown; + + immutable int allTestsEnabled = 0; + + // Just a number + { + immutable question = "What is 5?"; + assert(answer(question) == 5); + } + + static if (allTestsEnabled) + { + // Just a zero + { + immutable question = "What is 0?"; + assert(answer(question) == 0); + } + + // Just a negative number + { + immutable question = "What is -123?"; + assert(answer(question) == -123); + } + + // Addition + { + immutable question = "What is 1 plus 1?"; + assert(answer(question) == 2); + } + + // Addition with a left hand zero + { + immutable question = "What is 0 plus 2?"; + assert(answer(question) == 2); + } + + // Addition with a right hand zero + { + immutable question = "What is 3 plus 0?"; + assert(answer(question) == 3); + } + + // More addition + { + immutable question = "What is 53 plus 2?"; + assert(answer(question) == 55); + } + + // Addition with negative numbers + { + immutable question = "What is -1 plus -10?"; + assert(answer(question) == -11); + } + + // Large addition + { + immutable question = "What is 123 plus 45678?"; + assert(answer(question) == 45801); + } + + // Subtraction + { + immutable question = "What is 4 minus -12?"; + assert(answer(question) == 16); + } + + // Multiplication + { + immutable question = "What is -3 multiplied by 25?"; + assert(answer(question) == -75); + } + + // Division + { + immutable question = "What is 33 divided by -3?"; + assert(answer(question) == -11); + } + + // Multiple additions + { + immutable question = "What is 1 plus 1 plus 1?"; + assert(answer(question) == 3); + } + + // Addition and subtraction + { + immutable question = "What is 1 plus 5 minus -2?"; + assert(answer(question) == 8); + } + + // Multiple subtraction + { + immutable question = "What is 20 minus 4 minus 13?"; + assert(answer(question) == 3); + } + + // Subtraction then addition + { + immutable question = "What is 17 minus 6 plus 3?"; + assert(answer(question) == 14); + } + + // Multiple multiplication + { + immutable question = "What is 2 multiplied by -2 multiplied by 3?"; + assert(answer(question) == -12); + } + + // Addition and multiplication + { + immutable question = "What is -3 plus 7 multiplied by -2?"; + assert(answer(question) == -8); + } + + // Multiple division + { + immutable question = "What is -12 divided by 2 divided by -3?"; + assert(answer(question) == 2); + } + + // Unknown operation + { + immutable question = "What is 52 cubed?"; + assertThrown(answer(question)); + } + + // Non math question + { + immutable question = "Who is the President of the United States?"; + assertThrown(answer(question)); + } + + // Reject problem missing an operand + { + immutable question = "What is 1 plus?"; + assertThrown(answer(question)); + } + + // Reject problem with no operands or operators + { + immutable question = "What is?"; + assertThrown(answer(question)); + } + + // Reject two operations in a row + { + immutable question = "What is 1 plus plus 2?"; + assertThrown(answer(question)); + } + + // Reject two numbers in a row + { + immutable question = "What is 1 plus 2 1?"; + assertThrown(answer(question)); + } + + // Reject postfix notation + { + immutable question = "What is 1 2 plus?"; + assertThrown(answer(question)); + } + + // Reject prefix notation + { + immutable question = "What is plus 1 2?"; + assertThrown(answer(question)); + } + + // Large number multiplication and addition + { + immutable question = "What is 342668567865 multiplied by 348 plus 6576456942334?"; + assert(answer(question) == 125825118559354L); + } + + // Large number division and subtraction + { + immutable question = "What is 6548074074001254 divided by 654 minus 9876543210001?"; + assert(answer(question) == 135802468900L); + } + } +}