2

I am making a program where you insert some letters and then the output are all the posible combinations of that letters.

For example: if the input is "ABC" the output should be "A","B","C","AB","AC","BC","ABC","ACB" and so on...

Finally, my idea is to put all that combinations in a set so that it can be intersect with another set containing a certain dictionary of english words being that intersection the ideal output

As far, my script is this one:

import random

p = list(raw_input('Insert some letters: '))

p2 = []

p3 = []
for j in range((len(p))):
    p2.append(p[j])

for i in range(len(p)):
    a = random.sample(p2,len(p))
    p3.append(str("".join(a)))
    print p3[]

Obviously, there are some errors and its not complete. Can you help me to finish or tell me which path should I take? Thanks for reading

Facundo
  • 729
  • 2
  • 6
  • 7

4 Answers4

6

If you don't care about order, you are looking for a combination. You can use itertools.combination for this:

import itertools

items = 'ABC'
for i in range(len(items)+1):
    for combination in itertools.combinations('ABC', i): 
        print(combination)

List Comprehension version:

[combination for i in range(len(items)+1) for combination in itertools.combinations('ABC', i)]
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
4

I like putting stuff like this into a nice generator function, for example:

from itertools import permutations

def words(letters):
    for n in range(1, len(letters)+1):
        yield from map(''.join, permutations(letters, n))

That can then conveniently be used for example like this:

for word in words('ABC'):
    print(word)

Or since you want it as a set:

>>> set(words('ABC'))
{'BA', 'BCA', 'CB', 'A', 'AC', 'AB', 'C', 'BC', 'CA', 'ABC', 'CBA', 'B', 'BAC', 'CAB', 'ACB'}

However, it might be a better idea to not generate this word set but go through those English words and check each whether it can be made from the given letters. Depends on which set is larger, which depends on the input letters.

Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
0

Pity I can not do +1 to Stefan. I have edited Stefan's genius answer(to my purpose):

from itertools import permutations

def words(letters):
    yield from map(''.join, permutations(letters, len(letters)))

result = set()
for word in words('algorithm'):
    result.add(word)

print(f' There are {len(result)} combinations')

for i, each in enumerate(result):
    if i%5 == 0 and input(''):
        break
    print(each)

It makes permutations of full length.

0

for all small alphabet combinations <=8 digits:

words = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for a in words:
    print(a)
    for b in words:
        print(a,b)
        for c in words:
            print(a,b,c)
            for d in words:
                print(a,b,c,d)
                for e in words:
                    print(a,b,c,d,e)
                    for f in words:
                        print(a,b,c,d,e,f)
                        for g in words:
                            print(a,b,c,d,e,f,g)
                            for h in words:
                                print(a,b,c,d,e,f,g,h)