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

View File

@ -2,7 +2,6 @@ package matrix
import (
"errors"
"fmt"
"strconv"
"strings"
)
@ -42,68 +41,31 @@ func New(in string) (*Matrix, error) {
return &ret, nil
}
// Saddle finds all Saddle Points in the matrix
func (m *Matrix) Saddle() []Pair {
var ret []Pair
// We're going to get a list of coords for row highs
// then compare that to a list of coords for col lows
var testRows []Pair
for i := range m.vals {
testRows = append(testRows, Pair{i, m.findRowHigh(i)})
}
fmt.Println(testRows)
return ret
for j := range m.vals[0] {
v := Pair{m.findColLow(j), j}
// If this pair is in testRows, it's a saddle point
for i := range testRows {
if v[0] == testRows[i][0] && v[1] == testRows[i][1] {
ret = append(ret, v)
for j := range m.vals[i] {
val := m.vals[i][j]
// Check if i,j is a saddle point
// (Highest in row, lowest in col)
colTest, rowTest := true, true
for k := range m.vals {
if m.vals[k][j] < val {
colTest = false
break
}
}
for k := range m.vals[i] {
if m.vals[i][k] > val {
rowTest = false
break
}
}
if colTest && rowTest {
ret = append(ret, Pair{i, j})
}
}
}
return ret
}
// Cols returns the values of the cols in the matrix
func (m *Matrix) Cols() [][]int {
var ret [][]int
for i := range m.vals[0] {
var row []int
for j := range m.vals {
row = append(row, m.vals[j][i])
}
ret = append(ret, row)
}
return ret
}
// findColLow finds the lowest value in a column
func (m *Matrix) findColLow(c int) int {
cols := m.Cols()
var idx int
val := -1
if c >= 0 && c < len(cols) {
for i := range cols[c] {
if cols[c][i] < val || val == -1 {
val = cols[c][i]
idx = i
}
}
}
return idx
}
// findColLow finds the lowest value in a column
func (m *Matrix) findRowHigh(r int) int {
var idx int
val := -1
if r >= 0 && r < len(m.vals) {
for i := range m.vals[r] {
if m.vals[r][i] < val || val == -1 {
val = m.vals[r][i]
idx = i
}
}
}
return idx
}

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)
}
}
}