Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / puzzlesolver.py
Last active November 29, 2024 19:52
Generic breadth-first puzzle solution search. A generic framework from the knightspages.py gist
# MIT License
#
# Copyright (c) 2022,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:
@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 / 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 / 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 / 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 / A046253.py
Created February 21, 2022 16:42
Compute OEIS A046253 sequence. For each digit d in a number n, compute d**d; if they sum to n then n is in the sequence.
from functools import lru_cache
@lru_cache
def _powA046253(i, n):
"""Return i**n for two integers i and n. Defines 0**0 to be ZERO."""
r = i
while n > 1:
r *= i
n -= 1
return r
@outofmbufs
outofmbufs / pymroinpy.py
Created October 1, 2021 19:13
Rewrote the python MRO (C3 method resolution order) in python, just as an exercise in understanding it
# toy example re-implementing C3 method resolution order
# (MRO, as used for python multi-inheritance)
#
# Algorithm snippets in comments come from:
# https://www.python.org/download/releases/2.3/mro/
# and are notated as [2.3MRO]
#
# A Hier represents a class hierarchy, with a name, and a possibly-empty
# list of bases (each base itself also a Hier).
@outofmbufs
outofmbufs / collatz.py
Created July 20, 2021 00:27
Python Collatz Sequence as in OEIS A006577
#!/usr/bin/env python3
def collatz_sequence(n):
"""Generate the collatz sequence for n (see OEIS A006577)."""
c = int(n)
if c < 1 or c != n:
raise ValueError(f"Argument n ({n}) must be a positive integer")
while c != 1: