Created
November 21, 2022 11:25
-
-
Save hsleonis/d487f5d5b11a9947ef7f23da5ba1fa23 to your computer and use it in GitHub Desktop.
Encode and Decode numbers
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 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