Skip to content

Instantly share code, notes, and snippets.

View DomNomNom's full-sized avatar

DomNomNom DomNomNom

View GitHub Profile
@DomNomNom
DomNomNom / RpcClient.js
Created September 14, 2017 11:12
Hiding a private key by using JS scopes and closures
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 {
@DomNomNom
DomNomNom / twice_as_shifty.py
Created July 25, 2017 11:48
Twice as shifty
# 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)
@DomNomNom
DomNomNom / ZigZag.js
Last active July 9, 2017 03:02
Count the number of zig zags in an array (switches from increasing to decreasing or from decreasing to increasing)
// 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) {
@DomNomNom
DomNomNom / asciiClock.py
Created May 28, 2016 16:56
Prints the current time in nice ascii art. Clean code using numpy
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)))
@DomNomNom
DomNomNom / X.py
Last active April 28, 2016 01:37
"""
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:'.
@DomNomNom
DomNomNom / circularAverage.py
Last active April 10, 2016 11:17
Programming puzzle: Average direction on a circle
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)
@DomNomNom
DomNomNom / powerset.py
Last active April 5, 2016 21:48
Two ways of writing powerset which produce the same ordering. Have a go at proving it.
def powerset(list):
if not list:
yield set()
return
*rest, last = list
yield from powerset(rest)
for subset in powerset(rest):
yield subset | {last}
@DomNomNom
DomNomNom / UnweightedDijkstras.py
Last active September 9, 2015 03:37
Searches a graph and returns the shortest (unweighted) path to the goal
# 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:
#!/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()))
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')