Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The joy of reinvention solution in Clear category for Hacker Language by veky
import string, io, re
special, space = string.digits + '.:!?@$%', str(10**6)
def eat(sio, patt):
position = sio.tell()
regex = re.compile(re.escape(patt).replace('d', '\d').replace('b', '[01]'))
candidate = sio.read(len(patt))
if regex.fullmatch(candidate): return candidate
else: sio.seek(position)
def encrypt(char):
if char in special: return char
elif char == ' ': return space
else: return format(ord(char), '06b')
def decrypt(code):
sio = io.StringIO(code)
while ...:
for pattern in ['dd.dd.dddd', 'dd:dd', space, 'b'*7, *special]:
candidate = eat(sio, pattern)
if candidate:
if pattern == space: yield ' '
elif pattern == 'b'*7: yield chr(int(candidate, 2))
else: yield candidate
break
else: break
class HackerLanguage:
def __init__(self): self.text = ''
def write(self, text): self.text += text
def delete(self, N): self.text = self.text[:-N]
def send(self): return ''.join(map(encrypt, self.text))
@staticmethod
def read(code): return ''.join(decrypt(code))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
message_1 = HackerLanguage()
message_1.write("secrit")
message_1.delete(2)
message_1.write("et")
message_2 = HackerLanguage()
assert message_1.send() == "111001111001011100011111001011001011110100"
assert message_2.read("11001011101101110000111010011101100") == "email"
print("Coding complete? Let's try tests!")
July 13, 2018
Comments: