0

I am fairly new to the more 'intermediate' concepts in python. To recap what I've 'learned', I decided to make a combination generator. Basically, you give some input then it should return every combination of it. (e.g. I type in 'mark' and it should return 'kram', 'makr' etc.)

But I suspect that there's a problem with the 'combinations' function, I cant quite figure it out why.

from itertools import combinations


def combo_generator():
    user_string = {}
    for l in input("Enter a string for combo: "):
        combo = combinations(user_string, 8)
        user_string[l] = 1

    if user_string[l] >= 8:
        print("Too much strings! Enter less than 10 letters.")
        combo_generator()

    else:
        print(list(combo))


combo_generator()

Because here is the broken output:

Enter a string for combo: "mark"
[]
[]
[]
[]

Any help will do. Thanks.

colappse
  • 71
  • 5

1 Answers1

0

The return value of input() is a str, and when you call for i in str, like in for l in input("Enter a string for combo: "):, you process each character separately.

And as the itertools docs specify, The number of items returned is n! / r! / (n-r)! when 0 <= r <= n or zero when r > n.. Your n=1 and r=8, therefore you observe empty arrays.

I believe you probably expected input() to be a bunch of words separated by spaces, if that's the case, you may wanna change your code to:

 for l in input("Enter a string for combo: ").split(" "):
     combo = combinations(user_string, 8)

but keep in mind that your words must be at least 8 characters long (and even then, it wouldn't be very cool of a combination. You may want to look into combinations_with_repetition()).

Captain Trojan
  • 2,800
  • 1
  • 11
  • 28
  • Alright, thank you. – colappse Dec 29 '20 at 14:28
  • This is not accurate. Note that while the OP is doing `for l in input()` they still call `combinations(user_string, 8)`... The problem is much simpler than that: `len(user_string) = 4` so there are no combinations of that string with 8 elements... – Tomerikoo Dec 29 '20 at 14:31
  • A solution in a similar question worked. I just gotta work on the 'letter detector' to avoid any crashes because of too much combinations. – colappse Dec 29 '20 at 14:45