Created
September 17, 2020 01:30
-
-
Save grevych/e63cc48dfe00fd038043c599b7b560fa to your computer and use it in GitHub Desktop.
Find if a binary representation of an integer is a repeated (maybe incomplete) substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(n): | |
d = [0] * 30 | |
l = 0 | |
while (n > 0): | |
d[l] = n % 2 | |
n //= 2 | |
l += 1 | |
for p in range(1, l - 1): | |
ok = True | |
print(d[:l-p]) | |
for i in range(l - p): | |
print(i, p, d[:i + p + 1]) | |
if d[i] != d[i + p]: | |
ok = False | |
break | |
if ok and l - p > p: | |
return p | |
return -1 | |
print(solution(955)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment