10

I have a list of tokens, like:

hel
lo
bye

and i want to generate all the possible combinations of such strings, like:

hello
lohel
helbye
byehel
lobye
byelo

Language is not important, any advice?

I found Generating permutations using bash, but this makes permutation on a single line.

Community
  • 1
  • 1
lbedogni
  • 7,917
  • 8
  • 30
  • 51
  • LucaB, I don't think "hello" is a permutation of "hel", "lo", "bye" in the normal understanding of "permutation". Specifically, a permutation of a set cannot leave out members of the set. So answerers have to choose between what you explicitly asked for, and the sample output you show. Probably "combinations" is what you meant, as @Sven pointed out. – LarsH Oct 31 '10 at 02:20
  • You're right, I meant combinations. I'll update my question. – lbedogni Oct 31 '10 at 08:19
  • just to [link this](https://stackoverflow.com/a/5898031/875020) here for people looking for ALL combinations – x29a Feb 14 '18 at 18:08

8 Answers8

24

Your example can be written in Python as

from itertools import combinations
print list(combinations(["hel", "lo", "bye"], 2))

To combine the output to strings again:

print ["".join(a) for a in combinations(["hel", "lo", "bye"], 2)]

If you interested in the actual implementation of this function, have a look at the documentation.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
8

itertools.permutations can do that for you.

>>> l = ['hel', 'lo', 'bye']
>>> list(itertools.permutations(l, 2))
[('hel', 'lo'), ('hel', 'bye'), ('lo', 'hel'), ('lo', 'bye'), ('bye', 'hel'), ('bye', 'lo')]

Or if you want combinations, you can use itertools.combinations.

>>> l = ['hel', 'lo', 'bye']
>>> list(itertools.combinations(l, 2))
[('hel', 'lo'), ('hel', 'bye'), ('lo', 'bye')]
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
3

Given that other languages are acceptable:

#!/usr/bin/perl

use strict; use warnings;
use Algorithm::Combinatorics qw(permutations);

my $data = [ qw( hel lo bye ) ];
my $it = permutations($data);

while ( my $p = $it->next ) {
    print @$p, "\n";
}
hellobye
helbyelo
lohelbye
lobyehel
byehello
byelohel
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
2
a = ['hel', 'lo', 'bye']
print '\n'.join(''.join(x) for x in itertools.permutations(a, 2))
Mike Axiak
  • 11,827
  • 2
  • 33
  • 49
2

Easy in python with itertools.

Here is the token permutation example:

import itertools

tokens = ["hel", "lo", "bye"]

for i in range(1, len(tokens) + 1):
    for p in itertools.permutations(tokens, i):
        print "".join(p)

Alternatively, this treats each character as a token:

import itertools

tokens = ["hel", "lo", "bye"]

chars = "".join(tokens)
for i in range(1, len(chars) + 1):
    for p in itertools.permutations(chars, i):
        print "".join(p)
kanaka
  • 70,845
  • 23
  • 144
  • 140
2

Looks like you want permutations:

from itertools import permutations

# easy way to make a list for words
words = 'hel lo bye'.split()

# fetch two-word permutations, joined into a string
for word in [''.join(s) for s in permutations(words,2)]:
    print word

Output:

hello
helbye
lohel
lobye
byehel
byelo
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

Python has a permutations too. :)

ceth
  • 44,198
  • 62
  • 180
  • 289
0

Update: I see I wasn't explicit enough.

Haskell has a permutations function that would help:

import Data.List
permutations ["hel","lo","bye"] ==
[["hel","lo","bye"],["lo","hel","bye"],["bye","lo","hel"],
 ["lo","bye","hel"],["bye","hel","lo"],["hel","bye","lo"]]

If you want each permutation concatenated, use

map concat (permutations ["hel","lo","bye"]) ==
["hellobye","lohelbye","byelohel","lobyehel","byehello","helbyelo"]

If you actually want combinations of two substrings (like your example output) instead of all permutations of substrings, as @Sven noticed, use the Math.Combinatorics.Graph module and:

map concat (combinationsOf 2 ["hel","lo","bye"])

That matches your example data in some respects but not others. I could go on to speculate that you want "all possible strings" as the title says, or all permutations of two-token subsets, or what have you, but it's kind of pointless to speculate since you've already accepted an answer.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Nice, but this permutes all letters on a given string, not set of strings. – lbedogni Oct 30 '10 at 16:28
  • 1
    LucaB, permutations will work on any iterable. Look at the examples in python. – Mike Axiak Oct 30 '10 at 16:32
  • @LucaB, look again... the function permutes a list. If you pass it a list of characters (a string), it will permute those characters. If you pass it a list of strings, it will permute that list of strings. – LarsH Oct 31 '10 at 02:01
  • @LucaB, if you downvoted this answer because you thought the function I was talking about only worked on strings, consider changing your vote. In Haskell, strings are lists, and the function works on any list. – LarsH Nov 02 '10 at 09:21