enumerate_algorithm

package module
v0.0.0-...-3cc850a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 1 Imported by: 0

README

排列、组合、笛卡尔积等等

一、排列

slice := []int{1, 2, 3}
permutation := enumerate_algorithm.Permutation(slice)
fmt.Println(permutation)
// Output:
// [[1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 2 1] [3 1 2]]

二、组合

slice := []int{1, 2, 3, 4, 5, 6}
combination := enumerate_algorithm.Combination(slice, 5)
fmt.Println(combination)
// Output:
// [[1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [2 3 4 5 6]]

三、笛卡尔积

sliceA := []string{"A", "B", "C"}
sliceB := []int{1, 2, 3}
r := enumerate_algorithm.Product(sliceA, sliceB)
t.Log(r)
// Output:
// [("A", 1) ("A", 2) ("A", 3) ("B", 1) ("B", 2) ("B", 3) ("C", 1) ("C", 2) ("C", 3)]

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combination

func Combination[T any](slice []T, n int) [][]T

Combination 组合

Example
slice := []int{1, 2, 3, 4, 5, 6}
combination := Combination(slice, 5)
fmt.Println(combination)
Output:

[[1 2 3 4 5] [1 2 3 4 6] [1 2 3 5 6] [1 2 4 5 6] [1 3 4 5 6] [2 3 4 5 6]]

func Permutation

func Permutation[T any](slice []T) [][]T

Permutation 全排列

Example
slice := []int{1, 2, 3}
permutation := Permutation(slice)
fmt.Println(permutation)
Output:

[[1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 2 1] [3 1 2]]

func Product

func Product[T1, T2 any](sliceA []T1, sliceB []T2) []*tuple.Tuple2[T1, T2]

Product 计算两个切片的笛卡尔积

Example
sliceA := []string{"A", "B", "C"}
sliceB := []int{1, 2, 3}
r := Product(sliceA, sliceB)
fmt.Println(r)
Output:

[("A", 1) ("A", 2) ("A", 3) ("B", 1) ("B", 2) ("B", 3) ("C", 1) ("C", 2) ("C", 3)]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL