Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created November 21, 2022 11:25
Show Gist options
  • Save hsleonis/d487f5d5b11a9947ef7f23da5ba1fa23 to your computer and use it in GitHub Desktop.
Save hsleonis/d487f5d5b11a9947ef7f23da5ba1fa23 to your computer and use it in GitHub Desktop.
Encode and Decode numbers
def encode(num: int):
digits = str(num)
out = 0
prev = 0
for tmp in digits:
tmp = 10 if tmp=='0' else int(tmp)
cur_prev = prev
prev = tmp
if cur_prev <= tmp:
tmp = tmp - cur_prev
else:
tmp = (10 + tmp - cur_prev)%10
out = out*10 + tmp
print(out)
return out
def decode(num: int):
digits = str(num)
out = 0
prev = 0
for tmp in digits:
tmp = int(tmp)
tmp = (tmp+prev)%10
prev = tmp
out = out*10 + tmp
print(out)
return out
print(encode(3435))
print(decode(3192))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment