title: Python Hamming distance rewrite countless for cycles into recursion
body: <p>I have created a code generating strings which have hamming distance n from given binary string. Though I'm not able to rewrite this in a simple recursive function. There are several sequences (edit: actually only one, the length change) in the for loops logic but I don't know how to write it into the recursive way (the input for the function is string and distance (int), but in my code the distance is represented by the count of nested for cycles. Could you please help me?</p>
  
  <p>(e.g. for string <code>'00100'</code> and distance 4, code returns <code>['11010', '11001', '11111', '10011', '01011']</code>,
  for string <code>'00100'</code> and distance 3, code returns <code>['11000', '11110', '11101', '10010', '10001', '10111', '01010', '01001', '01111', '00011']</code>)</p>
  
  <pre><code>def change(string, i):
      if string[i] == '1':
          return string[:i] + '0' + string[i+1:]
      else: return string[:i] + '1' + string[i+1:] #'0' on input
  
  
  def hamming_distance(number):
  
      array = []
      for i in range(len(number)-3): #change first bit
          a = number
          a = change(a, i) #change bit on index i
          for j in range(i+1, len(number)-2): #change second bit
              b = a
              b = change(b, j)
              for k in range(j+1, len(number)-1): #change third bit
                  c = b
                  c = change(c, k)
                  for l in range(k+1, len(number)): #change fourth bit
                      d = c
                      d = change(d, l)
                      array.append(d)
      return array
  
  print(hamming_distance('00100'))
  </code></pre>
  
  <p>Thank you!</p>