feat: add second solution for shuffle experiment

This commit is contained in:
Dan Anglin 2024-01-24 17:51:53 +00:00
parent 71eb585ac6
commit 3c9afeb1b9
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 86 additions and 47 deletions

View file

@ -7,10 +7,10 @@ const (
right
)
// shuffle 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
// shuffleFuncOne creates a new list where an element in one position is repositioned
// 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.
func shuffle(list []any, oldPos, newPos int) []any {
func shuffleFuncOne(list []any, oldPos, newPos int) []any {
var direction shuffleDirection
if newPos < oldPos {
@ -40,3 +40,33 @@ func shuffle(list []any, oldPos, newPos int) []any {
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
}

View file

@ -1,18 +1,18 @@
package main
import (
"fmt"
"reflect"
"testing"
)
func TestShuffle(t *testing.T) {
cases := []struct {
type Cases struct {
list []any
oldPosition int
newPosition int
want []any
}{
}
var cases = []Cases{
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 5,
@ -39,19 +39,28 @@ func TestShuffle(t *testing.T) {
},
}
func TestShuffleFuncOne(t *testing.T) {
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))
}
}
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)
func testFunc(list []any, oldPosition, newPosition int, want []any) func(t *testing.T) {
return func(t *testing.T) {
t.Logf("Input list: %v", list)
t.Logf("We want '%v' to move to position %d", list[oldPosition], newPosition)
got := shuffle(list, oldPosition, newPosition)
if !reflect.DeepEqual(want, got) {
t.Errorf("TEST FAILED: want: %v, got %v", want, got)
got := shuffleFuncOne(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 {
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 {
t.Logf("TEST PASSED: got %v", got)
}