Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Q solution in Clear category for Cipher Dict by pokosasa
from collections import deque
def get_cipher(plain):
digits = str(sum(n * 256**i for i, n in enumerate(plain.encode()[::-1])))
res = A = {}
que = deque()
prev_d, order = -1, +1
for d in digits:
d = int(d)
if d == prev_d or (d - prev_d) // abs(d - prev_d) != order:
order *= -1
A = que.popleft()
A[d] = {}
que.append(A[d])
prev_d = d
return res
if __name__ == '__main__':
print("Example:")
print(get_cipher("hello"))
# These "asserts" are used for self-checking and not for an auto-testing
assert get_cipher("hello") == {4: {4: {8: {3: {7: {0: {4: {}, 7: {}}, 2: {2: {}}}, 8: {3: {}}}}}}}
assert get_cipher("This is a plain text.") == {1: {3: {2: {1: {4: {7: {4: {1: {0: {}, 2: {}, 7: {}},4: {9: {}}, 8: {3: {}}},
6: {8: {1: {}, 4: {}, 9: {}}}}}, 9: {5: {5: {6: {1: {}, 4: {}}, 8: {}}}}}}}},
2: {6: {2: {8: {4: {9: {5: {2: {}, 8: {}}}}}}}}, 3: {2: {4: {1: {4: {8: {3: {0: {},
2: {}}}}}, 3: {5: {3: {5: {4: {}, 7: {}}}}}}}}}
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 31, 2021
Comments: