From 3889536a56748500b2332aea768ae954dfd48132 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 21 Jan 2022 13:12:47 -0800 Subject: [PATCH] init --- .gitignore | 1 + LICENSE | 13 +++++++++++++ README.md | 34 ++++++++++++++++++++++++++++++++++ go.mod | 3 +++ permutation.go | 33 +++++++++++++++++++++++++++++++++ permutation_test.go | 12 ++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 permutation.go create mode 100644 permutation_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..53786be --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2021 Evan Burkey + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..716e463 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# permutation +A simple permutation package using generics. Requires go1.18beta1 or higher + +## Install +```bash +go get git.fputs.com/fputs/permutation +``` + +## Usage + +```go +package main + +import ( + "fmt" + + perm "git.fputs.com/fputs/permutation" +) + +func main() { + a := []int{1, 2, 3, 4} + p := perm.Permutations(a) + fmt.Println(p) +} +``` +result: +``` +[1 2 3] +[2 1 3] +[3 1 2] +[1 3 2] +[2 3 1] +[3 2 1] +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..50f02b6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.fputs.com/fputs/permutation + +go 1.18 diff --git a/permutation.go b/permutation.go new file mode 100644 index 0000000..ab0e1ae --- /dev/null +++ b/permutation.go @@ -0,0 +1,33 @@ +package permutation + +type GenSlice[T any] []T + +func Permutations[T any](arr GenSlice[T]) []GenSlice[T] { + var helper func(GenSlice[T], int) + var res []GenSlice[T] + + helper = func(arr GenSlice[T], n int) { + if n == 1 { + var tmp GenSlice[T] + for _, i := range arr { + tmp = append(tmp, i) + } + res = append(res, tmp) + } else { + for i := 0; i < n; i++ { + helper(arr, n-1) + if n%2 == 1 { + tmp := arr[i] + arr[i] = arr[n-1] + arr[n-1] = tmp + } else { + tmp := arr[0] + arr[0] = arr[n-1] + arr[n-1] = tmp + } + } + } + } + helper(arr, len(arr)) + return res +} diff --git a/permutation_test.go b/permutation_test.go new file mode 100644 index 0000000..7bb94ec --- /dev/null +++ b/permutation_test.go @@ -0,0 +1,12 @@ +package permutation + +import ( + "fmt" + "testing" +) + +func Test(t *testing.T) { + a := []int{1, 2, 3, 4} + p := Permutations(a) + fmt.Println(p) +}