This commit is contained in:
2016-09-06 11:51:22 -05:00
parent 4c0197f8c6
commit 1572f99a57
4 changed files with 133 additions and 58 deletions

39
go/trinary/README.md Normal file
View File

@@ -0,0 +1,39 @@
# Trinary
Write a program that will convert a trinary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles.
The program should consider strings specifying an invalid trinary as the
value 0.
Trinary numbers contain three symbols: 0, 1, and 2.
The last place in a trinary number is the 1's place. The second to last
is the 3's place, the third to last is the 9's place, etc.
```bash
# "102012"
1 0 2 0 1 2 # the number
1*3^5 + 0*3^4 + 2*3^3 + 0*3^2 + 1*3^1 + 2*3^0 # the value
243 + 0 + 54 + 0 + 3 + 2 = 302
```
If your language provides a method in the standard library to perform the
conversion, pretend it doesn't exist and implement it yourself.
To run the tests simply run the command `go test` in the exercise directory.
If the test suite contains benchmarks, you can run these with the `-bench`
flag:
go test -bench .
For more detailed info about the Go track see the [help
page](http://exercism.io/languages/go).
## Source
All of Computer Science [http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-)
## Submitting Incomplete Problems
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

31
go/trinary/trinary.go Normal file
View File

@@ -0,0 +1,31 @@
package trinary
import (
"errors"
"fmt"
"math"
)
// ParseTrinary takes a trinary string and returns the
// decimal value
func ParseTrinary(in string) (int64, error) {
fmt.Println("=== New Parse (" + in + ") ===")
var ret int
place := len(in)
for i := range in {
fmt.Print("Finding ", (place - i), " place value ", string(in[i]))
switch in[i] {
case '0':
case '1':
ret = ret + ((place - i) * 3)
case '2':
ret = ret + ((place - i) * int(math.Pow(3, 2)))
default:
return 0, errors.New("Invalid String")
}
fmt.Println("==", ret)
place--
}
return int64(ret), nil
}

View File

@@ -0,0 +1,43 @@
package trinary
import "testing"
var tests = []struct {
arg string
want int64
ok bool
}{
{"0", 0, true},
{"1", 1, true},
{"2", 2, true},
{"10", 3, true},
{"201", 19, true},
{"0201", 19, true},
{"0000000000000000000000000000000000000000201", 19, true},
{"2021110011022210012102010021220101220221", 9223372036854775807, true},
{"2021110011022210012102010021220101220222", 0, false},
}
func TestParseTrinary(t *testing.T) {
for _, test := range tests {
switch res, err := ParseTrinary(test.arg); {
case err != nil:
if test.ok {
t.Errorf("ParseTrinary(%q) returned error %q, Error not expected",
test.arg, err)
}
case !test.ok:
t.Errorf("ParseTrinary(%q) = %d, %v, expected error", test.arg, res, err)
case res != test.want:
t.Errorf("ParseTrinary(%q) = %d, want %d", test.arg, res, test.want)
}
}
}
func BenchmarkParseTrinary(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
ParseTrinary(test.arg)
}
}
}