Skip to content

Commit

Permalink
Merge pull request #201 from vaskoz/day96
Browse files Browse the repository at this point in the history
Day96
  • Loading branch information
vaskoz committed Nov 27, 2018
2 parents 439cbed + 876ffca commit 8139b2a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ problems from
* [Day 91](https://github.com/vaskoz/dailycodingproblem-go/issues/192)
* [Day 92](https://github.com/vaskoz/dailycodingproblem-go/issues/194)
* [Day 95](https://github.com/vaskoz/dailycodingproblem-go/issues/198)
* [Day 96](https://github.com/vaskoz/dailycodingproblem-go/issues/200)
74 changes: 74 additions & 0 deletions day96/problem.go
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)
}
65 changes: 65 additions & 0 deletions day96/problem_test.go
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)
}
}
}

0 comments on commit 8139b2a

Please sign in to comment.