Posts

Showing posts from November, 2017

Pardon My Parsing...

Image
Advent of Code 2016 problems Since they all involve parsing some kind of input data, this will be a job for ... Instaparse ! With it we can use a context-free grammar to generate a tree-like structure from our data. For example, here an input string for the day 4 problem. Each room is represented as a key, id and checksum: aaaaa-bbb-z-y-x-123[abxyz] The key is made of one or more strings of letters separated with hyphens. The id is a string of numbers, and the checksum is a string of letters in brackets. This is how we represent that in EBNF notation... In Clojure!: (def room-parser (insta/parser "room = key id checksum key = (word <separator>)+ <separator> = '-' word = #'[a-zA-Z]+' checksum = <'['> word <']'> id = #'[0-9]+'")) Now we pass the input to our shiny-new parser: (room-parser "aaaaa-bbb-z-y-x-123[abxyz]") And we get our magical tree! Ooowee! => [:...

Programming Beginners Guide

This guide only assumes that you can use a computer. All we're gonna need are 3 things: 1. Web browser - to find and read documentation, 2. Text editor - for writing your source code, 3. Terminal - for entering commands. Depending on your operating system you may need to install Java. You can verify you have it by opening your terminal and typing: java -version The only other thing we need to install is Leiningen , which will take care of everything else. Follow the instructions over there. Download and run the installer and test it out by typing the magic words: lein repl This is where it all happens! Test it out by trying a simple calculation: (+ 1 1) If you've gotten this far, welcome to the future! The power to shape it is yours. In the next post, we'll learn about basic commands.

Theory of Computation - Finite State Automata

Image
An honest effort in tackling the fundamentals of program design must include an understanding of the basics of computation best embodied by automata theory , and our chosen software ecosystem fortunately makes available to us a rich set of visualization tools to assist us in learning these abstract concepts. We will begin this investigation by utilizing a great library called automat , built specifically for this task, which will allow us to use AT&T's  Graph-Visualization  software to turn simple expressions into images to represent models of algorithms. The simplest automaton is a chain of valid inputs, like [1 2 3]. We punch that into automat, and it spits out a nice pretty diagram for us: > ( view [ 1 2 3 ]) And then we can combine them: > ( view ( a/or [ 1 2 3 ] [ 1 3 ])) This represents the union of the two automata, and returns an automaton which will either accept [1 2 3] or [ 1 3] . Game designer Mark Engelberg , creator of i...

Yes, I said instaparse. How to actually use it.

Create a project and add instaparse to your dependencies: [instaparse "1.4.8"]   and at the top of your file:   (ns example.core (:require [instaparse.core :as insta]))   Now we will take the previous example, the problem from day 4 .   It gives us a series of keys like this:   aaaaa-bbb-z-y-x-123[abxyz] a-b-c-d-e-f-g-h-987[abcde] not-a-real-room-404[oarel] totally-real-room-200[decoy]   Which keys are the real rooms? Each key is made up of an encrypted name (lowercase letters separated by dashes) followed by a dash, a sector ID, and a checksum in square brackets. It is valid if the checksum is the five most common letters in the encrypted name, in order, with ties broken by alphabetization. aaaaa-bbb-z-y-x-123[abxyz] is a real room because the most common letters are a (5), b (3), and then a tie between x , y , and z , which are listed alphabetically. a-b-c-d-e-f-g-h-987[abcde] is a real room because although the let...

Meta-Problem #1: Parse the Input Data

One thing every one of these coding problems has in common is you are given some kind of input data in the form of some string of characters. Parsing is defining a grammar that describes that string of characters so it can be interpreted as data that you can poke around and ask questions about. Mark Engelberg's Clojure library, instaparse , will serve all our parsing needs... but that's not all... it also promises to make it FUN! It enables us to describe our data structure's production rules using context-free grammar notation . For example: S = AB* AB = A B A = 'a'+ B = 'b'+" This looks for alternating runs of 'a' followed by runs of 'b'. So for example "aaaaabbaaabbb" satisfies this grammar. On the other hand, "aaabbbbaa" does not (because the grammar specifies that each run of 'a' must be followed by a run of 'b'). So if we feed it the input "aaaaabbbaaaabb"   We will get...

My Interview for Apple

As I am preparing my solutions and tests for the Advent of Code problems to get into Apple, I realize that if I could explain a little bit of how in the world I got into the position of even having a chance to work at such an important company, it can potentially reveal a whole lot about the current state of web technology. I've only been doing this stuff for a couple of years but have managed to make some critical observations and decisions that have given me a major advantage. I want to explain how you could do this too, even more easily than I have. Learning to make abstractions begins with math, but more relevant to the issue here is the way programming is taught, particularly the differences in the rate of adaptation to the technological trends by the tech industry and the computer science education curriculum. A recent example of this is MIT switching from Lisp, a theoretical language invented for AI in 1958, to Python, a modern dynamic scripting language. I love Python an...