Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active March 15, 2022 09:30
Show Gist options
  • Save joshuakfarrar/65712f04d1e2d2d2e6cf1ee417f9e4b4 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/65712f04d1e2d2d2e6cf1ee417f9e4b4 to your computer and use it in GitHub Desktop.
para abril, on encoding
import functools
# our goal is to use the format feature of strings to render an array of integers, our encoded message, as a string
## remember character encodings?
# let's load up a fake one
# not a real one, not ascii; a fakeskii
encoding_file = open('zyx-fakeskii', 'r')
fakeskii = encoding_file.read()
encoding_file.close() # ow!
## first we define our core application function
def main(): # that we generally call "main"
# message is our secret message, hidden in numbers! :)
message = [ 73, 32, 108, 111, 118, 101, 32, 65, 98, 114, 105, 108, 33, 32, 72, 97, 112, 112,
121, 32, 86, 97, 108, 101, 110, 116, 105, 110, 101, 39, 115, 32, 68, 97, 121, 33 ]
# if we had to save this message to a physical medium, how might it look? (think: binary)
print(decode(message, encoding=fakeskii))
## then some helper functions
def decode(message, encoding): # this converts [1, 2, 3] to "abc"
# you can think of lambdas as even smaller helper functions
# that have parameters and return values
# in a smaller syntactic package
join = lambda z, a: z + a
prepare_character = lambda char: "{{{0}}}".format(char)
# map and reduce are very good friends
return functools.reduce(join, map(prepare_character, message), "").format(*encoding)
# oh yeah, *encoding converts encoding, which exists as an array of characters, as arguments to the function format
# e.g. "".format(*['a', 'b', 'c', 'A', 'B', 'C']) becomes "".format('a', 'b', 'c', ...)
## let's run our program, shall we?
main()
#
# @@@ debug @@@
#
# "fakeskii" like abril just learned what ascii is!
# fixing fakeskii, sorry, no tests for this :P
#
# hint:
#
# fakeskii[33] = "!"
# print(functools.reduce(lambda z, a: z + a, fakeskii)) # z defaults to the first a
#
# just run `python3.7 winkey-face.py > zyx-fakeskii`, tada 🎉
#
#
# nerd stuff: scoping and "hoisting", keyword arguments, lambda calculus, higher-order functions
#
! ' ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
@joshuakfarrar
Copy link
Author

joshuakfarrar$ python3.7 winkey-face.py
I love Abril! Happy Valentine's Day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment