Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / contour_cal.xml
Last active July 26, 2017 10:49
OpenCV camera calibration for Contour+2
<?xml version="1.0"?>
<opencv_storage>
<image_Width>1920.0</image_Width>
<image_Height>1090.0</image_Height>
<flagValue>14</flagValue>
<Camera_Matrix type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>d</dt>
<data>
@TimSC
TimSC / maryrose.py
Created February 11, 2017 23:25
Solve position of mary rose site
import numpy as np
if __name__=="__main__":
X = np.ones((4, 4))
X[:, :2] = [[663, 706], [1303, 74], [2491, 1302], [2935, 747]]
X[:, 2] = X[:, 0] * X[:, 1]
lats = 50.777453115, 50.7856611742, 50.7704862, 50.778133
coeffLats = np.linalg.lstsq(X, lats)[0]
@TimSC
TimSC / test.cpp
Last active January 2, 2017 23:15
Test geos_c library
//Compile with: g++ test.cpp -lgeos_c -o test
#include <geos_c.h>
#include <stdio.h>
void noticeFunc(const char *message, ...)
{
printf("notice: %s\n", message);
}
void errorFunc(const char *message, ...)
@TimSC
TimSC / nginx-uwsgi-django.md
Last active January 4, 2017 22:24
Recipe for getting nginx-uwsgi-django working on Mint 18.1 (which is systemd based)
  • cd /var/www
  • sudo django-admin startproject mysite
  • sudo chown www-data:www-data -R mysite
  • sudo chmod g+rw -R mysite
  • cd mysite
  • sudo pip install uwsgi
  • python manage.py startapp polls

sudo xed /etc/nginx/sites-available/mysite

@TimSC
TimSC / zero-move-nim.py
Created December 23, 2016 01:27
Test code for zero move nim
#Generating zero move nim game outcomes
import copy
def GenGames(piles, passes, turn, stateOptions, stateWins):
options = []
nonZeroPiles = []
for i, p in enumerate(piles):
if p > 0:
@TimSC
TimSC / primesieve.py
Last active December 15, 2016 11:53
Sieve Of Eratosthenes in python
import math
class SieveOfEratosthenes(object):
def __init__(self, limit1, limit2 = None):
if limit2 is None:
self.lowerLimit = 2
self.upperLimit = limit1
else:
if limit1 <= 1:
raise RuntimeError("Lower limit must be at least 2")
@TimSC
TimSC / eertree.py
Last active February 9, 2023 01:12
Implementation of eertree, a palindromic tree in python.
#!/bin/python
#Based on: EERTREE: An Efficient Data Structure for Processing Palindromes in Strings
#by Mikhail Rubinchik and Arseny M. Shur https://arxiv.org/pdf/1506.04862.pdf
#Useful: https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b#.ofgt2r7xn
#Port of https://github.com/zimpha/algorithmic-library/blob/master/string-utility/eertree.cc
#to python by TimSC. zimpha kindly allowed me to release this code under the CC0 license.
from __future__ import print_function
@TimSC
TimSC / stocks.py
Created December 13, 2016 00:53
Dead end approach to stock predition on hackerrank.com
#Dead end approach to https://www.hackerrank.com/challenges/stockprediction
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
fi = open("sample.txt", "rt")
lis = fi.read().split("\n")
stocks = {}
for li in lis:
@TimSC
TimSC / stats.py
Last active February 13, 2024 03:00
Python statistics and matrices without numpy
#By Tim Sheerman-Chase 2016
#Released under the CC0 license
from __future__ import print_function
import itertools, copy
class RunningAverage(object):
def __init__(self):
self.Val = 0.0
self.Count = 0
@TimSC
TimSC / knightsmove.py
Last active January 15, 2017 04:53
Number of moves for chess knight to move to a position (O(1) algorithm)
from __future__ import print_function
def calc_board(limit = 9):
board = {}
board[(0,0)] = 0
posToCheck = set([(0,0)])
while len(posToCheck) > 0:
posToCheck2 = set()
for pos in posToCheck: