-1

Good morning, I saw this post this and the question asked was how can I make a script/function that makes every possible combination of letters. I found the code from user Kevin pretty useful so I started using it. although sometimes my code stops and it has to go all over again.

My question is: how can I start this code (see below) with an string that I choose. for example if I type somewhere bbbb it's going to generate every possible combination between bbbb and cccc.

from string import ascii_lowercase
import itertools

def iter_all_strings():
    for size in itertools.count(1):
        for s in itertools.product(ascii_lowercase, repeat=size):
            yield "".join(s)

for s in iter_all_strings():
    print(s)
    if s == 'cccc':
        break

EDIT: so I found the source code of itertools.product() and it is this:

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

source: https://docs.python.org/3/library/itertools.html#itertools.combinations Does anyone how can I accomplish my goal with this code?

Sven
  • 49
  • 6
  • You're going to need to write your own generator that allows you specify a starting point. It shouldn't be very hard. – Tom Karzes Sep 25 '21 at 10:05

2 Answers2

0

Try -

a = input('Enter Letter Combination: ') 
check = False
for s in iter_all_strings():
    if s == a:
        check = True

    if check:
        print(s)

    if s == 'cccc':
        break

If you do not want input, then just set the value of a as - a = 'bbbb'

PCM
  • 2,881
  • 2
  • 8
  • 30
  • Thanks for the reply but this would not be helpful for me. bbbb and cccc were just examples. I'm already at zyWeAZ so generating everything from a to zyWeAZ would take another couple of hours before it start generating again. – Sven Sep 25 '21 at 09:31
0

Is this what you are looking for?

import itertools

def iter_all_strings():
    for size in itertools.count(1):
        for s in itertools.product(('b','c'), repeat=size):
            yield "".join(s)
        if size == 4:
            break;

for s in iter_all_strings():
    print(s)

Here you can take the size as the input and the strings as input.