Skip to content

Instantly share code, notes, and snippets.

View KrzysztofCiba's full-sized avatar

Krzysztof Daniel Ciba KrzysztofCiba

View GitHub Profile
@KrzysztofCiba
KrzysztofCiba / perm.py
Last active October 11, 2021 09:05
Get nth permutation of a list, while permutations are created in a lexicographical order.
#!/bin/env python
"""
Solves a problem of getting permutation of a given index,
while you know that permutations are created in a lexicographical order.
As input you are specifying a list of elements and index of permutation you want to get.
"""
## imports
from math import factorial
## this one just for checking
@KrzysztofCiba
KrzysztofCiba / TypedList.py
Last active August 25, 2021 09:30
TypedList: definition of a python list that can hold only items of desired type.
########################################################################
# $HeadURL $
# File: TypedList.py
# Author: Krzysztof.Ciba@NOSPAMgmail.com
# Date: 2012/07/19 08:21:16
########################################################################
""" :mod: TypedList
=======================
.. module: TypedList
:synopsis: typed list
@KrzysztofCiba
KrzysztofCiba / Observer.py
Created October 1, 2012 19:22
python observer pattern
########################################################################
# $HeadURL $
# File: Observer.py
# Author: Krzysztof.Ciba@NOSPAMgmail.com
# Date: 2012/09/26 09:39:58
########################################################################
""" :mod: Observer
==============
.. module: Observer
:synopsis: Observer pattern
@KrzysztofCiba
KrzysztofCiba / color.py
Last active December 18, 2015 16:39
simple graph coloring
#!/usr/bin/env python
import random
import sys
class node( object ):
""" a vertex
"""
def __init__( self, key ):
""" c'tor """
self.key = key
class Node( object ):
"""
.. class:: Node
rbTree Node
"""
def __init__( self, key ):
""" c'tor """
self.left = None
__bc16b = [ 0 ] * (256*256)
for i in range(256*256):
__bc16b[i] = ( i & 1 ) + __bc16b[i/2]
def popcnt16b( arg ):
""" popcnt using 16bits loom up table """
return sum( [ __bc16b[( arg >> i ) & 65535 ] for i in range( 0, arg.bit_length(), 16 ) ] )
## bit count look up table in range 0..255
__bcLUT = [ 0 ] * 256
for i in range(256):
__bcLUT[i] = ( i & 1 ) + __bcLUT[i/2]
def popcntLUT( arg ):
""" popcnt using look up table """
assert type(arg) in ( int, long )
return sum( [ __bcLUT[ ( arg >> i ) & 255 ]
for i in range( 0, arg.bit_length(), 8 ) ] )
@KrzysztofCiba
KrzysztofCiba / gist:5717137
Last active December 18, 2015 03:19
popcnt benchmarks
# --- testBK.py
import sys
import random
from popcnt import popcntBK
up = 2**256
for i in range(1, 1001):
if not i % 10:
print ".",
sys.stdout.flush()
if not i % 100:
@KrzysztofCiba
KrzysztofCiba / popcnt.py
Last active December 18, 2015 02:58
python pop count
#!/bin/env python
""" :mod: popcnt
============
.. module: popcnt
:synopsis: couting bits set ("1") in int or long
.. moduleauthor:: Krzysztof.Ciba@NOSPAMgmail.com
"""
## bit count look up table in range 0..255
__bcLUT = [ 0 ] * 256
from multiprocessing import Process, Manager
def sender( S ):
print "sending 256 MB"
S["foo"] = "."*256*1024*1024
if __name__ == '__main__':
m = Manager()
S = m.dict()