title: Fix the solution of "Binary period"
body: <p>I found this task and completely stuck with its solution.</p>
  
  <p>A non-empty zero-indexed string S consisting of Q characters is given. The period of this string is the smallest positive integer P such that:</p>
  
  <p>P ≤ Q / 2 and S[K] = S[K+P] for 0 ≤ K &lt; Q − P.</p>
  
  <p>For example, 7 is the period of “abracadabracadabra”. A positive integer M is the binary period of a positive integer N if M is the period of the binary representation of N.</p>
  
  <p>For example, 1651 has the binary representation of "110011100111". Hence, its binary period is 5. On the other hand, 102 does not have a binary period, because its binary representation is “1100110” and it does not have a period.</p>
  
  <p>Consider above scenarios &amp; write a function in Python which will accept an integer N as the parameter. Given a positive integer N, the function returns the binary period of N or −1 if N does not have a binary period.</p>
  
  <p>The attached code is still incorrect on some inputs (9, 11, 13, 17 etc). The goal is to find and fix the bugs in the implementation. You can modify at most <b>2</b> line.</p>
  
  <pre><code>def binary_period(n):
      d = [0] * 30
      l = 0
      while n &gt; 0:
          d[l] = n % 2
          n //= 2
          l += 1
      for p in range(1, 1 + l):
          ok = True
          for i in range(l - p):
              if d[i] != d[i + p]:
                  ok = False
                  break
          if ok:
              return p
      return -1
  </code></pre>