Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def perm(tokens) {
- if (!tokens || tokens.size() == 1) {
- return [tokens];
- }
- if (tokens.size() == 2) {
- return [tokens, tokens.reverse()];
- }
- def permutations = []
- for (def token : tokens) {
- def tail = tokens.collect() - token;
- def tail_perms = perm(tail)
- for (def p : tail_perms) {
- p.add(0, token)
- permutations += [p]
- }
- }
- return permutations
- }
- assert [['a', 'b'], ['b', 'a']] == perm('ab'.toList())
- assert [['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']] == perm('abc'.toList())
- assert ['d', 'c', 'b', 'a'] == perm('abcd'.toList())[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement