Skip to content

Instantly share code, notes, and snippets.

View bpgergo's full-sized avatar

Peter Gergő Barna bpgergo

View GitHub Profile
@bpgergo
bpgergo / sub_sequence.py
Last active January 25, 2018 13:40
find sub sequence with largest sum in a sequence of numbers
import random
import time
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print('%s function took %0.3f ms' % (f.__name__, (time2-time1)*1000.0))
@bpgergo
bpgergo / merge_lists.py
Last active December 1, 2017 16:44
merge ordered lists in python. compare heap merge vs naive implementation
import heapq
import operator
import functools
import time
from random import randint
def timeit(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
@bpgergo
bpgergo / levenshtein_histogram.py
Last active March 14, 2017 10:00
String to histogram
from collections import Counter
from bitarray import bitarray
HISTOGRAM_RANGE = 16
CHAR_RANGE = 3
def string_to_charc_codes(s):
return map(ord, list(s))
def char_codes_to_modulo(chars_codes):
@bpgergo
bpgergo / LabyBug.py
Last active December 15, 2015 22:29
solves the labyrinths in the game named [Laby](https://wiki.ubuntu.com/Edubuntu/AppGuides/laby) import refers to this file: [robot.py](https://github.com/sgimenez/laby/blob/master/data/mods/python/lib/robot.py)
from robot import *
class LabyBug:
"""The laby bug
solves the labyrinths in the game named [Laby](https://wiki.ubuntu.com/Edubuntu/AppGuides/laby)
import refers to this file: [robot.py](https://github.com/sgimenez/laby/blob/master/data/mods/python/lib/robot.py)
@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
@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 / 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 / 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 / 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 / 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