Skip to content

Commit 62ab17e

Browse files
authored
Sync wordy (#932)
1 parent 7a5fa23 commit 62ab17e

File tree

6 files changed

+174
-81
lines changed

6 files changed

+174
-81
lines changed

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ strain
7171
sublist
7272
two-bucket
7373
two-fer
74+
wordy
7475
yacht
7576
zebra-puzzle

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@
784784
"uuid": "85408bdd-3c22-4b5a-9c61-044ddfb0c3ac",
785785
"practices": [],
786786
"prerequisites": [],
787-
"difficulty": 1,
787+
"difficulty": 5,
788788
"topics": [
789789
"parsing",
790790
"strings",

exercises/practice/wordy/.docs/instructions.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,20 @@ Now, perform the other three operations.
4040

4141
Handle a set of operations, in sequence.
4242

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

4645
> What is 5 plus 13 plus 6?
4746
4847
24
4948

5049
> What is 3 plus 2 multiplied by 3?
5150
52-
15 (i.e. not 9)
51+
15 (i.e. not 9)
5352

5453
## Iteration 4 — Errors
5554

5655
The parser should reject:
5756

58-
* Unsupported operations ("What is 52 cubed?")
59-
* Non-math questions ("Who is the President of the United States")
60-
* Word problems with invalid syntax ("What is 1 plus plus 2?")
61-
62-
## Bonus — Exponentials
63-
64-
If you'd like, handle exponentials.
65-
66-
> What is 2 raised to the 5th power?
67-
68-
32
57+
- Unsupported operations ("What is 52 cubed?")
58+
- Non-math questions ("Who is the President of the United States")
59+
- Word problems with invalid syntax ("What is 1 plus plus 2?")

exercises/practice/wordy/.meta/example.php

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function calculate($question = "")
286
{
297
preg_match(
30-
"/What is (-?\d+) (plus|minus|multiplied by|divided by) "
31-
. "(-?\d+)(?: (plus|minus|multiplied by|divided by) (-?\d+))?\?/",
8+
"/What is (-?\d+)(?: (plus|minus|multiplied by|divided by) "
9+
. "(-?\d+)(?: (plus|minus|multiplied by|divided by) (-?\d+))?)?\?/",
3210
$question,
3311
$matches
3412
);
3513

36-
if (empty($matches[2])) {
14+
if (!isset($matches[1])) {
3715
throw new InvalidArgumentException();
3816
}
3917

40-
switch ($matches[2]) {
41-
case 'plus':
42-
$number = $matches[1] + $matches[3];
43-
break;
44-
case 'minus':
45-
$number = $matches[1] - $matches[3];
46-
break;
47-
case 'multiplied by':
48-
$number = $matches[1] * $matches[3];
49-
break;
50-
case 'divided by':
51-
$number = $matches[1] / $matches[3];
52-
break;
53-
default:
54-
$number = 0;
18+
$number = $matches[1];
19+
20+
if (isset($matches[2]) && isset($matches[3])) {
21+
switch ($matches[2]) {
22+
case 'plus':
23+
$number = $matches[1] + $matches[3];
24+
break;
25+
case 'minus':
26+
$number = $matches[1] - $matches[3];
27+
break;
28+
case 'multiplied by':
29+
$number = $matches[1] * $matches[3];
30+
break;
31+
case 'divided by':
32+
$number = $matches[1] / $matches[3];
33+
break;
34+
default:
35+
throw new InvalidArgumentException();
36+
}
5537
}
5638

5739
if (isset($matches[4]) && isset($matches[5])) {

exercises/practice/wordy/.meta/tests.toml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

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

15+
[18983214-1dfc-4ebd-ac77-c110dde699ce]
16+
description = "just a zero"
17+
18+
[607c08ee-2241-4288-916d-dae5455c87e6]
19+
description = "just a negative number"
20+
821
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
922
description = "addition"
1023

24+
[bb9f2082-171c-46ad-ad4e-c3f72087c1b5]
25+
description = "addition with a left hand zero"
26+
27+
[6fa05f17-405a-4742-80ae-5d1a8edb0d5d]
28+
description = "addition with a right hand zero"
29+
1130
[79e49e06-c5ae-40aa-a352-7a3a01f70015]
1231
description = "more addition"
1332

0 commit comments

Comments
 (0)