Skip to content

Commit

Permalink
Merge feca005 into b36b669
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jan 9, 2019
2 parents b36b669 + feca005 commit 38f3501
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ problems from
* [Day 135](https://github.com/vaskoz/dailycodingproblem-go/issues/280)
* [Day 136](https://github.com/vaskoz/dailycodingproblem-go/issues/282)
* [Day 137](https://github.com/vaskoz/dailycodingproblem-go/issues/285)
* [Day 138](https://github.com/vaskoz/dailycodingproblem-go/issues/286)
* [Day 139](https://github.com/vaskoz/dailycodingproblem-go/issues/288)
33 changes: 33 additions & 0 deletions day138/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day138

import "errors"

var errNotPossible = errors.New("Can not make this value")

// ErrNotPossible represents that making this currency with these
// denominations is impossible.
func ErrNotPossible() error {
return errNotPossible
}

// MinCurrencyRequiredBrute returns the minimum number of currency
// necessary to make the desired amount.
func MinCurrencyRequiredBrute(amt int, denomCents []int) (int, error) {
if amt < 0 {
return 0, errNotPossible
} else if amt == 0 {
return 0, nil
}
min := int(^uint(0) >> 1)
found := false
for _, denom := range denomCents {
if count, err := MinCurrencyRequiredBrute(amt-denom, denomCents); err == nil && count+1 < min {
found = true
min = count + 1
}
}
if found {
return min, nil
}
return 0, errNotPossible
}
30 changes: 30 additions & 0 deletions day138/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day138

import "testing"

var testcases = []struct {
denom []int
n int
expected int
err error
}{
{[]int{1, 5, 10, 25}, 16, 3, nil},
{[]int{25}, 16, 0, ErrNotPossible()},
}

func TestMinCurrencyRequiredBrute(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result, err := MinCurrencyRequiredBrute(tc.n, tc.denom); err != tc.err || result != tc.expected {
t.Errorf("Expected (%v,%v) got (%v,%v)", tc.expected, tc.err, result, err)
}
}
}

func BenchmarkMinCurrencyRequiredBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MinCurrencyRequiredBrute(tc.n, tc.denom)
}
}
}

0 comments on commit 38f3501

Please sign in to comment.