This file contains hidden or 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
const rp = require('request-promise-native'); | |
const crypto = require('crypto'); | |
const assert = require('assert'); | |
const signatureMethod = 'RSA-SHA256' | |
const protocolVersion = '0.0.1'; | |
const signatureEncoding = 'base64'; | |
class RpcClient { |
This file contains hidden or 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
# Find a number n which is_twice_as_shifty(n) | |
def is_twice_as_shifty(n): | |
n = int(n) | |
if n < 1: | |
return False | |
n_str = str(n) | |
n_times_two_str = n_str[-1] + n_str[:-1] # Shift the digits around | |
n_times_two = int(n_times_two_str) |
This file contains hidden or 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
// Counts how often a sequence zig zags. | |
// ie. switches from increasing to decreasing or from decreasing to increasing | |
// countZigZags([]) --> 0 | |
// countZigZags([5,5,5,5]) --> 0 | |
// countZigZags([1,2,3]) --> 0 | |
// countZigZags([1,2,3,2,1]) --> 1 | |
// countZigZags([5,6,5,6,5]) --> 3 | |
// countZigZags([2,2,2,6,6,5,5]) --> 1 | |
function countZigZags(numbers) { |
This file contains hidden or 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 numpy as np | |
charWidth = 3 | |
char2index = { c : i*charWidth for i,c in enumerate('0123456789: ')} | |
bigAlphabet = ''' | |
_ _ _ _ _ _ _ _ | | |
| | | _| _||_||_ |_ ||_||_| . | | |
|_| ||_ _| | _||_| ||_| | . | | |
'''[1:-1].split('\n') | |
bigAlphabet = np.array(list(map(list, bigAlphabet))) |
This file contains hidden or 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
""" | |
The main use case of this is to replace either | |
>>> [ x * (x + 1) for x in [1,2,3] ] | |
or | |
>>> map(lambda x: x * (x + 1), [1,2,3]) | |
with | |
>>> map(X * (X + 1), [1,2,3]) #SWAG | |
You can naively think of it as 'lambda X:' without writing 'lambda X:'. |
This file contains hidden or 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 enforce0to360range(angle): | |
"""Converts the given angle (degrees) into the range [0 360). | |
>>> enforce0to360range(30) | |
30.0 | |
>>> enforce0to360range(180) | |
180.0 | |
>>> enforce0to360range(360) | |
0.0 | |
>>> enforce0to360range(390.0) |
This file contains hidden or 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 powerset(list): | |
if not list: | |
yield set() | |
return | |
*rest, last = list | |
yield from powerset(rest) | |
for subset in powerset(rest): | |
yield subset | {last} |
This file contains hidden or 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
# returns the shortest path (measured in the number of edges) from start-->goal. | |
# if no path exists None is returned | |
def unweightedDijkstra(graph, start, goal): | |
if goal is start: # special case | |
return (start,) | |
q = [(start,)] # q is a queue of paths (tuples of nodes) | |
visited = set() | |
while q: |
This file contains hidden or 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
#!/usr/bin/python3 -i | |
# Hello there! | |
# The following line of code creates a mystery object which behaves in really weird ways. | |
# Have fun trying to figure it out. | |
# :Dom | |
# | |
# PS: This concept came from a Kiwi Pycon 2015 talk. | |
mystery = next(filter(lambda _:'.'in repr([_])and not(list!= type(_) or _),['%s'%{}]+__import__('gc').get_objects())) |
This file contains hidden or 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 bs4 import BeautifulSoup as bs | |
import urllib.request as request | |
import urllib.parse as parse | |
import pprint | |
# Selects sections of html that we are interested in | |
def prepare(html): | |
soup = bs(html, 'html.parser') |