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 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
}

View file

@ -1,57 +1,66 @@
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
}{
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 5,
newPosition: 2,
want: []any{1, 2, 6, 3, 4, 5, 7, 8},
},
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 7,
newPosition: 0,
want: []any{8, 1, 2, 3, 4, 5, 6, 7},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 0,
newPosition: 5,
want: []any{"one", "two", "three", "four", "five", "zero"},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 2,
newPosition: 3,
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) { var cases = []Cases{
return func(t *testing.T) { {
t.Logf("Input list: %v", list) list: []any{1, 2, 3, 4, 5, 6, 7, 8},
t.Logf("We want '%v' to move to position %d", list[oldPosition], newPosition) oldPosition: 5,
newPosition: 2,
want: []any{1, 2, 6, 3, 4, 5, 7, 8},
},
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 7,
newPosition: 0,
want: []any{8, 1, 2, 3, 4, 5, 6, 7},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 0,
newPosition: 5,
want: []any{"one", "two", "three", "four", "five", "zero"},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 2,
newPosition: 3,
want: []any{"zero", "one", "three", "two", "four", "five"},
},
}
got := shuffle(list, oldPosition, newPosition) func TestShuffleFuncOne(t *testing.T) {
if !reflect.DeepEqual(want, got) { for i := range cases {
t.Errorf("TEST FAILED: want: %v, got %v", want, got) 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 := 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 { } else {
t.Logf("TEST PASSED: got %v", got) t.Logf("TEST PASSED: got %v", got)
} }