Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
outofmbufs / tokutil.py
Created January 23, 2024 04:26
add unget, peek, mark/accept functionality to a stream of tokens
# MIT License
#
# Copyright (c) 2024 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 / gist:2117ba3435eaf75003af6b4d9ce0441a
Created July 4, 2023 19:17
OEIS A109732 and (related) A261414
# 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:
@outofmbufs
outofmbufs / OEIS_A059405.py
Last active June 4, 2023 02:38
Python search algorithm for OEIS integer sequence A059405
# 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:
@outofmbufs
outofmbufs / nytdigits.py
Last active April 23, 2023 03:38
Solver for NYTimes Digits games. Use with my puzzlesolver gist. EDIT: also handles chain mode now.
# 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:
@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:
@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 / 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 / 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
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: