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 / converter.py
Last active December 22, 2015 01:58
Currency converter
from json import load
from re import compile
def validate(query):
# amount, start currency, end currency, .dp
preg = compile(r"(\d+\.?\d*?)\s*?([a-zA-Z]{3})\s*?([a-zA-Z]{3})\s*?(\d*?)")
if preg.match(query):
return [i.upper() for i in preg.split(query) if len(i) > 0]
return False
from random import choice
# s:string, k:key, c:shift characters
e = lambda s,k,c:"".join([c[(c.index(i[1])+ord(k[i[0]]))%len(c)]for i in enumerate(s)])
d = lambda s,k,c:"".join([c[(c.index(i[1])-ord(k[i[0]]))%len(c)]for i in enumerate(s)])
s = "qwertyiop"
#c = list(map(chr,range(65,123))) # [A-Za-z] (chars for shifting)
c = list(map(chr,range(min(map(ord,s)),max(map(ord,s))+1))) # min-max char ord
#k = s[::-1] # reverse the string
@cameronp98
cameronp98 / string_match.py
Last active December 30, 2015 17:49
Returns all strings from the supplied iterable in which the supplied characters are present with the same number of occurrences
def string_match(data, chars):
for item in data:
stack = list(item)
[stack.pop(stack.index(c)) for c in chars if c in stack]
if len(stack) == len(item) - len(chars):
yield item
data = ["happy", "hand", "charming", "this", "horn", "jamming", "choppy"]
chars = "pp"
from collections import defaultdict
def letter_frequency(text):
freq = defaultdict(int)
for char in text.replace("\n", ""):
freq[char] += 1
return freq
def main():
with open("data/lists/bacteria.txt") as f:
def similarity(a, b):
a, b = set(a), set(b)
return len(a.intersection(b)) / len(a.union(b))
print(similarity(["a", "b", "c"], ["a", "c"]))
import re
def slugify(string, delim="-"):
""" Return a (naively) normalized slug of a string"""
words = re.split(r"\W+", string.lower())
return delim.join(words).strip("-")
class Provider(object):
""" A provider holds its publishers alongside their
subscribers and can run the published content to
import unittest
from itertools import cycle
from binascii import hexlify
from os import urandom
# returns a random hex string of length l
# urandom returns 2 bits for every length unit hence the 'l/2'
uniqid = lambda l: hexlify(urandom(round(l/2))).decode()
# '^' = xor (exclusive or): is true when bit a != bit b e.g. 0b10 ^ 0b01 = 0b11
@cameronp98
cameronp98 / router.php
Created January 9, 2014 21:56
Simple (crappy) PHP URI routing system
<?php
class Route {
private $_uri = [];
private $_method = [];
private function add($verb, $uri, $method) {
$this->_uri[] = ["/".trim($uri, "/"), strtoupper($verb)];
if ($method != null) {
$this->_method[] = $method;
@cameronp98
cameronp98 / letters.py
Created January 9, 2014 22:01
Sorting by letter frequency in Python using defaultdict
from collections import defaultdict
def letter_frequency(text):
freq = defaultdict(int)
for char in text.replace("\n", ""):
freq[char] += 1
return freq
def main():
text = "The quick brown fox jumps over the lazy dog"
@cameronp98
cameronp98 / camel-snake.py
Created January 9, 2014 22:01
Convert CamelCase to snake_case in Python
import re
def convert(name):
s1 = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
tests = ["CamelCase", "CamelCamelCase", "getHTTPResponseCode",
"getHTTPResponseCode", "HTTPResponseCodeXYZ"]
for test in tests: