Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
PM2Ring / knight_tour1.py
Created May 26, 2018 16:50
Knight tour, using a generator
#!/usr/bin/env python3
''' Knight tour, using recursive backtracking
Version using a generator in a class
Written by PM 2Ring 2018.05.27
'''
class Board:
def __init__(self, size):
@PM2Ring
PM2Ring / knight_tour.py
Created May 25, 2018 13:28
Knight tour, using recursive backtracking
#!/usr/bin/env python3
''' Knight tour, using recursive backtracking
Written by PM 2Ring 2018.05.25
'''
moves = ((-2, -1), (-2, 1), (-1, -2), (-1, 2),
(1, -2), (1, 2), (2, -1), (2, 1))
@PM2Ring
PM2Ring / Fenwick_demo.py
Created January 2, 2018 20:51
A Python version of the Fenwick tree demo code from the Wikipedia article.
#! /usr/bin/env python3
''' Demo of the Fenwick tree, aka Binary Indexed Tree
See https://en.wikipedia.org/wiki/Fenwick_tree
Converted from C to Python by PM 2Ring
'''
def LSB(i):
@PM2Ring
PM2Ring / random_bits_speed_test.py
Created December 9, 2017 11:06
Test speeds of various ways of creating a list of random bits
#!/usr/bin/env python3
''' Test speeds of various ways of creating a list of random bits
Typical results on a 32bit 2GHz machine running Python 3.6.0 on Linux
null : [0.04344886299986683, 0.04375551599878236, 0.04837666000094032]
zipgen : [0.28622414100027527, 0.28712629300025583, 0.2921328890006407]
nextgen : [0.363577712998449, 0.3746541500004241, 0.3797654469999543]
randrange : [1.342143091998878, 1.3589175790002628, 1.3717609110008198]
@PM2Ring
PM2Ring / acot_triangle.svg
Created December 4, 2017 11:29
Show that arccot(2)+arccot(3)=arccot(1)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PM2Ring
PM2Ring / cbg-printer0.rle
Created November 22, 2017 12:11
Game of Life "cbg" printer in RLE format
x = 632, y = 391, rule = B3/S23
265b2o$265bo$257b2o3b2obo$256bo3bo3bo$240b2o13bo$240b2o13bo3bo2bo$231b
2o3bo6b2o10bo5bo$231bobo3bo5b3o10bo3bo4b2o12b2o$232b5o6b2o12b2o6bo13b
2o$233b3o4b2o9bobo9bobo$240b2o10b2o9b2o$252bo24bo$259b2o17bo$258bobo
17bo$260bo2$276b2o3b2o51b2o$279bo54bo$248bo27bo5bo40b2o7bobo$248b2o5b
2o20b2ob2o41bobo6b2o$247bobo5b2o21bobo30bo12b3o$279bo28b4o3b2o8b3o$
279bo20b2o5b4o3b2o8b3o$300b2o5bo2bo3b2obob2o2bobo8b2o12b2o$307b4o4bobo
b2o2b2o9bo13b2o$308b4o3bob2o13bobo$240b2o69bo20b2o$241b2o$240bo34bobo$
275b2o70b3o$276bo3b3o39b2o2b2o19b3o$266b2o11bo3bo37bo3b2o19bo3bo$266b
2o10bo5bo37b2obo77b2o$233bo44b2obob2o60b2o3b2o51bo$233b2o159b2o5bobo$
@PM2Ring
PM2Ring / circular_shift.py
Created October 31, 2017 17:20
Detect if a list is a circular permutation of a given list
#!/usr/bin/env python3
''' Detect if a list is a circular permutation of a given list
Written by PM 2Ring 2017.11.01
'''
from random import seed, randrange
seed(42)
@PM2Ring
PM2Ring / circles.py
Created October 26, 2017 15:19
Apollonian gasket in GTK2+
#! /usr/bin/env python
''' Draw an Apollonian gasket. Non symmetrical version.
Recursively solves Descartes Theorem to find the circles tangent
to 3 circles that are tangent to each other.
Written by PM 2Ring 2008.09.14
'''
import sys, colorsys
@PM2Ring
PM2Ring / combosums.py
Last active October 15, 2017 03:07
Find combinations of integers from 3 lists that sum to the items in a target list
#!/usr/bin/env python3
''' Find combinations of integers from 3 lists that sum to the items in a target list
https://gist.github.com/PM2Ring/f051ab09f9074ab5a378fa30f4141352
Written by PM 2Ring 2017.10.15
'''
from itertools import product
@PM2Ring
PM2Ring / xor_bytes_speed_tests.py
Last active October 8, 2017 18:11
Test speeds of performing XOR on two bytes strings of equal length
#! /usr/bin/env python3
''' Test speeds of performing XOR on two bytes strings of equal length
Typical results on a 32bit 2GHz machine running Python 3.6.0 on Linux
Size: 2 Loops: 65536
Verify True
xor_bytes_I : [0.36799076000170317, 0.3949565820003045, 0.40133740400051465]
xor_bytes_BLC : [0.41839819899905706, 0.4583157610031776, 0.4664555540002766]