I'm trying to generate incremental combinations from a string, like:
// for "23405"
2
3
4
5
23
34
40
05
234
340
405
2340
3405
23405
I'm doing it with nested loops:
str := "23405"
for i := 0; i <= len(str); i++ {
for j := 0; j <= i; j++ {
fmt.Println(str[j:i])
}
}
Is it possible to do the same with recursive function? I'm writing it with go but an example in any language would be helpful. Here's the playground link.
Here's my attempt of recursion: https://repl.it/ElYY/9
package main
import "fmt"
func reverse(str string, length int, i int) {
if len(str) > length+i && length > 0 {
fmt.Println(str[i:length+i])
reverse(str, length, i+1)
} else if len(str) == length+i && length > 0 {
fmt.Println(str[i:length+i])
reverse(str, length-1, 0)
}
}
func recIterate(str string, length int, i int) {
if length > i {
fmt.Println(str[i:len(str)-length+i+1])
recIterate(str, length, i+1)
} else if length == i && length > 0{
recIterate(str, length-1, 0)
}
}
func main() {
str := "234051234"
recIterate(str, len(str), 0)
// reverse(str, len(str), 0)
}
Shout out to nexus66 for helping~