-
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.
- Loading branch information
Showing
3 changed files
with
73 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,22 @@ | ||
package day401 | ||
|
||
// ApplyPermutation mutates the given input based on the | ||
// permutation information in the 2nd argument. | ||
// Runs in O(N) time and O(N/2) space. | ||
// Correct input is the responsibility of the caller. | ||
func ApplyPermutation(input []interface{}, permutation []int) { | ||
if len(input) != len(permutation) { | ||
panic("permutation length must match input length") | ||
} | ||
|
||
passed := make(map[int]interface{}, len(input)) | ||
|
||
for i, pos := range permutation { | ||
if pos < i { | ||
input[i] = passed[pos] | ||
} else { | ||
passed[i] = input[i] | ||
input[i] = input[pos] | ||
} | ||
} | ||
} |
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,50 @@ | ||
package day401 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input []interface{} | ||
perm []int | ||
expected []interface{} | ||
}{ | ||
{ | ||
[]interface{}{"a", "b", "c"}, | ||
[]int{2, 1, 0}, | ||
[]interface{}{"c", "b", "a"}, | ||
}, | ||
} | ||
|
||
func TestApplyPermutation(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
copied := append([]interface{}{}, tc.input...) | ||
if ApplyPermutation(copied, tc.perm); !reflect.DeepEqual(copied, tc.expected) { | ||
t.Errorf("Expected %v, got %v", tc.expected, copied) | ||
} | ||
} | ||
} | ||
|
||
func TestApplyPermutationBadInput(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Errorf("Expected a panic") | ||
} | ||
}() | ||
ApplyPermutation([]interface{}{"a", "b", "c"}, []int{2, 1, 0, 3}) | ||
} | ||
|
||
func BenchmarkApplyPermutation(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
copied := append([]interface{}{}, tc.input...) | ||
ApplyPermutation(copied, tc.perm) | ||
} | ||
} | ||
} |