├── .gitignore ├── README.md └── dynamic-programming ├── matrix.go ├── matrix_test.go ├── binomial.go ├── binomial_test.go ├── coins_test.go └── coins.go /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### *a reunion of broken parts pt. 2* 2 | 3 | Diogenes :dog: 4 | -------------------------------------------------------------------------------- /dynamic-programming/matrix.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Matrix struct { 4 | height int 5 | width int 6 | table []float64 7 | } 8 | 9 | func NewMatrix(height, width int) Matrix { 10 | return Matrix{height, width, make([]float64, height*width)} 11 | } 12 | 13 | func (s *Matrix) Set(i, j int, val float64) { 14 | s.table[s.width*i+j] = val 15 | } 16 | 17 | func (s *Matrix) Get(i, j int) float64 { 18 | return s.table[s.width*i+j] 19 | } 20 | -------------------------------------------------------------------------------- /dynamic-programming/matrix_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestMatrix(t *testing.T) { 9 | m := NewMatrix(3, 4) 10 | 11 | m.Set(2, 3, 1.0) 12 | 13 | if m.Get(2, 3) != 1.0 { 14 | t.Fail() 15 | } 16 | 17 | if m.table[11] != 1.0 { 18 | t.Fail() 19 | } 20 | 21 | m.Set(1, 2, 5.0) 22 | 23 | if m.table[6] != 5.0 { 24 | fmt.Println(m.table) 25 | t.Fail() 26 | } 27 | } 28 | 29 | func _() { 30 | fmt.Println("x_x") 31 | } 32 | -------------------------------------------------------------------------------- /dynamic-programming/binomial.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func Binomial(power int) []int { 4 | // ... 5 | lookup := make(map[int]map[int]int) 6 | 7 | lookup[0] = make(map[int]int) 8 | 9 | // ... 10 | lookup[0][0] = 1 11 | 12 | // ... 13 | for i := 1; i < power+1; i++ { 14 | lookup[i] = make(map[int]int) 15 | for j := 0; j <= i; j++ { 16 | lookup[i][j] = lookup[i-1][j-1] + lookup[i-1][j] 17 | } 18 | } 19 | 20 | coefficients := make([]int, power+1) 21 | 22 | for i, _ := range coefficients { 23 | coefficients[i] = lookup[power][i] 24 | } 25 | 26 | // ... 27 | return coefficients 28 | } 29 | -------------------------------------------------------------------------------- /dynamic-programming/binomial_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func arrayEquals(lhs, rhs []int) bool { 8 | if len(lhs) != len(rhs) { 9 | return false 10 | } 11 | 12 | for i := 0; i < len(lhs); i++ { 13 | if lhs[i] != rhs[i] { 14 | return false 15 | } 16 | } 17 | 18 | return true 19 | } 20 | 21 | func TestArrayEquals(t *testing.T) { 22 | if arrayEquals([]int{1, 2, 3}, []int{1, 2, 4}) { 23 | t.Fail() 24 | } 25 | if !arrayEquals([]int{1, 2, 3}, []int{1, 2, 3}) { 26 | t.Fail() 27 | } 28 | } 29 | 30 | func TestBinomial(t *testing.T) { 31 | if !arrayEquals(Binomial(0), []int{1}) { 32 | t.Fail() 33 | } 34 | if !arrayEquals(Binomial(1), []int{1, 1}) { 35 | t.Fail() 36 | } 37 | if !arrayEquals(Binomial(2), []int{1, 2, 1}) { 38 | t.Fail() 39 | } 40 | if !arrayEquals(Binomial(3), []int{1, 3, 3, 1}) { 41 | t.Fail() 42 | } 43 | if !arrayEquals(Binomial(4), []int{1, 4, 6, 4, 1}) { 44 | t.Fail() 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /dynamic-programming/coins_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func xTestCoinCount(t *testing.T) { 9 | c := NewCoinCounter([]int{1}) 10 | d := NewCoinCounter([]int{5, 1}) 11 | e := NewCoinCounter([]int{2, 6}) 12 | f := NewCoinCounter([]int{1, 2, 3}) 13 | 14 | if v := c.Count(1); v != 1 { 15 | t.Fail() 16 | } 17 | 18 | if v := c.Count(2); v != 1 { 19 | t.Fail() 20 | } 21 | 22 | if v := c.Count(7); v != 1 { 23 | t.Fail() 24 | } 25 | 26 | if v := d.Count(5); v != 2 { 27 | t.Fail() 28 | } 29 | 30 | if v := e.Count(6); v != 2 { 31 | t.Fail() 32 | } 33 | 34 | if v := f.Count(5); v != 5 { 35 | fmt.Println(v, "!=", 5) 36 | t.Fail() 37 | } 38 | } 39 | 40 | func TestBroken(t *testing.T) { 41 | c := NewCoinCounter([]int{1, 2, 3}) 42 | d := NewCoinCounter([]int{2, 3, 5, 6}) 43 | 44 | if v := c.Count(5); v != 5 { 45 | fmt.Println("CoinCount(5, [1, 2, 3]) != 5 ~", v) 46 | t.Fail() 47 | } 48 | 49 | if v := c.Count(4); v != 4 { 50 | fmt.Println("CoinCount(4, [1, 2, 3]) != 4 ~", v) 51 | t.Fail() 52 | } 53 | 54 | if v := d.Count(10); v != 5 { 55 | fmt.Println("CoinCount(10, [2, 3, 5, 6]) != 5 ~", v) 56 | t.Fail() 57 | } 58 | } 59 | 60 | func x_x() { 61 | fmt.Println("x_x") 62 | } 63 | -------------------------------------------------------------------------------- /dynamic-programming/coins.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | type CoinCounter struct { 9 | // Table is structured like table[Coin][Value] 10 | Table map[int]map[int]int 11 | // Coins will be sorted 12 | Coins []int 13 | } 14 | 15 | func NewCoinCounter(coins []int) CoinCounter { 16 | sort.Ints(coins) 17 | 18 | table := make(map[int]map[int]int) 19 | table[0] = make(map[int]int) 20 | for _, c := range coins { 21 | table[c] = make(map[int]int) 22 | } 23 | 24 | return CoinCounter{table, coins} 25 | } 26 | 27 | func (self *CoinCounter) Contains(value, coin int) bool { 28 | if _, ok := self.Table[coin]; !ok { 29 | return false 30 | } 31 | 32 | _, ok := self.Table[coin][value] 33 | 34 | return ok 35 | } 36 | 37 | func (self *CoinCounter) Get(value, coin int) (int, bool) { 38 | if _, ok := self.Table[coin]; !ok { 39 | return 0, false 40 | } 41 | 42 | val, ok := self.Table[coin][value] 43 | 44 | return val, ok 45 | } 46 | 47 | func (self *CoinCounter) Set(value, coin, count int) { 48 | if _, ok := self.Table[coin]; ok { 49 | self.Table[coin][value] = count 50 | } 51 | } 52 | 53 | func (self *CoinCounter) Count(amount int) int { 54 | 55 | coins := make([]int, 0) 56 | coins = append(coins, 0) 57 | coins = append(coins, self.Coins...) 58 | 59 | height, width := len(self.Coins)+1, amount+1 60 | 61 | solution := make([][]int, height) 62 | 63 | for i := 0; i < height; i++ { 64 | solution[i] = make([]int, width) 65 | solution[i][0] = 1 66 | } 67 | 68 | for i := 1; i < width; i++ { 69 | solution[0][i] = 0 70 | } 71 | 72 | for i := 1; i <= len(self.Coins); i++ { 73 | for j := 1; j <= amount; j++ { 74 | if coins[i-1] <= j && j-self.Coins[i-1] >= 0 { 75 | solution[i][j] = solution[i-1][j] + solution[i][j-self.Coins[i-1]] 76 | } else { 77 | solution[i][j] = solution[i-1][j] 78 | } 79 | } 80 | } 81 | 82 | return solution[len(self.Coins)][amount] 83 | } 84 | 85 | func main() { 86 | fmt.Println("x_x") 87 | } 88 | --------------------------------------------------------------------------------