Created
April 26, 2020 19:06
-
-
Save hazurd/4b0f413fb5a02db3a0fd4523a5a8617e to your computer and use it in GitHub Desktop.
Return the length of a constant loop given some conditions, an ID, and a given base
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, b): | |
k = len(n) | |
xc = sorted(n, reverse=True) | |
yc = sorted(n) | |
x = ''.join(xc) | |
y = ''.join(yc) | |
zc = int(x, base=b) - int(y, base=b) | |
z = str_base(zc, b) | |
if z in a: | |
return len(a) - a.index(z) | |
else: | |
a.append(z) | |
return solution(z, b) | |
def digit_to_char(digit): | |
if digit < 10: | |
return str(digit) | |
return chr(ord('a') + digit - 10) | |
def str_base(number, base): | |
if number < 0: | |
return '-' + str_base(-number, base) | |
(d, m) = divmod(number, base) | |
if d > 0: | |
return str_base(d, base) + digit_to_char(m) | |
return digit_to_char(m) | |
a = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment