forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
algorithm: binomial coefficient (TheAlgorithms#539)
* feat: Add binomial coefficient implementation * test: Add tests for binomial coefficient * fix: Fix typing error in binomialcoefficient.go * fix: Add linter fixes to BenchmarkBinomialCoefficient() * fix: Fix import statement in binomialcoefficient_test.go * fix: Fix return statement * test: Add detailed unit tests for binomial coefficient * fix: Replace all occurrences of C with Combinations * fix: Move error variable outside Combinations function * refactor: Add t.Run block * refactor: Remove log statement * refactor: Refactor unit tests * fix: Change error variable name from error to err
- Loading branch information
1 parent
6c8e822
commit 94e8490
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// binomialcoefficient.go | ||
// description: Returns C(n, k) | ||
// details: | ||
// a binomial coefficient C(n,k) gives number ways | ||
// in which k objects can be chosen from n objects. | ||
// wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient | ||
// author: Akshay Dubey (https://github.com/itsAkshayDubey) | ||
// see binomialcoefficient_test.go | ||
|
||
package math | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ErrPosArgsOnly error = errors.New("arguments must be positive") | ||
|
||
// C is Binomial Coefficient function | ||
// This function returns C(n, k) for given n and k | ||
func Combinations(n int, k int) (int, error) { | ||
if n < 0 || k < 0 { | ||
return -1, ErrPosArgsOnly | ||
} | ||
if k > (n - k) { | ||
k = n - k | ||
} | ||
res := 1 | ||
for i := 0; i < k; i++ { | ||
res *= (n - i) | ||
res /= (i + 1) | ||
} | ||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// binomialcoefficient_test.go | ||
// description: Returns C(n, k) | ||
// author: Akshay Dubey (https://github.com/itsAkshayDubey) | ||
// see binomialcoefficient.go | ||
|
||
package math_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/TheAlgorithms/Go/math" | ||
) | ||
|
||
func TestCombinations(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
n int | ||
k int | ||
expectedValue int | ||
expectedError error | ||
}{ | ||
{"n = 5, k = 2", 5, 2, 10, nil}, | ||
{"n = 7, k = 4", 7, 4, 35, nil}, | ||
{"n = 0, k = 0", 0, 0, 1, nil}, | ||
{"n = -1, k = 1", -1, 1, -1, math.ErrPosArgsOnly}, | ||
{"n = 1, k = -1", 1, -1, -1, math.ErrPosArgsOnly}, | ||
{"n = -1, k = -1", -1, -1, -1, math.ErrPosArgsOnly}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result, err := math.Combinations(test.n, test.k) | ||
if result != test.expectedValue || test.expectedError != err { | ||
t.Errorf("expected error: %s, got: %s; expected value: %v, got: %v", test.expectedError, err, test.expectedValue, result) | ||
} | ||
}) | ||
} | ||
} | ||
func BenchmarkCombinations(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, _ = math.Combinations(65536, 65536) | ||
} | ||
} |