Skip to content

Instantly share code, notes, and snippets.

@altilunium
Created November 27, 2021 16:38
Show Gist options
  • Save altilunium/b0443ae5fcbb111e8062531b632e3b7b to your computer and use it in GitHub Desktop.
Save altilunium/b0443ae5fcbb111e8062531b632e3b7b to your computer and use it in GitHub Desktop.
i_already_did_that.py
def split(word):
return [char for char in word]
def join(x):
new = ""
for i in x:
new += i
return new
def neg(x,y,b):
sol = y
digi = len(x) - 1
flag = False
while digi >= 0 :
cx = int(x[digi])
cy = int(y[digi])
if flag:
cx = cx - 1
flag = False
if cx >= cy:
sol[digi] = str(cx - cy)
else:
flag = True
cx = cx + b
sol[digi] = str(cx - cy)
digi = digi - 1
return sol
def solution(n,b):
dictofid = dict()
bagofid = set()
bagofid.add(n)
dictofid[n] = 1
#print(n)
k = len(n)
x = split(n)
y = split(n)
x.sort(reverse=True)
y.sort()
nextid = join(neg(x,y,b))
bagofid.add(nextid)
dictofid[nextid] = 2
#print(nextid)
count = 2
while True:
k = len(nextid)
x = split(nextid)
y = split(nextid)
x.sort(reverse=True)
y.sort()
nextid = join(neg(x,y,b))
#print(nextid)
if nextid in bagofid:
return (count - dictofid[nextid]) + 1
else:
bagofid.add(nextid)
count = count + 1
dictofid[nextid] = count
return count
print(solution('1211',10))
print(solution('210022',3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment