This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import functools | |
import itertools | |
from collections import Counter | |
try: | |
from scipy.special import gammaincc | |
except ModuleNotFoundError: | |
def gammaincc(x1, x2): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |