Skip to content

Instantly share code, notes, and snippets.

View pepasflo's full-sized avatar

Jason Pepas (FloSports.tv) pepasflo

View GitHub Profile
@pepasflo
pepasflo / README.md
Last active October 2, 2023 16:23
Notes on using graphviz to visualize abstract syntax trees (ASTs)

Using graphviz to visualize abstract syntax trees

screen shot 2019-02-15 at 5 52 08 pm

  • After you've written your first lexer and parser, write a function which walks the AST and "compiles" it into graphviz notation like the above
@pepasflo
pepasflo / gist:9babaf9a3d955229a315663a7967b355
Created February 1, 2019 20:24
speed comparison of md5 and crc32 (python)
jpepas@radium$ cat do-md5.py
#!/usr/bin/env python
import sys
import hashlib
h = hashlib.md5()
with open(sys.argv[1], 'rb') as f:
# thanks to https://stackoverflow.com/a/3431838/7543271
for chunk in iter(lambda: f.read(4096 * 64), b""):
h.update(chunk)
print h.hexdigest()
@pepasflo
pepasflo / queue.py
Created January 31, 2019 16:46
A trivial queue data structure, written in Python.
#!/usr/bin/env python
class LinkedListNode(object):
def __init__(self, value, next=None):
self.value = value
self.next = None
class Queue(object):
def __init__(self):
self.head = None
@pepasflo
pepasflo / check.py
Last active January 31, 2019 00:14
Interview Cake problem: verify a binary search tree https://www.interviewcake.com/question/python/bst-checker
#!/usr/bin/env python
# Intervie Cake problem: verify a binary search tree.
# See https://www.interviewcake.com/question/python/bst-checker
class BinarySearchTree(object):
def __init__(self, value):
self.value = value
self.left = None
@pepasflo
pepasflo / superbalanced.py
Last active January 24, 2019 17:37
Interviewcake problem: is a tree "superbalanced"? https://www.interviewcake.com/question/python/balanced-binary-tree
#!/usr/bin/env python
class BinaryTreeNode(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert_left(self, value):
#!/usr/bin/env python
# does this linked list have a cycle?
# https://www.interviewcake.com/question/python/linked-list-cycles?__s=rf71cpqpqpezb7dgts5r
class ListNode:
next = None
def has_loop(l):
seen = set()
@pepasflo
pepasflo / bad-coloring.json
Last active December 14, 2018 21:04
A python script to verify a graph coloring. This is to aid in solving https://www.interviewcake.com/question/python/graph-coloring
[
[1,1],
[2,1],
[3,3]
]
@pepasflo
pepasflo / gen-units.py
Created December 6, 2018 16:44
Python script to generate a units.js file for quickqwerty https://github.com/susam/quickqwerty
#!/usr/bin/env python
# generate a "Units.js" file for quickqwerty.
# see https://github.com/susam/quickqwerty
import sys
import random
def gen_subunit_str(chars):
words = []
@pepasflo
pepasflo / C char frequency (Linux kernel)
Last active November 28, 2018 23:19
Find the frequency of characters in Swift source files.
./charfreq.py c linux-4.19.5
' ' 9.38%
e 5.59%
t 4.79%
_ 4.57%
\t 4.30%
r 3.82%
\n 3.68%
i 3.64%
s 3.41%
@pepasflo
pepasflo / c_output.txt
Last active November 15, 2018 17:22
programming puzzle: matching parens.
$ gcc -O3 -std=c99 -Wall parens.c
$ ./a.out parens-tests/100k.txt
answer: 1
elapsed time: 13.773000ms
$ ./a.out parens-tests/1million.txt
answer: 1
elapsed time: 125.703000ms