-3

I am writing a program that will combine words like permute them but i when i run the code, if prints them anyhow

i would like the program to run in an orderly manner like after running the first print statement, it will move to the second print statement instead of scattering it

here is the code

len_of_char = input('What is the lenght of the characters: ')
if len_of_char == 1:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        print(str(a) + '\n')
elif len_of_char == 2:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        for b in ['a', 'b', 'c', 'd', 'e', 'f']:
            print(str(a) + '\n')
            print(str(a + b) + '\n')

elif len_of_char == 3:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        for b in ['a', 'b', 'c', 'd', 'e', 'f']:
            for c in ['a', 'b', 'c', 'd', 'e', 'f']:
                print(str(a) + '\n')
                print(str(a + b) + '\n')
                print(str(a + b + c) + '\n')

elif len_of_char == 4:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        for b in ['a', 'b', 'c', 'd', 'e', 'f']:
            for c in ['a', 'b', 'c', 'd', 'e', 'f']:
                for d in ['a', 'b', 'c', 'd', 'e', 'f']:
                    print(str(a) + '\n')
                    print(str(a + b) + '\n')
                    print(str(a + b + c) + '\n')
                    print(str(a + b + c + d) + '\n')

elif len_of_char == 5:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        for b in ['a', 'b', 'c', 'd', 'e', 'f']:
            for c in ['a', 'b', 'c', 'd', 'e', 'f']:
                for d in ['a', 'b', 'c', 'd', 'e', 'f']:
                    for e in ['a', 'b', 'c', 'd', 'e', 'f']:
                        print(str(a) + '\n')
                        print(str(a + b) + '\n')
                        print(str(a + b + c) + '\n')
                        print(str(a + b + c + d) + '\n')
                        print(str(a + b + c + d + e) + '\n')
elif len_of_char == 6:
    for a in ['a', 'b', 'c', 'd', 'e', 'f']:
        for b in ['a', 'b', 'c', 'd', 'e', 'f']:
            for c in ['a', 'b', 'c', 'd', 'e', 'f']:
                for d in ['a', 'b', 'c', 'd', 'e', 'f']:
                    for e in ['a', 'b', 'c', 'd', 'e', 'f']:
                        for f in ['a', 'b', 'c', 'd', 'e', 'f']:
                            print(str(a) + '\n')
                            print(str(a + b) + '\n')
                            print(str(a + b + c) + '\n')
                            print(str(a + b + c + d) + '\n')
                            print(str(a + b + c + d + e) + '\n')
                            print(str(a + b + c + d +e +f) + '\n')
  • Can you provide an example desired output? I am not sure what _scattering_ means. Also, your code can be simplified a lot. A simple example would be to define `letters = ['a', 'b', 'c', 'd', 'e', 'f']` and then replacing that entire list with the variable `letters`. – Jamie Counsell Jan 19 '17 at 22:17
  • what i mean by scattering is that it do print a aa aaa a aa aab a aa aac a aa aad instead of printing all the a b c d e f aa ab ac ad ae ..... – ELOIKE DAVID Jan 19 '17 at 22:20
  • Providing an example of what you're looking for is your best bet here. Please add it to the question as an edit to preseve newlines. – Jamie Counsell Jan 19 '17 at 22:22
  • 1
    The order in which your program executes depends in the control flow of your program. In your case, Python will jump to whichever if statement holds true and skip all the rest. – Christian Dean Jan 19 '17 at 22:22
  • I would also look into using `itertools` to eliminate all these statements – gold_cy Jan 19 '17 at 22:23
  • Also, you shouldn't be trying to _hard-code_ all of those permutations. There are better ways of doing it such as: http://stackoverflow.com/questions/8306654/finding-all-possible-permutations-of-a-given-string-in-python/20955291#20955291. – Christian Dean Jan 19 '17 at 22:26
  • i dont understand – ELOIKE DAVID Jan 19 '17 at 22:26
  • am still new to python thats why i do the hard coding – ELOIKE DAVID Jan 19 '17 at 22:30

4 Answers4

1
for prod in itertools.product(*[['a', 'b', 'c', 'd', 'e', 'f']]*len_of_char ):
   print prod

... I guess

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • i tried it, it gave me this error message for prod in itertools.product(*[['a', 'b', 'c', 'd', 'e', 'f']]*len_of_char ): NameError: name 'itertools' is not defined – ELOIKE DAVID Jan 19 '17 at 22:41
1

Specifically, I think what you want is:

import itertools
alf = 'abcdef'
for n in range(1, len(alf)+1):
    for w in map(lambda t: ''.join(t), itertools.combinations_with_replacement(alf, n)):
         print w

itertools is standard Python library full of many very handy tools for iterating through lists and other collections, hence the name.

user2137858
  • 229
  • 2
  • 3
0

The code is not running scattered. It's running exactly how you programmed it to do. Take a look at what I wrote, see how it works, then look to see how to improve on it. itertools would be more efficient, but I didn't have the time.

def permute(inputNum):
    letters = ['a', 'b', 'c', 'd', 'e', 'f']

    for i in range(int(inputNum),0,-1):
        if i >= 1:
            for a in letters:
                if i >= 2:
                    for b in letters:
                        if i >= 3:
                            for c in letters:
                                if i >= 4:
                                    for d in letters:
                                        if i >= 5:
                                            for e in letters:
                                                if i >= 6:
                                                    for f in letters:
                                                        print(a+b+c+d+e+f,"\n")

                                                else:
                                                    print(a+b+c+d+e,"\n")
                                       else:
                                           print(a+b+c+d,"\n")
                                else:
                                    print(a+b+c,"\n")
                        else:
                            print(a+b,"\n")
                else:
                    print(a,"\n")

myNum = input("Enter a number from 1 to 6 ---> ")

permute(myNum)

This should give you the output I believe you are looking for.

I also don't understand why you have a multi-threading tag on this question, as it has nothing to do with multi-threading.

Casey
  • 419
  • 5
  • 13
  • Sorry about that. Edited to fix grammer. – Casey Jan 19 '17 at 23:19
  • Don't forget to accept what you think is the best answer. – Casey Jan 19 '17 at 23:20
  • No. Multi-threading would allow you to process the permutations (as the full 6 level permutations will give you over 500,000 print statements) while still being able to execute other code. – Casey Jan 19 '17 at 23:30
-1

You have an extra for loop for the case of length 2.

elif len_of_char == 2:
   for a in ['a', 'b', 'c', 'd', 'e', 'f']:
       for b in ['a', 'b', 'c', 'd', 'e', 'f']:
           for a in ['a', 'b', 'c', 'd', 'e', 'f']: # here is the extra for loop
               print(str(a) + '\n')
               print(str(a + b) + '\n')

Sorry I can't comment, so I just wanted to point out the apparent errors in the code and thought it'd be easy for you to spot the error once I mentioned it. I tested your codes and didn't see the problems except in case of len_of_char == 2.

Shiping
  • 1,203
  • 2
  • 11
  • 21
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Uyghur Lives Matter Jan 19 '17 at 22:51