feat: add second solution for shuffle experiment
This commit is contained in:
parent
71eb585ac6
commit
3c9afeb1b9
2 changed files with 86 additions and 47 deletions
|
@ -7,10 +7,10 @@ const (
|
||||||
right
|
right
|
||||||
)
|
)
|
||||||
|
|
||||||
// shuffle creates a new list where an element in one position is repositioned
|
// shuffleFuncOne creates a new list where an element in one position is repositioned
|
||||||
// to the index of another element. All affected elements then shuffle one place to the left
|
// to the index of another element. All affected elements then shuffleFuncOne one place to the left
|
||||||
// or right depending on the direction of the move.
|
// or right depending on the direction of the move.
|
||||||
func shuffle(list []any, oldPos, newPos int) []any {
|
func shuffleFuncOne(list []any, oldPos, newPos int) []any {
|
||||||
var direction shuffleDirection
|
var direction shuffleDirection
|
||||||
|
|
||||||
if newPos < oldPos {
|
if newPos < oldPos {
|
||||||
|
@ -40,3 +40,33 @@ func shuffle(list []any, oldPos, newPos int) []any {
|
||||||
|
|
||||||
return newList
|
return newList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shuffleFuncTwo produces the same output as shuffleFuncOne using a slightly different method.
|
||||||
|
// The original slice is copied into a new slice and the element is immediately placed in the new
|
||||||
|
// position. The for loop then only focuses on the elements in range between the two positions and
|
||||||
|
// are shuffled left or right as required.
|
||||||
|
func shuffleFuncTwo(list []any, oldPos, newPos int) []any {
|
||||||
|
newList := make([]any, len(list))
|
||||||
|
|
||||||
|
copy(newList, list)
|
||||||
|
|
||||||
|
newList[newPos] = list[oldPos]
|
||||||
|
|
||||||
|
if newPos < oldPos {
|
||||||
|
for i := newPos; i <= oldPos; i++ {
|
||||||
|
if i != oldPos {
|
||||||
|
newList[i+1] = list[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newList
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := oldPos; i <= newPos; i++ {
|
||||||
|
if i != oldPos {
|
||||||
|
newList[i-1] = list[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newList
|
||||||
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestShuffle(t *testing.T) {
|
type Cases struct {
|
||||||
cases := []struct {
|
|
||||||
list []any
|
list []any
|
||||||
oldPosition int
|
oldPosition int
|
||||||
newPosition int
|
newPosition int
|
||||||
want []any
|
want []any
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
var cases = []Cases{
|
||||||
{
|
{
|
||||||
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
|
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
|
||||||
oldPosition: 5,
|
oldPosition: 5,
|
||||||
|
@ -37,21 +37,30 @@ func TestShuffle(t *testing.T) {
|
||||||
newPosition: 3,
|
newPosition: 3,
|
||||||
want: []any{"zero", "one", "three", "two", "four", "five"},
|
want: []any{"zero", "one", "three", "two", "four", "five"},
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
for i := range cases {
|
|
||||||
t.Run(fmt.Sprintf("Test Case %d", i), testFunc(cases[i].list, cases[i].oldPosition, cases[i].newPosition, cases[i].want))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFunc(list []any, oldPosition, newPosition int, want []any) func(t *testing.T) {
|
func TestShuffleFuncOne(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
for i := range cases {
|
||||||
t.Logf("Input list: %v", list)
|
t.Logf("Input list: %v", cases[i].list)
|
||||||
t.Logf("We want '%v' to move to position %d", list[oldPosition], newPosition)
|
t.Logf("We want '%v' to move to position %d", cases[i].list[cases[i].oldPosition], cases[i].newPosition)
|
||||||
|
|
||||||
got := shuffle(list, oldPosition, newPosition)
|
got := shuffleFuncOne(cases[i].list, cases[i].oldPosition, cases[i].newPosition)
|
||||||
if !reflect.DeepEqual(want, got) {
|
if !reflect.DeepEqual(cases[i].want, got) {
|
||||||
t.Errorf("TEST FAILED: want: %v, got %v", want, got)
|
t.Errorf("TEST FAILED: want: %v, got %v", cases[i].want, got)
|
||||||
|
} else {
|
||||||
|
t.Logf("TEST PASSED: got %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShuffleFuncTwo(t *testing.T) {
|
||||||
|
for i := range cases {
|
||||||
|
t.Logf("Input list: %v", cases[i].list)
|
||||||
|
t.Logf("We want '%v' to move to position %d", cases[i].list[cases[i].oldPosition], cases[i].newPosition)
|
||||||
|
|
||||||
|
got := shuffleFuncTwo(cases[i].list, cases[i].oldPosition, cases[i].newPosition)
|
||||||
|
if !reflect.DeepEqual(cases[i].want, got) {
|
||||||
|
t.Errorf("TEST FAILED: want: %v, got %v", cases[i].want, got)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("TEST PASSED: got %v", got)
|
t.Logf("TEST PASSED: got %v", got)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue