Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
def load_graph():
g = {}
with open('sample.txt', 'r') as f:
for line in filter(lambda l: l != '\n', f):
row = line.split('\t')
g[int(row[1])] = list(map(int, row[2:]))
return g
@md2perpe
md2perpe / index.html
Created December 28, 2018 20:28
Game of life
<!doctype html>
<body>
<style>
td.selected {
background: red;
}
</style>
<script>
const N = 100;
const ROWS = N;
@md2perpe
md2perpe / pretty_print.py
Last active October 15, 2018 19:42
Pretty print av polynom
# https://www.facebook.com/photo.php?fbid=10155461108530633&set=p.10155461108530633&type=3&theater&ifg=1
def prettyPrint(lista):
# Funktionen tar en lista med koefficienter
# till ett polynom i avtagande ordning och
# skriver ut polynomet
# Polynomets gradtal
polynomets_gradtal = len(lista) - 1
@md2perpe
md2perpe / calculate-e.py
Created September 23, 2018 15:32
Calculate Euler's number
from math import factorial
e = 0
for k in range(0, 20):
e += 1/factorial(k)
print("k=% 3d: e=%.16f" % (k, e,))
@md2perpe
md2perpe / calculate-e.py
Last active September 23, 2018 15:09
Calculate Eulers number (e)
# https://www.facebook.com/groups/progfml/permalink/344454616128233/
from math import factorial
e=0
n=0
step_length=1
e_right=2.71828
margin_of_error=0.001
Klart=False
@md2perpe
md2perpe / sequence.hs
Created June 26, 2018 18:12
En talföljd
toSwedish :: Integer -> String
toSwedish 1 = "ett"
toSwedish 2 = "två"
toSwedish 3 = "tre"
toSwedish 4 = "fyra"
toSwedish 5 = "fem"
toSwedish 6 = "sex"
toSwedish 7 = "sju"
toSwedish 8 = "åtta"
toSwedish 9 = "nio"
@md2perpe
md2perpe / lambda.ts
Created June 13, 2018 18:39
Lambda calculus in Typescript
type _bool<X, Y> = (X, Y) => X | Y;
let _true = <X, Y>(x: X, y: Y) => x;
let _false = <X, Y>(x: X, y: Y) => y;
let _not = <X, Y>(b: _bool<Y, X>) => (x: X, y: Y) => b(y, x);
let _and = <X, Y>(b1: _bool<X | Y, Y>, b2: _bool<X, Y>) => (x: X, y: Y) => b1(b2(x, y), _false(x, y));
let _or = <X, Y>(b1: _bool<X, X | Y>, b2: _bool<X, Y>) => (x: X, y: Y) => b1(_true(x, y), b2(x, y));
// Using
// let __compose = <X, Y, U, V, W>(f: (U, V) => W, g: (X, Y) => U, h: (X, Y) => V) => (x: X, y: Y) => f(g(x, y), h(x, y));
@md2perpe
md2perpe / monad.ts
Created May 25, 2018 19:27
Monad example in Typescript
/////////////////////////////////////////////////////////
// Origin: https://www.youtube.com/watch?v=t1e8gqXLbsU
// Rewritten to Typescript by md2perpe on 25 May 2018
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
// The interface of a monad
/////////////////////////////////////////////////////////
@md2perpe
md2perpe / sierpinski.py
Created May 12, 2018 07:28
Sierpinsky triangle in Python turtle graphics
# https://trinket.io/python
import turtle
L = 200
turtle.speed(1000)
turtle.penup()
turtle.goto(-100, -100)
turtle.color("red")
@md2perpe
md2perpe / matching_positions.py
Created April 11, 2018 13:57
Matching positions
def matching_positions(data, choices):
# Example: data="apabanan", choices="aan"
# Initialize a dictionary for all positions of the char's in choices
positions = {}
for c in choices:
positions[c] = []
# Populate the dictionary
for k, c in enumerate(data):