Created
October 30, 2015 21:04
-
-
Save bsidhom/17822e89a1cd919fd1b9 to your computer and use it in GitHub Desktop.
Yo coding. Turn a message into a sequence of 'yo's and 'yoyo's. Messages are encoded with ASCII and concatenated into a bit string. Each '1' is encoded as a 'yo' and each '0' encoded as a 'yoyo'.
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
from itertools import zip_longest | |
def encode(message): | |
codepoints = list(map(ord, message)) | |
binary_strings = list(map(lambda x: '{0:08b}'.format(x), codepoints)) | |
binary_joined = ''.join(binary_strings) | |
binary_spaced = binary_joined.replace('', ' ').strip() | |
return binary_spaced.replace('1', 'yo').replace('0', 'yoyo') | |
def decode(message): | |
binary_string = message.replace('yoyo', '0').replace('yo', '1').replace(' ','') | |
binary_strings = map(lambda x: ''.join(x), grouper(binary_string, 8)) | |
codepoints = map(lambda x: int(x, 2), binary_strings) | |
return ''.join(map(chr, codepoints)) | |
def grouper(iterable, n, fillvalue=None): | |
"Collect data into fixed-length chunks or blocks" | |
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" | |
args = [iter(iterable)] * n | |
return zip_longest(*args, fillvalue=fillvalue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment