-
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 #201 from vaskoz/day96
Day96
- Loading branch information
Showing
3 changed files
with
140 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,74 @@ | ||
package day96 | ||
|
||
// HeapsAlgorithmRecursive finds all the permutations recursively. | ||
func HeapsAlgorithmRecursive(digits []int) <-chan []int { | ||
num := 1 | ||
for i := 2; i <= len(digits); i++ { | ||
num *= i | ||
} | ||
copied := make([]int, len(digits)) | ||
copy(copied, digits) | ||
perms := make(chan []int, num) | ||
go generateRecursive(len(copied), copied, perms) | ||
return perms | ||
} | ||
|
||
func generateRecursive(n int, digits []int, perms chan []int) { | ||
if n == 1 { | ||
copied := make([]int, len(digits)) | ||
copy(copied, digits) | ||
perms <- copied | ||
return | ||
} | ||
for i := 0; i < n-1; i++ { | ||
generateRecursive(n-1, digits, perms) | ||
if n%2 == 0 { | ||
digits[i], digits[n-1] = digits[n-1], digits[i] | ||
} else { | ||
digits[0], digits[n-1] = digits[n-1], digits[0] | ||
} | ||
} | ||
generateRecursive(n-1, digits, perms) | ||
if n == len(digits) { | ||
close(perms) | ||
} | ||
} | ||
|
||
// HeapsAlgorithmIterative finds all the permutations iteratively. | ||
func HeapsAlgorithmIterative(digits []int) <-chan []int { | ||
num := 1 | ||
for i := 2; i <= len(digits); i++ { | ||
num *= i | ||
} | ||
copied := make([]int, len(digits)) | ||
copy(copied, digits) | ||
perms := make(chan []int, num) | ||
go generateIterative(len(copied), copied, perms) | ||
return perms | ||
} | ||
|
||
func generateIterative(n int, digits []int, perms chan []int) { | ||
c := make([]int, len(digits)) | ||
send := make([]int, len(digits)) | ||
copy(send, digits) | ||
perms <- send | ||
var i int | ||
for i < n { | ||
if c[i] < i { | ||
if i%2 == 0 { | ||
digits[0], digits[i] = digits[i], digits[0] | ||
} else { | ||
digits[c[i]], digits[i] = digits[i], digits[c[i]] | ||
} | ||
send = make([]int, len(digits)) | ||
copy(send, digits) | ||
perms <- send | ||
c[i]++ | ||
i = 0 | ||
} else { | ||
c[i] = 0 | ||
i++ | ||
} | ||
} | ||
close(perms) | ||
} |
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,65 @@ | ||
package day96 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
input []int | ||
expected [][]int | ||
}{ | ||
{[]int{1, 2, 3}, | ||
[][]int{ | ||
{1, 2, 3}, | ||
{2, 1, 3}, | ||
{3, 1, 2}, | ||
{1, 3, 2}, | ||
{2, 3, 1}, | ||
{3, 2, 1}}, | ||
}, | ||
} | ||
|
||
func TestHeapsAlgorithmRecursive(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
results := HeapsAlgorithmRecursive(tc.input) | ||
pos := 0 | ||
for result := range results { | ||
if !reflect.DeepEqual(result, tc.expected[pos]) { | ||
t.Errorf("Expected %v got %v", tc.expected[pos], result) | ||
} | ||
pos++ | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkHeapsAlgorithmRecursive(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
HeapsAlgorithmRecursive(tc.input) | ||
} | ||
} | ||
} | ||
|
||
func TestHeapsAlgorithmIterative(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
results := HeapsAlgorithmIterative(tc.input) | ||
pos := 0 | ||
for result := range results { | ||
if !reflect.DeepEqual(result, tc.expected[pos]) { | ||
t.Errorf("Expected %v got %v", tc.expected[pos], result) | ||
} | ||
pos++ | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkHeapsAlgorithmIterative(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
HeapsAlgorithmIterative(tc.input) | ||
} | ||
} | ||
} |