Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions bin/auto-sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ strain
sublist
two-bucket
two-fer
wordy
yacht
zebra-puzzle
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@
"uuid": "85408bdd-3c22-4b5a-9c61-044ddfb0c3ac",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"difficulty": 5,
"topics": [
"parsing",
"strings",
Expand Down
19 changes: 5 additions & 14 deletions exercises/practice/wordy/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,20 @@ Now, perform the other three 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._
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)
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?")

## Bonus — Exponentials

If you'd like, handle exponentials.

> What is 2 raised to the 5th power?

32
- 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?")
62 changes: 22 additions & 40 deletions exercises/practice/wordy/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function calculate($question = "")
{
preg_match(
"/What is (-?\d+) (plus|minus|multiplied by|divided by) "
. "(-?\d+)(?: (plus|minus|multiplied by|divided by) (-?\d+))?\?/",
"/What is (-?\d+)(?: (plus|minus|multiplied by|divided by) "
. "(-?\d+)(?: (plus|minus|multiplied by|divided by) (-?\d+))?)?\?/",
$question,
$matches
);

if (empty($matches[2])) {
if (!isset($matches[1])) {
throw new InvalidArgumentException();
}

switch ($matches[2]) {
case 'plus':
$number = $matches[1] + $matches[3];
break;
case 'minus':
$number = $matches[1] - $matches[3];
break;
case 'multiplied by':
$number = $matches[1] * $matches[3];
break;
case 'divided by':
$number = $matches[1] / $matches[3];
break;
default:
$number = 0;
$number = $matches[1];

if (isset($matches[2]) && isset($matches[3])) {
switch ($matches[2]) {
case 'plus':
$number = $matches[1] + $matches[3];
break;
case 'minus':
$number = $matches[1] - $matches[3];
break;
case 'multiplied by':
$number = $matches[1] * $matches[3];
break;
case 'divided by':
$number = $matches[1] / $matches[3];
break;
default:
throw new InvalidArgumentException();
}
}

if (isset($matches[4]) && isset($matches[5])) {
Expand Down
25 changes: 22 additions & 3 deletions exercises/practice/wordy/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# 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"

Expand Down
Loading