Let’s program a commline line tool using GoLang. And we take the first parameter and perform a full permutation on it – then it outputs each permutation line by line.
The string in GoLang is immutable – thus we convert it to byte array and then we can swap two characters in the Recursion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // perm.go package main import ( "fmt" "os" ) func perm(s []byte, idx int) { if idx == len(s) { fmt.Println(string(s)) return } for i := idx; i < len(s); i++ { s[i], s[idx] = s[idx], s[i] perm(s, idx+1) s[i], s[idx] = s[idx], s[i] } } func main() { if len(os.Args) != 2 { os.Exit(0) } perm([]byte(os.Args[1]), 0) } |
// perm.go package main import ( "fmt" "os" ) func perm(s []byte, idx int) { if idx == len(s) { fmt.Println(string(s)) return } for i := idx; i < len(s); i++ { s[i], s[idx] = s[idx], s[i] perm(s, idx+1) s[i], s[idx] = s[idx], s[i] } } func main() { if len(os.Args) != 2 { os.Exit(0) } perm([]byte(os.Args[1]), 0) }
1 2 3 4 5 6 7 | $ go run perm.go 123 123 132 213 231 321 312 |
$ go run perm.go 123 123 132 213 231 321 312
Full Permutation Algorithms:
- A Recursive Full Permutation Algorithm in Python
- Teaching Kids Programming – Recursive Permutation Algorithm
- The Permutation Iterator in Python
- GoLang: Full Permutation Command Line Tool
- Teaching Kids Programming – Introduction to Permutation and Combination
- The Permutation Algorithm for Arrays using Recursion
- Full Permutation Algorithm Implementation in BASH
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
336 wordsloading...
Last Post: Teaching Kids Programming - Sum of Distinct Positive Factorial Numbers
Next Post: Teaching Kids Programming - Sum of Distinct Positive Factorial Numbers via Depth First Search Algorithm