Skip to content

Commit

Permalink
add Shuffle operation (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
psampaz authored Dec 6, 2019
1 parent fa2b096 commit 8cb84e4
Show file tree
Hide file tree
Showing 4 changed files with 1,608 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Max operation
- Min operation
- Reverse operation
- Shuffle operation
- Sum operation
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ go get github.com/psampaz/slice
| Push | - | - | - | - | - | - | - | - |
| Reverse |||||||||
| Shift | - | - | - | - | - | - | - | - |
| Shuffle | - | - | - | - | - | - | - | - |
| Shuffle | | | | | | | | |
| Sum |||||||||
| Unshift | - | - | - | - | - | - | - | - |

Expand Down Expand Up @@ -111,6 +111,14 @@ Reverse performs in place reversal of a slice
a = slice.ReverseInt(a) // [5, 4, 3, 2, 1]
```

## slice.Shuffle

Shuffle shuffles (in place) a slice
```go
a := []int{1, 2, 3, 4, 5}
a = slice.ShuffleInt(a) // [3, 5, 1, 4, 2] (random output)
```

## slice.Sum

Sum returns the sum of the values of a slice or an error in case of a nil or empty slice
Expand Down
272 changes: 272 additions & 0 deletions shuffle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package slice

import (
"math/rand"
"time"
)

// ShuffleBool shuffles (in place) a bool slice
func ShuffleBool(a []bool) []bool {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleByte shuffles (in place) a byte slice
func ShuffleByte(a []byte) []byte {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleComplex128 shuffles (in place) a complex128 slice
func ShuffleComplex128(a []complex128) []complex128 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleComplex64 shuffles (in place) a complex64 slice
func ShuffleComplex64(a []complex64) []complex64 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleFloat32 shuffles (in place) a float32 slice
func ShuffleFloat32(a []float32) []float32 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleFloat64 shuffles (in place) a float64 slice
func ShuffleFloat64(a []float64) []float64 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleInt shuffles (in place) a int slice
func ShuffleInt(a []int) []int {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleInt16 shuffles (in place) a int16 slice
func ShuffleInt16(a []int16) []int16 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleInt32 shuffles (in place) a int32 slice
func ShuffleInt32(a []int32) []int32 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleInt64 shuffles (in place) a int64 slice
func ShuffleInt64(a []int64) []int64 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleInt8 shuffles (in place) a int8 slice
func ShuffleInt8(a []int8) []int8 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleRune shuffles (in place) a rune slice
func ShuffleRune(a []rune) []rune {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleString shuffles (in place) a string slice
func ShuffleString(a []string) []string {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUint shuffles (in place) a uint slice
func ShuffleUint(a []uint) []uint {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUint16 shuffles (in place) a uint16 slice
func ShuffleUint16(a []uint16) []uint16 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUint32 shuffles (in place) a uint32 slice
func ShuffleUint32(a []uint32) []uint32 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUint64 shuffles (in place) a uint64 slice
func ShuffleUint64(a []uint64) []uint64 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUint8 shuffles (in place) a uint8 slice
func ShuffleUint8(a []uint8) []uint8 {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}

// ShuffleUintptr shuffles (in place) a uintptr slice
func ShuffleUintptr(a []uintptr) []uintptr {
if len(a) <= 1 {
return a
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})

return a
}
Loading

0 comments on commit 8cb84e4

Please sign in to comment.