Skip to content

Commit

Permalink
Merge pull request #735 from vaskoz/day332
Browse files Browse the repository at this point in the history
Day332
  • Loading branch information
vaskoz committed Oct 21, 2019
2 parents 5667acc + 5cff1aa commit 747ac0a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ problems from
* [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658)
* [Day 325](https://github.com/vaskoz/dailycodingproblem-go/issues/660)
* [Day 326](https://github.com/vaskoz/dailycodingproblem-go/issues/661)
* [Day 332](https://github.com/vaskoz/dailycodingproblem-go/issues/669)
* [Day 333](https://github.com/vaskoz/dailycodingproblem-go/issues/668)
* [Day 337](https://github.com/vaskoz/dailycodingproblem-go/issues/674)
* [Day 338](https://github.com/vaskoz/dailycodingproblem-go/issues/675)
Expand Down
24 changes: 24 additions & 0 deletions day332/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package day332

// Pair is a set of the A and B values that satisfy
// the requirements below.
type Pair struct {
A, B int
}

// PositiveIntegerPairs returns all candidates
// that satisfy the following conditions:
// a + b = M
// a XOR b = N
func PositiveIntegerPairs(m, n int) []Pair {
var pairs []Pair

for a := 0; a < (m/2)+1; a++ {
b := m - a
if a^b == n {
pairs = append(pairs, Pair{A: a, B: b})
}
}

return pairs
}
35 changes: 35 additions & 0 deletions day332/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day332

import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
m, n int
expected []Pair
}{
{
100, 4,
[]Pair{{48, 52}},
},
}

func TestPositiveIntegerPairs(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if pairs := PositiveIntegerPairs(tc.m, tc.n); !reflect.DeepEqual(pairs, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, pairs)
}
}
}

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

0 comments on commit 747ac0a

Please sign in to comment.