Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
PM2Ring / GeodesicTriangulate.py
Last active January 4, 2018 10:59
Geodesic distance triangulation: Find two locations that are at given distances from two fixed points, using the WGS84 reference ellipsoid
#! /usr/bin/env python
''' Geodesic distance triangulation
Find two locations that are at given distances
from two fixed points, using the WGS84 reference ellipsoid
First approximate using spherical trig, then refine
using Newton's method.
@PM2Ring
PM2Ring / allfactors.py
Created October 19, 2016 14:04
Generate all factors of a number
#!/usr/bin/env python3
''' Generate all factors of m
Written by PM 2Ring 2016.10.20
'''
import sys
from itertools import product
from functools import reduce
@PM2Ring
PM2Ring / numpy_life.py
Created November 2, 2016 15:52
Simple Game of Life engine using Numpy
#!/usr/bin/env python3
''' Game of Life in Numpy
Written by PM 2Ring 2016.11.03
'''
import numpy as np
from time import sleep
@PM2Ring
PM2Ring / zbar_demo.py
Created November 10, 2016 17:35
Simple demo of using the Python 2 zbar module to decode QR codes / barcodes
#!/usr/bin/env python
''' zbar demo
Handles local files and URLs
Not that PIL requires ImageMagick or GraphicsMagick to display images
Written by PM 2Ring 2016.11.11
'''
@PM2Ring
PM2Ring / bitcount_speed_tests.py
Created December 14, 2016 11:14
Speed tests of various functions for counting the '1' bits in a 32 bit integer in Python 2 or Python 3
#!/usr/bin/env python3
''' Count bits in a 32 bit Python integer
Speed tests of various implementations, mostly
from http://stackoverflow.com/questions/9829578/fast-way-of-counting-bits-in-python
Some of these functions work on arbitrary input, but some are specifically for
32 bit integers, see the function sources for detail.
@PM2Ring
PM2Ring / CRT_demo.py
Created December 16, 2016 10:50
Chinese Remainder Theorem demo
#!/usr/bin/env python3
''' Solve systems of simultaneous linear congruences using the Chinese Remainder Theorem
Chinese Remainder Theorem:
If X = r_i mod m_i for i in 1..n, m_i pairwise coprime
Let
M_i = M // m_i, y_i = M_i^(-1) mod m_i, i.e., y_i * M_i = 1 mod m_i
Then
y_i * M_i = 0 mod m_j for j != i
@PM2Ring
PM2Ring / sum_digits_speed_test.py
Created December 19, 2016 10:31
Digit sum speed tests
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' Sum the digits of a non-negative integer
Speed tests of various implementations
Written by PM 2Ring & hiro protagonist 2016.12.19
sum_digits_hiro is faster than sum_digits_pm_map until digits=20
@PM2Ring
PM2Ring / word_squares.py
Created January 20, 2017 11:28
A recursive word square generator
#!/usr/bin/env python3
''' Generate word squares
Written by PM 2Ring 1999.01.18
Translated from C to Python 2017.01.20
'''
import sys
from bisect import bisect_left
@PM2Ring
PM2Ring / alphagrams.py
Created April 5, 2017 15:18
Alphagram Maker
#!/usr/bin/env python3
''' Transform words into 1-based indices of their letters in alphabetical order.
Eg 'APPLE' gets these indices: [(1, 'A'), (2, 'E'), (3, 'L'), (4, 'P'), (5, 'P')]
So we encode 'APPLE' as (1, 4, 5, 3, 2)
See http://stackoverflow.com/questions/43233004/regex-expression-to-find-words-where-you-know-the-alphabetical-order-of-the-lett
Written by PM 2Ring 2017.04.06
@PM2Ring
PM2Ring / flip.py
Created April 16, 2017 15:06
Flip - an infuriating XOR-based game for Python 2, using GTK2+
#!/usr/bin/env python2
''' Classic Flip game, with Help function & selectable goal
Written by PM 2Ring 2007.12.22
Updated 2011.08.07
'''
import pygtk
pygtk.require('2.0')
import gtk, sys, random