-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #735 from vaskoz/day332
Day332
- Loading branch information
Showing
3 changed files
with
60 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
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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |