2024-01-23 20:51:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
type shuffleDirection int
|
|
|
|
|
|
|
|
const (
|
|
|
|
left shuffleDirection = iota
|
|
|
|
right
|
|
|
|
)
|
|
|
|
|
2024-01-24 17:51:53 +00:00
|
|
|
// 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
|
2024-01-23 20:51:52 +00:00
|
|
|
// or right depending on the direction of the move.
|
2024-01-24 23:33:37 +00:00
|
|
|
func shuffleFuncOne(input []any, oldPos, newPos int) []any {
|
2024-01-23 20:51:52 +00:00
|
|
|
var direction shuffleDirection
|
|
|
|
|
|
|
|
if newPos < oldPos {
|
|
|
|
direction = right
|
|
|
|
} else {
|
|
|
|
direction = left
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
output := make([]any, len(input))
|
2024-01-23 20:51:52 +00:00
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
for i := range input {
|
2024-01-23 20:51:52 +00:00
|
|
|
switch {
|
|
|
|
case (direction == right) && (i < newPos || i > oldPos):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
case (direction == right) && (i == oldPos):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[newPos] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
case (direction == right):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i+1] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
case (direction == left) && (i < oldPos || i > newPos):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
case (direction == left) && (i == oldPos):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[newPos] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
case (direction == left):
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i-1] = input[i]
|
2024-01-23 20:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
return output
|
2024-01-23 20:51:52 +00:00
|
|
|
}
|
2024-01-24 17:51:53 +00:00
|
|
|
|
|
|
|
// 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.
|
2024-01-24 23:33:37 +00:00
|
|
|
func shuffleFuncTwo(input []any, oldPos, newPos int) []any {
|
|
|
|
output := make([]any, len(input))
|
2024-01-24 17:51:53 +00:00
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
copy(output, input)
|
2024-01-24 17:51:53 +00:00
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
output[newPos] = input[oldPos]
|
2024-01-24 17:51:53 +00:00
|
|
|
|
|
|
|
if newPos < oldPos {
|
|
|
|
for i := newPos; i <= oldPos; i++ {
|
|
|
|
if i != oldPos {
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i+1] = input[i]
|
2024-01-24 17:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
return output
|
2024-01-24 17:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := oldPos; i <= newPos; i++ {
|
|
|
|
if i != oldPos {
|
2024-01-24 23:33:37 +00:00
|
|
|
output[i-1] = input[i]
|
2024-01-24 17:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:33:37 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// shuffleFuncThree produces the same output as the above two functions by simply swapping
|
|
|
|
// the target element with the element one above or below in the list until that element reaches
|
|
|
|
// the desired index.
|
|
|
|
func shuffleFuncThree(list []any, oldPos, newPos int) []any {
|
|
|
|
if newPos < oldPos {
|
|
|
|
for i := oldPos; i > newPos; i-- {
|
|
|
|
list[i], list[i-1] = list[i-1], list[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := oldPos; i < newPos; i++ {
|
|
|
|
list[i], list[i+1] = list[i+1], list[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
2024-01-24 17:51:53 +00:00
|
|
|
}
|