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,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,
@ -39,19 +39,28 @@ func TestShuffle(t *testing.T) {
}, },
} }
func TestShuffleFuncOne(t *testing.T) {
for i := range cases { 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) { got := shuffleFuncOne(cases[i].list, cases[i].oldPosition, cases[i].newPosition)
return func(t *testing.T) { if !reflect.DeepEqual(cases[i].want, got) {
t.Logf("Input list: %v", list) t.Errorf("TEST FAILED: want: %v, got %v", cases[i].want, got)
t.Logf("We want '%v' to move to position %d", list[oldPosition], newPosition) } else {
t.Logf("TEST PASSED: got %v", got)
got := shuffle(list, oldPosition, newPosition) }
if !reflect.DeepEqual(want, got) { }
t.Errorf("TEST FAILED: want: %v, got %v", want, 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)
} }