How do I compute all possibilities for an array of numbers/bits (in python, or any language for that matter)

前端 未结 4 1632
野趣味
野趣味 2020-12-04 01:53

I have been wracking my brains out for 3 hours straight, but I still don\'t get it, so I am asking here. (I wrote Python in the title, but this could be for pretty much any

相关标签:
4条回答
  • 2020-12-04 02:07

    In Python, use itertools for stuff like this

    from itertools import product
    for i in product([0,1], repeat=5): 
        print i
    

    Yields:

    (0, 0, 0, 0, 0)
    (0, 0, 0, 0, 1)
    (0, 0, 0, 1, 0)
    (0, 0, 0, 1, 1)
    (0, 0, 1, 0, 0)
    etc...
    
    0 讨论(0)
  • 2020-12-04 02:09
    import numpy as np
    def all_combinations(width, vals):
        return np.array(np.meshgrid(*[vals]*width,
                        indexing='ij')).reshape((width,-1)).transpose()
    
    print(all_combinations(width=3, vals=[0,1]))
    print(all_combinations(width=2, vals=['a','b','c']))
    

    Output:

    [[0 0 0]
     [0 0 1]
     [0 1 0]
     [0 1 1]
     [1 0 0]
     [1 0 1]
     [1 1 0]
     [1 1 1]]
    [['a' 'a']
     ['a' 'b']
     ['a' 'c']
     ['b' 'a']
     ['b' 'b']
     ['b' 'c']
     ['c' 'a']
     ['c' 'b']
     ['c' 'c']]
    
    0 讨论(0)
  • 2020-12-04 02:11

    Here is a generalized recursive pseudocode to do what you seek.

    array_combination is function (length, elements)
      if length < 1 
      then abort
      end if
    
      declare arrays as new array
      if length is 1 
      then
        loop for element in elements
          declare element_array as new array
          set element_array[0] to element
          append element_array to arrays
        end loop
      else
        loop for array in array_combination(length - 1, elements)
          loop for element in elements
            declare element_array as new array
            set element_array[0] to element
            append array to element_array
            append element_array to arrays
          end loop
          append array to arrays
        end loop
      end if
      return arrays
    end function
    

    You would call this function as "array_combination(5, [1,0])" for your given example. There are better ways to build it but a) I am too old to do homework, b) I don't know the constraints of your assignment, and c) I don't want to make it too obvious that you cheated.

    Note repeated code and opportunities for common subexpression elimination along with extremely wasteful memory allocation and cache abuse. However, I'm assuming this is a first quarter computer science assignment, so they probably won't hold it against you.

    0 讨论(0)
  • 2020-12-04 02:13

    I would approach this problem by just looping from 0 to 31 (0b11111) and turning the binary representation into an array of fixed length.

    You didn't tag this with a language, so I'm not sure how to give you example code, but that approach should work.

    1: 00001
    2: 00010
    3: 00011
    ...
    30:11110
    31:11111
    

    Edit: Just saw you tagged this question with Python. Sample python code implementing the above algorithm:

    listLength=5
    for x in range(0,2**listlength):
        print(list(bin(x)[2:].zfill(listlength)))
    

    prints out:

    ['0', '0', '0', '0', '0']
    ['0', '0', '0', '0', '1']
    ['0', '0', '0', '1', '0']
    ['0', '0', '0', '1', '1']
    ['0', '0', '1', '0', '0']
    ['0', '0', '1', '0', '1']
    ['0', '0', '1', '1', '0']
    ['0', '0', '1', '1', '1']
    ['0', '1', '0', '0', '0']
    ['0', '1', '0', '0', '1']
    ['0', '1', '0', '1', '0']
    ['0', '1', '0', '1', '1']
    ['0', '1', '1', '0', '0']
    ['0', '1', '1', '0', '1']
    ['0', '1', '1', '1', '0']
    ['0', '1', '1', '1', '1']
    ['1', '0', '0', '0', '0']
    ['1', '0', '0', '0', '1']
    ['1', '0', '0', '1', '0']
    ['1', '0', '0', '1', '1']
    ['1', '0', '1', '0', '0']
    ['1', '0', '1', '0', '1']
    ['1', '0', '1', '1', '0']
    ['1', '0', '1', '1', '1']
    ['1', '1', '0', '0', '0']
    ['1', '1', '0', '0', '1']
    ['1', '1', '0', '1', '0']
    ['1', '1', '0', '1', '1']
    ['1', '1', '1', '0', '0']
    ['1', '1', '1', '0', '1']
    ['1', '1', '1', '1', '0']
    
    0 讨论(0)
提交回复
热议问题