Skip to content

Instantly share code, notes, and snippets.

@mdamien
mdamien / cookies.py
Last active August 29, 2015 14:07
atelier python
# https://docs.python.org/3.4/library/turtle.html
from turtle import *
import random
def draw_cookie(x, y):
penup()
goto(x,y)
pendown()
radius = random.randint(100,200)
col = random.choice(['#CC6600','#FFCD82','#8F4700'])
@mdamien
mdamien / match.py
Created December 20, 2014 11:10
pattern matching
def iterate_by_chunk(word, chunk_length):
i = 0
while i < len(word):
yield word[i:i+chunk_length]
i += chunk_length
def check(word, pattern, len_pattern_word, i_offset):
"""check if the word match the pattern"""
pattern_length = len(pattern)*len_pattern_word

given n, print all the combinations of 0...(n-1) of length n

example

n = 2

00
01
10

11

@mdamien
mdamien / jam.py
Last active August 29, 2015 14:12
#https://code.google.com/codejam/contest/7214486/dashboard
from collections import deque
from itertools import combinations
class Matrix:
@staticmethod
def from_string(M):
return Matrix([list(x.strip()) for x in M.split('\n')])
def __init__(self,M):
from pprint import pprint as pp
from collections import deque
def find_bridge_bf(nodes, edges, a, b):
"""
Simple brute-force bridge finding
For each edge, explore nodes from *a* and try to reach b without using the edge
If it can't reach the *b*, it was a bridge
"""
for edge in edges:
@mdamien
mdamien / count.py
Created February 11, 2015 14:06
Trie Python
#!/usr/bin/env python3
import sys
"""
Use a naive implementation of a Trie: http://en.wikipedia.org/wiki/Trie
"""
class Trie:
def __init__(self):
self.children = {} #letter<->sub-Trie mappings
@mdamien
mdamien / exec.py
Created March 12, 2015 11:09
Formal grammar interpreter
#Execute a type 3 grammar
rules2 = """
S -> ATC
A -> aA |
C -> cC |
T -> abT | ab
"""
class bcolors:
@mdamien
mdamien / sol.py
Created March 13, 2015 10:15
hashcode2014 painting
import numpy as np
import sys
import random
MAX_ERASE_RATIO = 30 # 10 => 27 500
DILLATE_SIZE = 0
ERASED = -8
EXTRA = -6
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 0 1 1 0 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
0 1 0 1 1 -1 1 0 -1 1 0 1 1 0 1 0 1 0 1 1 0 -1 1 0 1 -1 1 0 1 -1 1 1 1 -1 0 1 0 1 1 0 0 0 1 -1 0 -1 0 -1 1 0 0 -1 1
1 1 1 1 0 1 -1 1 1 1 0 1 -1 0 1 0 1 1 -1 0 1 1 -1 -1 1 0 -1 1 0 0 1 0 1 1 0 1 1 1 -1 -1 1 1 1 1 1 1 1 0 1 1 1 1 1
1 1 0 1 1 -1 0 1 0 1 0 1 -1 1 -1 1 0 1 1 1 1 1 0 0 -1 0 -1 1 1 1 -1 0 1 1 1 0 -1 1 1 0 1 0 1 1 1 1 1 1 0 0 -1 -1 1
-1 1 -1 -1 -1 1 1 -1 0 1 0 -1 1 1 -1 1 0 1 -1 1 0 -1 0 1 -1 0 0 -1 1 0 -1 0 -1 1 1 -1 0 -1 -1 0 0 -1 1 0 0 -1 -1 -1 -1 -1 0 1 1
0 1 0 0 1 0 0 1 1 -1 1 1 1 0 1 1 0 -1 0 1 1 1 1 1 0 0 1 1 1 1 0 1 -1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 0 0 -1
-1 0 -1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 -1 1 1 -1 0 1 1 0 0 1 1 1 1 -1 -1 0 1 1 0 1 1 1 1 0 1 1
0 -1 0 -1 1 1 0 0 0 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 0 -1 1 1 1 1 1 0 1 1 1 -1 1 1 0 1 1 1 0 1 1 1 1 1 0 -1 1 0 1 0
1 1
from __future__ import print_function
from itertools import *
def find_switches(a,b):
return {i for i,v in enumerate(a) if v != b[i]}
def transform(a,switchs):
r = ""
for i, v in enumerate(a):
if i in switchs: