Skip to content

Instantly share code, notes, and snippets.

@fahnub
Last active June 20, 2023 04:50
Show Gist options
  • Save fahnub/e311fc2349000b255106f06e178d277b to your computer and use it in GitHub Desktop.
Save fahnub/e311fc2349000b255106f06e178d277b to your computer and use it in GitHub Desktop.
Foobar Level 2 Problem 2 (hey I already did that)
def order(n):
return int(''.join(sorted(n, reverse=True))), int(''.join(sorted(n)))
def subtract(n, b):
k = len(n)
x, y = order(n)
difference = ""
borrow = 0
while x > 0:
tmp = (x % 10) - (y % 10) + borrow
if tmp < 0:
tmp += b
borrow = -1
else:
borrow = 0
difference += str(tmp)
x //= 10
y //= 10
return (k - len(difference)) * '0' + difference[::-1]
def solution(n, b):
ids = []
while True:
z = subtract(n, b)
if z in ids:
break
else:
ids.append(z)
n = z
return len(ids) - ids.index(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment