Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Alphametics | ||
|
||
Write a function to solve alphametics puzzles. | ||
|
||
[Alphametics](https://en.wikipedia.org/wiki/Alphametics) is a puzzle where | ||
letters in words are replaced with numbers. | ||
|
||
For example `SEND + MORE = MONEY`: | ||
|
||
```text | ||
S E N D | ||
M O R E + | ||
----------- | ||
M O N E Y | ||
``` | ||
|
||
Replacing these with valid numbers gives: | ||
|
||
```text | ||
9 5 6 7 | ||
1 0 8 5 + | ||
----------- | ||
1 0 6 5 2 | ||
``` | ||
|
||
This is correct because every letter is replaced by a different number and the | ||
words, translated into numbers, then make a valid sum. | ||
|
||
Each letter must represent a different digit, and the leading digit of | ||
a multi-digit number must not be zero. | ||
|
||
Write a function to solve alphametics puzzles. | ||
|
||
## Implementation | ||
|
||
Define a single Go func, Solve, which accepts a puzzle string which may have zero | ||
or more + operators, and one == operator; Solve should attempt to solve the alphametics puzzle | ||
and return a map of all the letter substitutions for both the puzzle and the addition solution. | ||
|
||
Use the following signature for func Solve: | ||
|
||
``` | ||
func Solve(puzzle string) (map[string]int, error) { | ||
``` | ||
Solve should return an error if there is no solution to the given puzzle. | ||
|
||
An example puzzle and returned solution is: | ||
``` | ||
Solve("SEND + MORE == MONEY") | ||
``` | ||
would return | ||
``` | ||
map[string]int{"M":1, "O":0, "N":6, "E":5, "Y":2, "S":9, "D":7, "R":8}, nil | ||
``` | ||
|
||
```text | ||
S E N D | ||
M O R E + | ||
----------- | ||
M O N E Y | ||
``` | ||
|
||
Replacing these with valid numbers gives: | ||
|
||
```text | ||
9 5 6 7 | ||
1 0 8 5 + | ||
----------- | ||
1 0 6 5 2 | ||
``` | ||
|
||
|
||
|
||
## Coding the solution | ||
|
||
Look for a stub file having the name alphametics.go | ||
and place your solution code in that file. | ||
|
||
## Running the tests | ||
|
||
To run the tests run the command `go test` from within the exercise directory. | ||
|
||
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` | ||
flags: | ||
|
||
go test -v --bench . --benchmem | ||
|
||
Keep in mind that each reviewer will run benchmarks on a different machine, with | ||
different specs, so the results from these benchmark tests may vary. | ||
|
||
## Further information | ||
|
||
For more detailed information about the Go track, including how to get help if | ||
you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources). | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package alphametics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package alphametics | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestSolve(t *testing.T) { | ||
for _, tc := range testCases { | ||
s, err := Solve(tc.input) | ||
switch { | ||
case tc.errorExpected: | ||
if err == nil { | ||
t.Fatalf("FAIL: %s\nSolve(%q)\nExpected error\nActual: %#v", | ||
tc.description, tc.input, s) | ||
} | ||
case err != nil: | ||
t.Fatalf("FAIL: %s\nSolve(%q)\nExpected: %#v\nGot error: %q", | ||
tc.description, tc.input, tc.expected, err) | ||
case !reflect.DeepEqual(s, tc.expected): | ||
t.Fatalf("FAIL: %s\nSolve(%q)\nExpected: %#v\nActual: %#v", | ||
tc.description, tc.input, tc.expected, s) | ||
} | ||
t.Logf("PASS: %s", tc.description) | ||
} | ||
} | ||
|
||
func BenchmarkSolve(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testCases { | ||
Solve(tc.input) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package alphametics | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 361cf3c alphametics: v1.3.0 Add test for two digits carry (#1344) | ||
// Problem Specifications Version: 1.3.0 | ||
|
||
// Solve the alphametics puzzle | ||
var testCases = []struct { | ||
description string | ||
input string | ||
expected map[string]int | ||
errorExpected bool | ||
}{ | ||
{ | ||
description: "puzzle with three letters", | ||
input: "I + BB == ILL", | ||
expected: map[string]int{"B": 9, "I": 1, "L": 0}, | ||
}, | ||
{ | ||
description: "solution must have unique value for each letter", | ||
input: "A == B", | ||
errorExpected: true, | ||
}, | ||
{ | ||
description: "leading zero solution is invalid", | ||
input: "ACA + DD == BD", | ||
errorExpected: true, | ||
}, | ||
{ | ||
description: "puzzle with two digits final carry", | ||
input: "A + A + A + A + A + A + A + A + A + A + A + B == BCC", | ||
expected: map[string]int{"A": 9, "B": 1, "C": 0}, | ||
}, | ||
{ | ||
description: "puzzle with four letters", | ||
input: "AS + A == MOM", | ||
expected: map[string]int{"A": 9, "M": 1, "O": 0, "S": 2}, | ||
}, | ||
{ | ||
description: "puzzle with six letters", | ||
input: "NO + NO + TOO == LATE", | ||
expected: map[string]int{"A": 0, "E": 2, "L": 1, "N": 7, "O": 4, "T": 9}, | ||
}, | ||
{ | ||
description: "puzzle with seven letters", | ||
input: "HE + SEES + THE == LIGHT", | ||
expected: map[string]int{"E": 4, "G": 2, "H": 5, "I": 0, "L": 1, "S": 9, "T": 7}, | ||
}, | ||
{ | ||
description: "puzzle with eight letters", | ||
input: "SEND + MORE == MONEY", | ||
expected: map[string]int{"D": 7, "E": 5, "M": 1, "N": 6, "O": 0, "R": 8, "S": 9, "Y": 2}, | ||
}, | ||
{ | ||
description: "puzzle with ten letters", | ||
input: "AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE", | ||
expected: map[string]int{"A": 5, "D": 3, "E": 4, "F": 7, "G": 8, "N": 0, "O": 2, "R": 1, "S": 6, "T": 9}, | ||
}, | ||
{ | ||
description: "puzzle with ten letters and 199 addends", | ||
input: "THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + ROOFS + FOR + THEIR + SAFE == FORTRESSES", | ||
expected: map[string]int{"A": 1, "E": 0, "F": 5, "H": 8, "I": 7, "L": 2, "O": 6, "R": 3, "S": 4, "T": 9}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module alphametics | ||
|
||
go 1.13 |