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 | |
# | |
# A "sieve of Eratosthenes" for integer vectors of length N. | |
# | |
# The original usage of this was for studying pythagorean triples and | |
# filtering out those that were simply multiples of smaller ones, e.g. | |
# | |
# (5, 12, 13) as a "fundamental" Pythagorean triple vs | |
# (10, 24, 26) as a multiple of (5, 12, 13) | |
# |
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 | |
// | |
// 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: |
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 | |
# | |
# 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: |
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
# Python support for argument parsing of list-like things (usually comma separated). | |
# For example: | |
# def commapair(s): | |
# return argtuple(s, n=2) | |
# can then be used as: | |
# type=commapair | |
# in argparse for arguments like "--blah 17,42" | |
# | |
def argtuple(s, *dflts, n=None, seps=(',',), type=int): | |
"""Return tuple of 'type' from a,b,c,d... format. |
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
import functools | |
__IGX_SENTINEL = object() # sentinel for a "default argument" see below | |
def IgnoreExceptionsDecorator(excepts, *, exception_result=__IGX_SENTINEL): | |
"""Decorator - catch exceptions and convert to a return value. | |
'excepts' - exceptions (as a tuple, or a single exception) to catch |
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
import functools | |
from collections import ChainMap | |
def tuplify(arg, *, keep=str): | |
"""Return tuple from an iterable or a naked single object. | |
If arg is a non-iterable object, returns: (arg,) | |
If arg is iterable, returns: tuple(arg) |
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
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) |
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: |
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
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 |
OlderNewer