arrayspython-3.xbinary

How to generate all possible binary sequences of a set length --given set portions of the sequence


This began as a simple exercise to practice some Python programming, but I have been unable to fix the flaw in my logic!

The goal is simple. Given an array of a predetermined size, I need to build all possible binary permutations of the array size. An additional complication is that specific array indices will be already set, thus avoiding the need to generate a number of permutations.

To give a concrete example:

n=3
arr = [None]*n
arr[0] = 0

The resultant list of binary permutations should be: [0, 0, 0],[0, 0, 1],[0, 1, 0],[0, 1, 1]

My code is returning: [0, 0, 0],[0, 0, 1],[0, 1, 1]

So I can clearly see at which point it is not creating further permutations (as this problem is only exacerbated with larger array sizes). Unfortunately, I do not understand why it not generating that last permutation.

This is the code I am using to generate and print the binary strings:

def arrayPrint(arr, n):  
  
    for i in range(0, n):  
        print(arr[i], end=" ")  
      
    print() 

def binaryStringGen(n, arr, i): 

    if i == n: 
        arrayPrint(arr, n) 
        return
    
    if (arr[i] == None):
        arr[i] = 0
        binaryStringGen(n, arr, i + 1) 

        arr[i] = 1
        binaryStringGen(n, arr, i + 1)
    else:
        binaryStringGen(n, arr, i + 1) 

Any help in understanding this would be greatly appreciated!

  - LDAP: 
      pre: mkdir -p <%= @settings["rootDir"] %>/loot/ldap-domaindump <%= @settings["rootDir"] %>/scans/ldap 
      panes:
        - ldapdomaindump:
          - tmux select-pane -T 'ldapdomaindump' -t "authd:LDAP.0"
          - mkdir -p <%= @settings["rootDir"] %>/loot/ldap-domaindump && cd <%= @settings["rootDir"] %>/loot/ldap-domaindump
          - ldapdomaindump -u '<%= @settings["domain"] %>\<%= @settings["user"] %>' -p '<%= @settings["pass"] %>' $(cat <%= @settings["rootDir"] %>/dcIP.txt | head -n 1)
        - getADusers:
          - tmux select-pane -T 'getADusers' -t "authd:LDAP.1"
          - GetADUsers.py -all '<%= @settings["domain"] %>/<%= @settings["user"] %>:<%= @settings["pass"] %>' -dc-ip $(cat <%= @settings["rootDir"] %>/dcIP.txt | head -n 1) | tee <%= @settings["rootDir"] %>/scans/ldap/getADUsers.txt
        - ldap signing:
          - tmux select-pane -T 'ldap signing' -t "authd:LDAP.2"
          - cd <%= @settings["toolDir"] %>/LdapRelayScan || git clone https://github.com/zyn3rgy/LdapRelayScan.git <%= @settings["toolDir"] %>/LdapRelayScan && cd <%= @settings["toolDir"] %>/LdapRelayScan && echo "git+https://github.com/wbond/oscrypto.git@d5f3437" >> requirements.txt; pipenv run pip install -r requirements.txt --force-reinstall && mkdir -p <%= @settings["rootDir"] %>/scans/ldap && cd <%= @settings["toolDir"] %>/LdapRelayScan && pipenv install --skip-lock && cd <%= @settings["toolDir"] %>/LdapRelayScan; clear
          - pipenv run python3 LdapRelayScan.py -method BOTH -dc-ip $(cat <%= @settings["rootDir"] %>/dcIP.txt | head -n 1) -u '<%= @settings["user"] %>' -p '<%= @settings["pass"] %>' | tee <%= @settings["rootDir"] %>/scans/ldap/ldapSigning.txt
        - get user descriptions:
          - tmux select-pane -T 'get user descriptions' -t "authd:LDAP.3"
          - crackmapexec ldap <%= @settings["rootDir"] %>/dcIP.txt -u '<%= @settings["user"] %>' -p '<%= @settings["pass"] %>' -d <%= @settings["domain"] %> -M get-desc-users | tee <%= @settings["rootDir"] %>/scans/ldap/userDescriptions.txt

Solution

  • You must add arr[i] = None right before your else branch:

        if (arr[i] == None):
            arr[i] = 0
            binaryStringGen(n, arr, i + 1) 
    
            arr[i] = 1
            binaryStringGen(n, arr, i + 1)
            arr[i] = None  #  <= add this
    

    Otherwise when you backtrack from binaryStringGen and later call again binaryStringGen with the same value of i, arr[i] will still be set to 1 and your code will enter the else branch.

    More generally when you're writing recursive functions that have side effects on your arguments be sure that these are properly set back after the function terminates.