-1

I found this Python script that prints all permutations of given variables. I am a complete Python newby, but after a few modifications it does almost exactly what I need.

Here is what I got so far:

blocks =  [ "0", "1a", "2a", "2b" ] # variables
num_blocks = len( blocks )
num_places = 4  # number of places

for i in range( pow( len( blocks ), num_places ) ): 
    value = i
    indexes = []

    while value:    
        indexes.append( value % num_blocks )
        value = value // num_blocks

    # print( i + 1 ) # alternatively print number of each permutation

    for j in range( len( indexes ), num_places ):
        print blocks[ num_blocks - 1 ]

    for j in range( len( indexes ) - 1, -1, -1 ):
        print( blocks[ num_blocks - 1 - indexes[ j ] ] )

    print " "  # add a line break after each entry

The output is

2b
2b
2b
2b

2b
2b
2b
2a

etc.

How can I A) change the output to

2b2b2b2b
2b2b2b2a

etc.

and B) only print occurences that include the last variable, in this case "2b". For the purpose of this example, I only included four variables: 0, 1a, 2a, 2b. The script prints all possible combinations of these four variables, from 0000 to 2b2b2b2b and anything in between. Is it possible to print only combinations that include the last variable 2b? So for example 2b2a1a0 or 1a1a02b, but not 2a2a1a0 or 2a01a1a? Later on, many more variables will be included (3a, 3b, etc.), but the script shall only list permutations including the last variable.

Thanks in advance!

George

EDITED to clarify second question.

george
  • 167
  • 1
  • 4
  • 16

1 Answers1

1

Updated: This will clarify both of your questions. Thanks.

blocks =  [ "0", "1a", "2a", "2b" ] # variables
num_blocks = len( blocks )
num_places = 4  # number of places

for i in range( pow( len( blocks ), num_places ) ): 
  value = i
  indexes = []

  while value:    
      indexes.append(value % num_blocks)
      value = value // num_blocks

  # print( i + 1 ) # alternatively print number of each permutation
  
  output = ''
  for j in range( len( indexes ), num_places ):
      output +=  str(blocks[ num_blocks - 1 ])
  
  for j in range( len( indexes ) - 1, -1, -1 ):
      output += str(blocks[ num_blocks - 1 - indexes[j] ])
  
  search = blocks[3] # search '2b' in output
  if output.find(search) != -1 :
      print output,
      print " "
Parn
  • 878
  • 7
  • 19
  • Yay! First problem is almost solved! Output now is `2b 2b 2a 1a` followed by a line break followed by the next permutation. Cool! Must've overlooked the comma-advice in the duplicate post mentioned above. But how do I remove the spaces in between the variables? – george Nov 30 '18 at 22:52
  • Add `'\b'` before each print. It will be `print '\b' + blocks[ num_blocks - 1 ],` or `print(blocks[ num_blocks - 1 ], end='')` in Python 3.0 – Parn Nov 30 '18 at 23:03
  • Thanks! That did the trick! I also revisited the post mentioned above and tried the following solution: `import sys` and `sys.stdout.write(blocks[ num_blocks - 1 ])`, which works as well. First problem solved! – george Nov 30 '18 at 23:17
  • Code updated to solve your both questions – Parn Nov 30 '18 at 23:22
  • Oh yes, you nailed it. Output format is perfect and I only get combinations including the last variable. Thank you so much, you saved my a lot of time!!! – george Nov 30 '18 at 23:28