Skip to content

Instantly share code, notes, and snippets.

View sweeneyde's full-sized avatar
😎

Dennis Sweeney sweeneyde

😎
  • The Ohio State University
  • Columbus, Ohio
View GitHub Profile
@sweeneyde
sweeneyde / monoid_homology.py
Last active March 8, 2024 03:56
Monoid homology calculator
"""
Monoid Homology Calculator
written by Dennis Sweeney
Given a monoid M, we can produce a topological space (cw complex) BM,
whose n-cells are n-tuples of non-identity elements of M.
This program computes the integral homology of the space BM.
The problem of deciding whether two words are equal is not computable,
so we restrict to nice classes of monoid presentations. In particular,
@sweeneyde
sweeneyde / smith.py
Last active July 21, 2023 20:11
Smith Normal Form Calculator
class Matrix:
def __init__(self, arr, n=None):
if isinstance(arr, Matrix):
self.arr = [list(row) for row in arr.arr]
self.m = arr.m
self.n = arr.n
else:
self.arr = [list(row) for row in arr]
self.m = len(arr)
if not arr:
@sweeneyde
sweeneyde / dragonturtle.py
Created September 25, 2022 04:40
try it...
from turtle import *
def turns():
s = []
while True:
yield 90
fold = [-x for x in reversed(s)]
yield from fold
s += [90] + fold
hideturtle()
speed(0)
@sweeneyde
sweeneyde / make_change.py
Last active August 31, 2022 23:23
Find the number of ways to make n cents of change with (c1, ..., cn)-cent coins.
"""
Find the number of nonnegative integer solutions (x_1, ..., x_n)
to the equation a_1*x_1 + ...+ a_k*x_k = n,
where each a_i and n are positive integers.
Example: to find the number of ways to make $10.00 out of
1-, 5-, 10-, and 25-cent coins:
>>> change(1000, (1, 5, 10, 25))
142511
@sweeneyde
sweeneyde / lazier_product.py
Created March 22, 2021 10:29
A less-greedy implementation of itertools.product: consume iterators lazily.
class LazyProductObject:
# Assume there are n >= 1 iterators, with repeat = r.
# len(pools) == n and len(indices) == len(result) == n*r
# pools[0:num_incomplete_pools] are lists of items that will still
# get added to from iterators, while pools[num_incomplete_pools:n]
# are finished and don not need to be re-modified.
# If repeat >= 2:
# then the pools will fill up as we iterate the first time
# over all possible result[(n-1)*r:n*r]
# If repeat == 1:
@sweeneyde
sweeneyde / lots_of_benches.py
Created October 18, 2020 21:28
String search benchmarks
This file has been truncated, but you can view the full file.
needles = ['H', 'F', 'C', 'E', 'D', 'G', 'A', 'B', 'L', 'U', 'AA', 'BA', 'AB', 'IB', 'EA', 'AD', 'AQ', 'BS', 'SA', 'AK', 'HA', 'GI', 'AP', 'FP', 'IN', 'FG', 'DQN', 'ABJ', 'GKC', 'AAD', 'IDM', 'BAB', 'JAB', 'CBD', 'AEC', 'CDB', 'AXA', 'BTG', 'RBC', 'VAC', 'YCB', 'AAZ', 'KAG', 'BHA', 'BFC', 'BAD', 'DCHU', 'BCIC', 'AASX', 'AAUU', 'AEDF', 'AAAA', 'HAQB', 'CBCA', 'AAUA', 'IBDA', 'BAKJ', 'AACC', 'BBEC', 'AABG', 'CAJC', 'EOCA', 'BAPL', 'ACAP', 'ADFL', 'HBCL', 'LABVF', 'EHRIR', 'AADSH', 'CCIBL', 'MEAKB', 'RIABB', 'BFHBA', 'GDAAN', 'AALCH', 'TABAH', 'RDAAH', 'BNAZA', 'BBCMB', 'AALGN', 'AAPWK', 'BYABD', 'MGBCK', 'XFFCH', 'ABAAX', 'BACDB', 'AVAAHA', 'DMPAXA', 'ACOSQF', 'ADNCBN', 'BLCDXA', 'AAHPGA', 'AFCBAD', 'CFTUBF', 'BDQAEB', 'AAABCC', 'LTHEEG', 'YJTCIA', 'BDAFBI', 'BBFBAG', 'FAAEBA', 'DBRBKI', 'EFHFIH', 'BHACAO', 'AAGDVD', 'PBDLOA', 'LPACABC', 'ATAJEEN', 'GBMXAAM', 'AALMCBY', 'AMABEQA', 'FFDAOCR', 'AABCDTC', 'LAAPAFA', 'EBHADDA', 'FWABCAM', 'DBJOIAF', 'IADNCAB', 'ABCDBAT', 'EAAAAIC', 'BCARAAT', 'TIGOEAC', 'ABACJKG',