Skip to content

Instantly share code, notes, and snippets.

class Stack(list):
def push(self, item):
"""Push item on top of stack"""
self.append(item)
def reverse(self, target=None):
"""Destructively reverse self onto another stack
To reverse a stack `s` in place, use `s = s.reverse()`
"""
@yoniLavi
yoniLavi / additive_heterogenous_array.py
Last active June 17, 2021 21:20
A numpy array that supports addition of different types
import numpy as np
class AdditiveHeterogenousArray(np.lib.mixins.NDArrayOperatorsMixin):
"""A numpy array that supports addition of different types"""
def __init__(self, *args, **kwargs):
self.array = np.array(*args, **kwargs)
def __repr__(self):
def incremental_max(l: List[int]) -> List[int]:
if not l:
return []
maxes = [l[0]]
for i in range(1, len(l)):
maxes.append(max(maxes[i-1], l[i]))
return maxes
class Solution:
DAYS = ["1st", "2nd", "3rd"] + [f"{i}th" for i in range(4, 12+1)]
GIFTS = ["a partridge in a pear tree.", "two turtle doves,",
"three French hens,", "four calling birds,",
"five gold rings,", "six geese a-laying,",
"seven swans a-swimming,", "eight maids a-milking,",
"nine ladies dancing,", "ten lords a-leaping,",
"eleven pipers piping,", "twelve drummers drumming,"]
STANZA_TEMPLATE = "On the {} day of Christmas,\nmy true love gave to me\n{}\n"
"""Hyperoperations implementation (https://en.wikipedia.org/wiki/Hyperoperation)"""
from functools import reduce
zeration = lambda a, b: b+1
addition = lambda a, b: reduce(lambda x, _: zeration(0, x), [None]*b, a)
multiplication = lambda a, b: reduce(lambda x, _: addition(a, x), [None]*b, 0)
exponentiation = lambda a, b: reduce(lambda x, _: multiplication(a, x), [None]*b, 1)
tetration = lambda a, b: reduce(lambda x, _: exponentiation(a, x), [None]*b, 1)
pentation = lambda a, b: reduce(lambda x, _: tetration(a, x), [None]*b, 1)
@yoniLavi
yoniLavi / reversible_linked_list.py
Last active November 20, 2018 13:58
A reversible Pythonic LinkedList class
class LinkedList:
def __init__(self, collection):
try:
iterator = iter(collection)
head = next(iterator)
self._value = head
tail = LinkedList(iterator)
self._tail = tail
self._len = tail._len + 1
except StopIteration:
@yoniLavi
yoniLavi / surveymonkeyextract.js
Created November 7, 2018 22:34
Some hacky code to copy an individual respondent's data out of surveymonkey
function extractRespondentAnswers(respondentElem) {
let answers = Array.from(respondentElem.querySelectorAll('.response-text'));
return answers
.map(elem =>
elem.parentElement.parentElement.parentElement.parentElement.parentElement
.querySelector('.response-question-title-text').innerText
+ (elem.previousElementSibling ? ">" + elem.previousElementSibling.innerText : "")
+ '\t' + elem.innerText
).join('\n');
@yoniLavi
yoniLavi / toggleNames.js
Created August 31, 2018 17:24
Toggle names on Class Profiles
// Go to the Class Profiles page in your desktop browser (doesn't matter which one you use):
// https://sisweb.ucd.ie/usis/W_HU_REPORTING.P_DISPLAY_QUERY?p_code1=B036Y1&p_query=OB200-1
// Open the developer tools console, every browser has a slightly different way to open it
// Paste the following into it, and then close it.
// From now on, pressing the Spacebar will toggle the visibiltiy of the names on the page.
// This only applies to that particular session, and will be forgotten when you reopen the page.
$(document).keypress(function(event) {
if (event.keyCode == 32) { // Spacebar
Afghanistan
Albania
Algeria
Angola
Antarctica
Argentina
Armenia
Australia
Austria
Azerbaijan
from functools import lru_cache
@lru_cache(maxsize=1000)
def coincount(coinlist, amount):
if not amount:
return 1
if not coinlist:
return 0