Skip to content

Instantly share code, notes, and snippets.

View mgronhol's full-sized avatar

Markus Grönholm mgronhol

View GitHub Profile
@mgronhol
mgronhol / generoi-sarja.py
Last active November 2, 2015 10:02
Generate sequence with unique 3-neighbourhood with no repetition
#!/usr/bin/env python
import random, itertools
def generate( symbols ):
out = ""
for s in symbols:
out += s
done = False
@mgronhol
mgronhol / lisp-parser1.py
Created May 30, 2015 20:12
Simple python lisp parser using shlex
#!/usr/bin/env python
import shlex, pprint
text = """
(define mauri (lambda (x y)
(+ x y)
)
)
(display (mauri 1 2) )
"""
@mgronhol
mgronhol / gist:fc508d4572034dc4d15d
Created December 2, 2014 23:07
Django form generator
def generic_create_form(name, modelclass):
model_fields = [field.name for field in modelclass._meta.fields]
class Meta:
pass
setattr(Meta,"model", modelclass)
setattr(Meta,"fields", model_fields)
attrs = {'__module__': '', 'Meta': Meta}
@mgronhol
mgronhol / DependencySolver.py
Created November 25, 2014 17:32
Dependency solver with O(N^2) runtime
#!/usr/bin/env python
# input as dependency graph
# {
# "node1": ["dep1", "dep2"...],
# "node2": [...],
# ...
# }
def solve( graph ):
@mgronhol
mgronhol / fast-str.c
Last active October 23, 2024 16:28
Fast string compare
int fast_compare( const char *ptr0, const char *ptr1, int len ){
int fast = len/sizeof(size_t) + 1;
int offset = (fast-1)*sizeof(size_t);
int current_block = 0;
if( len <= sizeof(size_t)){ fast = 0; }
size_t *lptr0 = (size_t*)ptr0;
size_t *lptr1 = (size_t*)ptr1;
@mgronhol
mgronhol / pplc-xor.py
Created July 6, 2014 19:07
Provable PLC first test (xor)
#!/usr/bin/env python
def generate_test_cases(N):
M = 2**N
out = []
for i in range( M ):
case = [ (i >> j & 0x01) == 1 for j in range( N ) ]
out.append( tuple( case ) )
return out
@mgronhol
mgronhol / fetchMultiple.js
Created March 15, 2014 20:12
Fetch and parse multiple JSON datasets
function fetchMultiple( datasets, predicate, finished ){
var functions = [];
var results = [];
for( var i = 0 ; i < datasets.length ; ++i ){
functions.push( (function(i){ return function(){
$.getJSON( datasets[i], function( response ){
results.push( predicate( response ) );
functions[i+1]();
});
@mgronhol
mgronhol / Vektori.java
Last active August 29, 2015 13:57
Simple vector class in java
public class Vektori {
public double x, y, z;
public static Vektori fromPolar( double r, double phi ){
return new Vektori( r * Math.cos( phi ), r * Math.sin( phi ), 0 );
}
@mgronhol
mgronhol / poor-grep.py
Created September 20, 2013 09:10
Search recursively a directory tree for files that contain a specified string (poor mans grep replacement)
#!/usr/bin/env python
import sys, os
basedir = "."
if len( sys.argv ) > 2:
basedir = sys.argv[1]
string = sys.argv[-1]
@mgronhol
mgronhol / pick-nodes.py
Last active December 23, 2015 12:19
How to pick outer nodes by inner node content
#!/usr/bin/env python
import xml.dom.minidom as dom
import xml
import sys
def read_xml_file( fn ):
return dom.parse( fn ).documentElement