Skip to content

Instantly share code, notes, and snippets.

View mgronhol's full-sized avatar

Markus Grönholm mgronhol

View GitHub Profile
@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 / fast-str.c
Last active July 15, 2025 12:17
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 / 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 / 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 / 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 / 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
#!/usr/bin/env python3.4
import sys
import shlex
class ParserError(Exception):
pass
@mgronhol
mgronhol / libModel.py
Last active August 29, 2016 18:14
Dataflow + stack machine
#!/usr/bin/env python
import math
_next_id = -1
def generate_id():
global _next_id
_next_id += 1
return _next_id
@mgronhol
mgronhol / bitteja.java
Created December 1, 2016 19:07
Some simple bit handling
// options
final int OPTION1 = 0;
final int OPTION2 = 1;
final int OPTION3 = 2;
// ad nauseam ...
// usage: if( getBit( flags, OPTION3 ) ){ ....
boolean getBit( int value, int pos ){
@mgronhol
mgronhol / noppapainotin.py
Created December 14, 2016 16:46
Tool to bin dice throwings
#!/usr/bin/env python3
import math, random
def simuloi_nopanheittoa( d, N ):
out = []
for i in range( N ):
out.append( random.randint( 1, d ) )
return out