This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
OlderNewer