https://github.com/isaccanedo/go-itertools
:rotating_light: Combinations and permutations generator in Go, in a similar way to python itertools
https://github.com/isaccanedo/go-itertools
combina combinations go golang permutation python
Last synced: about 1 year ago
JSON representation
:rotating_light: Combinations and permutations generator in Go, in a similar way to python itertools
- Host: GitHub
- URL: https://github.com/isaccanedo/go-itertools
- Owner: isaccanedo
- License: gpl-3.0
- Created: 2024-04-25T11:14:49.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-26T15:53:24.000Z (almost 2 years ago)
- Last Synced: 2024-04-26T17:03:43.525Z (almost 2 years ago)
- Topics: combina, combinations, go, golang, permutation, python
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README
# itertools
Itertools é um kit de ferramentas para gerar combinações e permutações de go slices. Funciona de forma semelhante ao python itertools.
## Installation
go get -u github.com/isaccanedo/go-itertools
## Usage (Examples)
After installation the package has to be imported.
```go
import "github.com/isaccanedo/go-itertools"
```
###Generating combinations
*Slices of integers*
```go
// combinations of r = 3 elements chosen from iterable
r := 3
iterable := []int{1, 2, 3, 4 }
for v := range CombinationsInt(iterable, r) {
fmt.Println(v)
```
output:
```
[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]
```
*Slices of strings*
```go
// combinations of r = 3 elements chosen from iterable
r := 3
iterable := []string{"A", "B", "C", "D"}
for v := range CombinationsStr(iterable, r) {
fmt.Println(v)
```
output:
```
[A B C]
[A B D]
[A C D]
[B C D]
```
*Custom type List*
Notice you have to use the right function depending of the type of the slice. For a more general case the user could define a custom type List as follows:
```go
type List []interface{}
```
Then can create a list of heterogeneous types and follow the same procedure. In this case CombinationsList has to be used.
```go
r := 3
myList := List{1, "B", 3, 3.14}
for v := range CombinationsList(myList, r) {
fmt.Println(v)
```
output:
```
[1 B 3]
[1 B 3.14]
[1 3 3.14]
[B 3 3.14]
```
###Generating permutations
*Slices of integers*
If the number (r) of chosen element is equal to the length of the _iterable_, then we will obtain the r-factorial permutations
```go
// permutations of r = 3 elements chosen from iterable with length 3
r := 3
iterable := []int{1, 2, 3}
for v := range PermutationsInt(iterable, r) {
fmt.Println(v)
```
output:
```
[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
[3 1 2]
[3 2 1]
```
On the other hand we can chose less than len(iterable) elements
```go
// permutations of r = 3 elements chosen from a iterable of length 4
r := 3
iterable := []int{1, 2, 3, 4}
for v := range CombinationsInt(iterable, r) {
fmt.Println(v)
```
output:
```
[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
[3 1 2]
[3 2 1]
[1 2 4]
[1 4 2]
[2 1 4]
[2 4 1]
[4 1 2]
[4 2 1]
[1 3 4]
[1 4 3]
[3 1 4]
[3 4 1]
[4 1 3]
[4 3 1]
[2 3 4]
[2 4 3]
[3 2 4]
[3 4 2]
[4 2 3]
[4 3 2]
```
*Slices of strings*
Same idea, just use the function PermutationsStr
*Custom type List*
Same as in combinations, fist define the new type List (see above) and then use PermutationsList.