Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
outofmbufs / A258484.py
Last active February 24, 2022 14:39
Compute elements from OEIS A258484 ... https://oeis.org/A258484
from itertools import combinations, chain
from math import prod
def _powA258484(i, n):
"""Return i**n for two integers i and n."""
# NOTE: Putting in these special cases sped up the search
# process enough to be worth it (measured via timeit)
if n == 0:
@outofmbufs
outofmbufs / descriptors.py
Last active March 21, 2022 16:01
python descriptor protocol - simple examples and framework
# miscellaneous descriptor-protocol classes, implemented mostly as an exercise
#
# The MIT License
#
# Copyright (c) 2022 Neil Webber
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
@outofmbufs
outofmbufs / saverestore.py
Created March 28, 2022 22:37
Context manager version of setattr that will do a save/restore (i.e., save previous value, restore it on context exit)
class SetattrThenRestore:
"""Save the old value of an attribute, set it, then restore it.
with SetattrThenRestore(obj, attrname, val):
...bunch of code here...
is SOMEWHAT equivalent to:
oldattrval = getattr(obj, attrname)
setattr(obj, attrname, val)
@outofmbufs
outofmbufs / knightspages.py
Created June 29, 2022 22:07
Knights and Pages puzzle from The Chicken from Minsk
# This implements the "Knights and Pages" problem
# from The Chicken from Minsk, Chapter 1
#
# PROBLEM:
# Many years ago three knights waited to cross the river Neva.
# Each knight had his own page, so there were six people. The
# boat they had could only carry two. However, the knights were
# ferocious killers and the pages were terrified. In fact it was
# certain that any one of the pages would die of heart failure
# if he were not protected at every instant from the other knights
@outofmbufs
outofmbufs / puzzlesolver.py
Created July 7, 2022 16:14
Generic breadth-first puzzle solution search. A generic framework from the knightspages.py gist
# MIT License
#
# Copyright (c) 2022 Neil Webber
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@outofmbufs
outofmbufs / smuggle.py
Created September 26, 2022 16:36
A Smuggle is a key/value pair where ONLY the key (which is read-only) is used to hash and compare. The smuggledvalue rides along without being part of the value of the object. Originally concocted as a hack for working with lru_cache
# The MIT License (MIT)
#
# Copyright (c) 2022 Neil Webber
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@outofmbufs
outofmbufs / A092317.py
Created November 7, 2022 22:42
Generator for OEIS sequence A092317. I don't know why I like making these, but I do. :)
from fractions import Fraction
from math import gcd
# compute the sequence OEIS A092317
# https://oeis.org/A092317
def _slowway(n):
b = Fraction(0)
i = 3
while b < n:
@outofmbufs
outofmbufs / optionalarg.py
Created November 19, 2022 22:08
Python decorator to call a function and omit arguments that need to take on their default values, but only sometimes, so they want to be present in the call syntax but sometimes should be conditionally defaulted (useful if the function sets a non-trivial default value)
import functools
# Given a function such as this:
#
# def example(a, b=17):
# pass
#
# If code needs to conditionally call example() sometimes with b supplied
# and sometimes with b defaulted, that ends up looking like this:
#
@outofmbufs
outofmbufs / nistrandomtests.py
Created February 5, 2023 22:51
small subset of NIST random number tests; see NIST 800-22; NOTE: requires scipy
import math
import random
import functools
import itertools
from collections import Counter
try:
from scipy.special import gammaincc
except ModuleNotFoundError:
def gammaincc(x1, x2):
@outofmbufs
outofmbufs / keyed_lru_cache.py
Created February 16, 2023 21:51
enhance python lru_cache with key capability so it will only use a subset of the arguments for cache lookup
# MIT License
#
# Copyright (c) 2023 Neil Webber
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions: