Skip to content

Instantly share code, notes, and snippets.

@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 / loadmultijson.py
Last active October 18, 2021 17:28
python function to decode multiple JSON representations in a single file (stream)
# The MIT License
#
# Copyright (c) 2019 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 / 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:
@outofmbufs
outofmbufs / iftttmaker.ino
Last active May 21, 2021 21:44
Arduino code to trigger an IFTTT/Maker event
// The MIT License
//
// Copyright (c) 2015 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 / keithnum.py
Created April 7, 2021 17:56
Generate keith numbers - OEIS A007629
def iskeith(n):
"""Return True if n is a Keith number - https://oeis.org/A007629"""
if n < 10:
return False # by definition
# initial sequence seeding is the digits of n
kl = [int(d) for d in str(n)] # crude, but easy. Speed not relevant.
ksum = sum(kl)