This file contains 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 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 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 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 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 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 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 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 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 |
This file contains 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
# 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). |
This file contains 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
#!/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: |