Skip to content

Commit 92ef74b

Browse files
authored
Sync alphametics (#846)
[no important files changed]
1 parent be28b27 commit 92ef74b

File tree

5 files changed

+102
-65
lines changed

5 files changed

+102
-65
lines changed

exercises/practice/alphametics/.docs/instructions.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Instructions
22

3-
Write a function to solve alphametics puzzles.
3+
Given an alphametics puzzle, find the correct solution.
44

55
[Alphametics][alphametics] is a puzzle where letters in words are replaced with numbers.
66

@@ -26,6 +26,4 @@ This is correct because every letter is replaced by a different number and the w
2626

2727
Each letter must represent a different digit, and the leading digit of a multi-digit number must not be zero.
2828

29-
Write a function to solve alphametics puzzles.
30-
3129
[alphametics]: https://en.wikipedia.org/wiki/Alphametics

exercises/practice/alphametics/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
".meta/example.php"
1414
]
1515
},
16-
"blurb": "Write a function to solve alphametics puzzles."
16+
"blurb": "Given an alphametics puzzle, find the correct solution."
1717
}

exercises/practice/alphametics/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
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
class Alphametics
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.
11+
12+
[e0c08b07-9028-4d5f-91e1-d178fead8e1a]
13+
description = "puzzle with three letters"
14+
15+
[a504ee41-cb92-4ec2-9f11-c37e95ab3f25]
16+
description = "solution must have unique value for each letter"
17+
18+
[4e3b81d2-be7b-4c5c-9a80-cd72bc6d465a]
19+
description = "leading zero solution is invalid"
20+
21+
[8a3e3168-d1ee-4df7-94c7-b9c54845ac3a]
22+
description = "puzzle with two digits final carry"
23+
24+
[a9630645-15bd-48b6-a61e-d85c4021cc09]
25+
description = "puzzle with four letters"
26+
27+
[3d905a86-5a52-4e4e-bf80-8951535791bd]
28+
description = "puzzle with six letters"
29+
30+
[4febca56-e7b7-4789-97b9-530d09ba95f0]
31+
description = "puzzle with seven letters"
32+
33+
[12125a75-7284-4f9a-a5fa-191471e0d44f]
34+
description = "puzzle with eight letters"
35+
36+
[fb05955f-38dc-477a-a0b6-5ef78969fffa]
37+
description = "puzzle with ten letters"
38+
39+
[9a101e81-9216-472b-b458-b513a7adacf7]
40+
description = "puzzle with ten letters and 199 addends"

exercises/practice/alphametics/AlphameticsTest.php

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,107 @@
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
class AlphameticsTest extends PHPUnit\Framework\TestCase
286
{
29-
private Alphametics $alphametics;
30-
317
public static function setUpBeforeClass(): void
328
{
339
require_once 'Alphametics.php';
3410
}
3511

36-
public function setUp(): void
37-
{
38-
$this->alphametics = new Alphametics();
39-
}
40-
12+
/**
13+
* uuid e0c08b07-9028-4d5f-91e1-d178fead8e1a
14+
* @testdox puzzle with three letters
15+
*/
4116
public function testSolveThreeLetterPuzzle(): void
4217
{
43-
$this->assertEquals(['I' => 1, 'B' => 9, 'L' => 0], $this->alphametics->solve('I + BB == ILL'));
18+
$alphametics = new Alphametics();
19+
$this->assertEquals(['I' => 1, 'B' => 9, 'L' => 0], $alphametics->solve('I + BB == ILL'));
4420
}
4521

22+
/**
23+
* uuid a504ee41-cb92-4ec2-9f11-c37e95ab3f25
24+
* @testdox solution must have unique value for each letter
25+
*/
4626
public function testSolutionsMustHaveUniqueValuesForLetters(): void
4727
{
48-
$this->assertEquals(null, $this->alphametics->solve('A == B'));
28+
$alphametics = new Alphametics();
29+
$this->assertEquals(null, $alphametics->solve('A == B'));
4930
}
5031

32+
/**
33+
* uuid 4e3b81d2-be7b-4c5c-9a80-cd72bc6d465a
34+
* @testdox leading zero solution is invalid
35+
*/
5136
public function testLeadingZerosAreInvalid(): void
5237
{
53-
$this->assertEquals(null, $this->alphametics->solve('ACA + DD == BD'));
38+
$alphametics = new Alphametics();
39+
$this->assertEquals(null, $alphametics->solve('ACA + DD == BD'));
5440
}
5541

42+
/**
43+
* uuid 8a3e3168-d1ee-4df7-94c7-b9c54845ac3a
44+
* @testdox puzzle with two digits final carry
45+
*/
5646
public function testPuzzleWithTwoDigitsFinalCarry(): void
5747
{
58-
$result = $this->alphametics->solve('A + A + A + A + A + A + A + A + A + A + A + B == BCC');
48+
$alphametics = new Alphametics();
49+
$result = $alphametics->solve('A + A + A + A + A + A + A + A + A + A + A + B == BCC');
5950
$this->assertEquals(['A' => 9, 'B' => 1, 'C' => 0], $result);
6051
}
6152

53+
/**
54+
* uuid a9630645-15bd-48b6-a61e-d85c4021cc09
55+
* @testdox puzzle with four letters
56+
*/
6257
public function testPuzzleWithFourLetters(): void
6358
{
64-
$result = $this->alphametics->solve('AS + A == MOM');
59+
$alphametics = new Alphametics();
60+
$result = $alphametics->solve('AS + A == MOM');
6561
$this->assertEquals(['A' => 9, 'S' => 2, 'M' => 1, 'O' => 0], $result);
6662
}
6763

64+
/**
65+
* uuid 3d905a86-5a52-4e4e-bf80-8951535791bd
66+
* @testdox puzzle with six letters
67+
*/
6868
public function testPuzzleWithSixLetters(): void
6969
{
70-
$result = $this->alphametics->solve('NO + NO + TOO == LATE');
70+
$alphametics = new Alphametics();
71+
$result = $alphametics->solve('NO + NO + TOO == LATE');
7172
$this->assertEquals(['N' => 7, 'O' => 4, 'T' => 9, 'L' => 1, 'A' => 0, 'E' => 2], $result);
7273
}
7374

75+
/**
76+
* uuid 4febca56-e7b7-4789-97b9-530d09ba95f0
77+
* @testdox puzzle with seven letters
78+
*/
7479
public function testPuzzleWithSevenLetter(): void
7580
{
76-
$result = $this->alphametics->solve('HE + SEES + THE == LIGHT');
81+
$alphametics = new Alphametics();
82+
$result = $alphametics->solve('HE + SEES + THE == LIGHT');
7783
$this->assertEquals(['E' => 4, 'G' => 2, 'H' => 5, 'I' => 0, 'L' => 1, 'S' => 9, 'T' => 7], $result);
7884
}
7985

86+
/**
87+
* uuid 12125a75-7284-4f9a-a5fa-191471e0d44f
88+
* @testdox puzzle with eight letters
89+
*/
8090
public function testPuzzleWithEightLetters(): void
8191
{
82-
$result = $this->alphametics->solve('SEND + MORE == MONEY');
92+
$alphametics = new Alphametics();
93+
$result = $alphametics->solve('SEND + MORE == MONEY');
8394
$this->assertEquals(['S' => 9, 'E' => 5, 'N' => 6, 'D' => 7, 'M' => 1, 'O' => 0, 'R' => 8, 'Y' => 2], $result);
8495
}
8596

97+
/**
98+
* uuid fb05955f-38dc-477a-a0b6-5ef78969fffa
99+
* @testdox puzzle with ten letters
100+
*/
86101
public function testPuzzleWithTenLetters(): void
87102
{
88-
$result = $this->alphametics->solve('AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE');
103+
$alphametics = new Alphametics();
104+
$result = $alphametics->solve('AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE');
89105
$this->assertEquals([
90106
'A' => 5,
91107
'D' => 3,
@@ -100,8 +116,13 @@ public function testPuzzleWithTenLetters(): void
100116
], $result);
101117
}
102118

119+
/**
120+
* uuid 9a101e81-9216-472b-b458-b513a7adacf7
121+
* @testdox puzzle with ten letters and 199 addends
122+
*/
103123
public function testPuzzleWithTenLettersAnd199Addends(): void
104124
{
125+
$alphametics = new Alphametics();
105126
$puzzle = 'THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS' .
106127
' + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST' .
107128
' + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE' .
@@ -117,7 +138,7 @@ public function testPuzzleWithTenLettersAnd199Addends(): void
117138
' + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE' .
118139
' + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A' .
119140
' + ROOFS + FOR + THEIR + SAFE == FORTRESSES';
120-
$result = $this->alphametics->solve($puzzle);
141+
$result = $alphametics->solve($puzzle);
121142
$this->assertEquals([
122143
'A' => 1,
123144
'E' => 0,

0 commit comments

Comments
 (0)