mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
31: coin sums
This commit is contained in:
20
31_coin_sums/main.go
Normal file
20
31_coin_sums/main.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var coins = []int{1, 2, 5, 10, 20, 50, 100, 200}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sumCoins(200))
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumCoins(target int) int {
|
||||||
|
t := make([]int, target+1)
|
||||||
|
t[0] = 1
|
||||||
|
for _, c := range coins {
|
||||||
|
for j := c; j <= target; j++ {
|
||||||
|
t[j] += t[j-c]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t[target]
|
||||||
|
}
|
||||||
42
31_coin_sums/main_test.go
Normal file
42
31_coin_sums/main_test.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_sumCoins(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
target int
|
||||||
|
}
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
args{1},
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2",
|
||||||
|
args{2},
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"3",
|
||||||
|
args{3},
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"4",
|
||||||
|
args{4},
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if got := sumCoins(tc.args.target); got != tc.want {
|
||||||
|
t.Errorf("sumCoins() = %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user