Slice permutations of r length

Trying to figure out how to generate slice permutations of specific length. Say in Python I can do:

>>> import itertools
>>> l = list(itertools.permutations(range(1, 4), 2)) 
>>> print(l)
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

What would be the best way to achieve this in Go? I’ve been looking for a solution here which gives all permutations, but can’t figure out on how to specify length. Also, with larger slice this would eat up resources quickly.

I did something to handle this last year: https://github.com/skillian/itertools/blob/master/permutation_test.go

EDIT: I’m not sure I’d do it the same way again, but I think a lot of the technique still applies.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.