Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cipher Dict by liuq901
def get_cipher(plain):
data = plain.encode('utf-8')
value = int.from_bytes(plain.encode('utf-8'), 'big')
ans = [{}]
prev = None
up = True
idx = 0
for x in str(value):
if prev is not None:
if up and prev < x or not up and prev > x:
pass
else:
idx += 1
up = not up
prev = x
ans.append({})
ans[idx][int(x)] = ans[-1]
return ans[0]
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!")
Dec. 13, 2021
Comments: