32 lines
599 B
Go
32 lines
599 B
Go
|
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
|
||
|
}
|