from itertools import groupby
from operator import itemgetter
import random
import time
lst = [ 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ]
def makeList( size) :
random .seed ( )
return [ random .randint ( 0 , 1 ) for r in xrange ( size) ]
def runs1( lst, showOutput) :
startLength = { }
for k, v in groupby( enumerate ( lst) , key= itemgetter( 1 ) ) :
if k:
v = list ( v)
startLength[ v[ 0 ] [ 0 ] ] = v[ -1 ] [ 0 ] + 1 - v[ 0 ] [ 0 ]
if showOutput == True :
for s, l in startLength.iteritems ( ) :
print s, l
def runs2( lst, showOutput) :
previous = 0
cnt = 0
startLength = { }
for r in lst:
if previous == 0 and r == 1 :
start = cnt
if previous == 1 and r == 0 :
startLength[ start] = cnt - start
previous = r
cnt += 1
if r == 1 :
startLength[ start] = cnt - start
if showOutput == True :
for s, l in startLength.iteritems ( ) :
print s, l
testList = makeList( 10 )
print "slow version"
runs1( testList, True )
print "fast version"
runs2( testList, True )
benchmarkList = makeList( 10000 )
start = time .time ( )
runs1( benchmarkList, False )
print "slow " , time .time ( ) - start
start = time .time ( )
runs2( benchmarkList, False )
print "fast " , time .time ( ) - start
start = time .time ( )
runs1( benchmarkList, False )
print "slow " , time .time ( ) - start
start = time .time ( )
runs2( benchmarkList, False )
print "fast " , time .time ( ) - start
start = time .time ( )
runs1( benchmarkList, False )
print "slow " , time .time ( ) - start
start = time .time ( )
runs2( benchmarkList, False )
print "fast " , time .time ( ) - start
ZnJvbSBpdGVydG9vbHMgaW1wb3J0IGdyb3VwYnkKZnJvbSBvcGVyYXRvciBpbXBvcnQgaXRlbWdldHRlcgppbXBvcnQgcmFuZG9tCmltcG9ydCB0aW1lCgpsc3QgPSBbIDEsMSwxLDEsMSwwLDAsMCwwLDAsMCwwLDEsMSwxLDEsMCwwLDAsMCwwLDBdCiAKZGVmIG1ha2VMaXN0KHNpemUpOgogICAgcmFuZG9tLnNlZWQoKQogICAgcmV0dXJuIFtyYW5kb20ucmFuZGludCgwLDEpIGZvciByIGluIHhyYW5nZShzaXplKV0KICAgIAogCmRlZiBydW5zMShsc3QsIHNob3dPdXRwdXQpOgogICAgc3RhcnRMZW5ndGggPSB7fQogICAgZm9yIGssdiBpbiBncm91cGJ5KGVudW1lcmF0ZShsc3QpLGtleT1pdGVtZ2V0dGVyKDEpKToKICAgICAgICBpZiBrOgogICAgICAgICAgICB2ID0gbGlzdCh2KQogICAgICAgICAgICBzdGFydExlbmd0aFt2WzBdWzBdXSA9IHZbLTFdWzBdICsgMSAtIHZbMF1bMF0KICAgIGlmIHNob3dPdXRwdXQgPT0gVHJ1ZToKICAgICAgICBmb3IgcyxsIGluIHN0YXJ0TGVuZ3RoLml0ZXJpdGVtcygpOgogICAgICAgICAgICBwcmludCBzLGwKIApkZWYgcnVuczIobHN0LCBzaG93T3V0cHV0KToKICAgIHByZXZpb3VzID0gMAogICAgY250ID0gMAogICAgc3RhcnRMZW5ndGggPSB7fSAKICAgIGZvciByIGluIGxzdDogCiAgICAgICAgaWYgcHJldmlvdXMgPT0gMCBhbmQgciA9PSAxOgogICAgICAgICAgICBzdGFydCA9IGNudAogICAgICAgIGlmIHByZXZpb3VzID09IDEgYW5kIHIgPT0gMDogCiAgICAgICAgICAgIHN0YXJ0TGVuZ3RoW3N0YXJ0XSA9IGNudCAtIHN0YXJ0CiAgICAgICAgcHJldmlvdXMgPSByCiAgICAgICAgY250ICs9IDEKICAgIGlmIHIgPT0gMToKICAgICAgICBzdGFydExlbmd0aFtzdGFydF0gPSBjbnQgLSBzdGFydAogICAgaWYgc2hvd091dHB1dCA9PSBUcnVlOgogICAgICAgIGZvciBzLGwgaW4gc3RhcnRMZW5ndGguaXRlcml0ZW1zKCk6CiAgICAgICAgICAgIHByaW50IHMsbAoKdGVzdExpc3QgPSBtYWtlTGlzdCgxMCkKcHJpbnQgInNsb3cgdmVyc2lvbiIKcnVuczEodGVzdExpc3QsIFRydWUpCnByaW50ICJmY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