Skip to content

Instantly share code, notes, and snippets.

View bpgergo's full-sized avatar

Peter Gergő Barna bpgergo

View GitHub Profile
@bpgergo
bpgergo / gist:1039770
Created June 22, 2011 09:40
Ngrams with coroutines
def coroutine(func):
""" A decorator function that takes care
of starting a coroutine automatically on call """
def start(*args,**kwargs):
coro = func(*args,**kwargs)
coro.next()
return coro
return start
@coroutine
@bpgergo
bpgergo / gist:1039774
Created June 22, 2011 09:42
Ngrams with coroutines 2
@coroutine
def filter_chars(accepted_chars,target):
""" A coroutine to filter out unaccepted chars.
Accepts one char at a time """
while True:
c = (yield)
if c.lower() in accepted_chars:
target.send(c.lower())
@coroutine
@bpgergo
bpgergo / gist:1039777
Created June 22, 2011 09:43
Ngrams with coroutines 3
counts = [[10 for i in xrange(k)] for i in xrange(k)]
bigrams = filter_chars(accepted_chars, ngrams(2, counter(counts)))
for c in open('big.txt').read().decode(enc): bigrams.send(c)
@bpgergo
bpgergo / mult.py
Created August 29, 2011 23:16
Matrix multiplication
import operator
a = [[1, 2, 3], [4, 5, 6]]
b = [[1, 2], [3, 4], [5, 6]]
def sumprod(row, col):
return sum(reduce(operator.mul, data) for data in zip(row, col))
r = map(lambda row: map(lambda col: sumprod(row, col), zip(*b)), a)
@bpgergo
bpgergo / matrix.py
Created November 28, 2011 11:54
Concurrent matrix multiplication
import random
import multiprocessing
from itertools import starmap, izip, repeat, imap
from operator import mul
def calc_row_of_product_matrix(a_row, b, izip=izip):
'''Calculate a row of the product matrix P = A * B
Arguments:
a_row is af A
b is the B matrix
@bpgergo
bpgergo / twibet.py
Created December 16, 2011 19:46
this is a solution of the EuroPython 2011 Problem D. Twibet
'''
this is a solution of the EuroPython 2011 Problem D. Twibet
http://code.google.com/codejam/contest/dashboard?c=1277486#s=p3
'''
def build_graph(input_list):
di = {}
for i in range(1, len(input_list)+1):
di[i] = []
for monk, followed_monk in enumerate(input_list, start=1):
di[followed_monk].append(monk)
@bpgergo
bpgergo / do_n_flips.py
Created January 31, 2012 11:19
monte_carlo_simulation_in_ptyhon
def do_n_flips(n):
return ''.join([str(random.getrandbits(1)) for i in range(n)])
@bpgergo
bpgergo / sudoku_checker.py
Created July 26, 2012 22:15
python sudoku checker
def check_row(row):
return set(row) == set(range(1, len(row)+1))
def check_sudoku(m):
return all(map(check_row, m)) and all(map(check_row, zip(*m)))
@bpgergo
bpgergo / check_mail.py
Created September 12, 2012 12:12
python email checker
'''
Created on Sep 11, 2012
@author: bpgergo
'''
import mail_checker_conf
import poplib
from email import parser
from itertools import imap, ifilter, tee
from email.utils import parsedate
@bpgergo
bpgergo / group_names_from_wiki.py
Created October 4, 2012 10:20
Download people info from wikipedia, group people by zodiac
# coding= utf-8
import sys
import requests
from lxml import etree
import re
import pickle
from datetime import datetime
'''
feeble but working