GOLANG_INTERVIEW Telegram 195
Вывод всех перестановок символов слайса или строки

Реализуйте функцию perm(), которая принимает слайс или строку и печатает все возможные комбинации символов.

Решение:

package main
import "fmt"

// Perm calls f with each permutation of a.

func Perm(a []rune, f func([]rune)) {
perm(a, f, 0)
}

// Permute the values at index i to len(a)-1.

func perm(a []rune, f func([]rune), i int) {
if i > len(a) {
f(a)
return
}
perm(a, f, i+1)
for j := i + 1; j < len(a); j++ {
a[i], a[j] = a[j], a[i]
perm(a, f, i+1)
a[i], a[j] = a[j], a[i]
}
}

func main() {
Perm([]rune("abc"), func(a []rune) {
fmt.Println(string(a))
})
}


Пишите свое решение в комментариях👇

#junior #golang
@golang_interview
👍52🔥1



tgoop.com/golang_interview/195
Create:
Last Update:

Вывод всех перестановок символов слайса или строки

Реализуйте функцию perm(), которая принимает слайс или строку и печатает все возможные комбинации символов.

Решение:

package main
import "fmt"

// Perm calls f with each permutation of a.

func Perm(a []rune, f func([]rune)) {
perm(a, f, 0)
}

// Permute the values at index i to len(a)-1.

func perm(a []rune, f func([]rune), i int) {
if i > len(a) {
f(a)
return
}
perm(a, f, i+1)
for j := i + 1; j < len(a); j++ {
a[i], a[j] = a[j], a[i]
perm(a, f, i+1)
a[i], a[j] = a[j], a[i]
}
}

func main() {
Perm([]rune("abc"), func(a []rune) {
fmt.Println(string(a))
})
}


Пишите свое решение в комментариях👇

#junior #golang
@golang_interview

BY Golang вопросы собеседований


Share with your friend now:
tgoop.com/golang_interview/195

View MORE
Open in Telegram


Telegram News

Date: |

As five out of seven counts were serious, Hui sentenced Ng to six years and six months in jail. While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: Healing through screaming therapy Telegram channels fall into two types:
from us


Telegram Golang вопросы собеседований
FROM American