This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder