Python Algorithm Challenge? -


i have python function (call myfunction) gets input a list of numbers, and, following complex calculation, returns result of calculation (which number).

the function looks this:

def myfunction( listnumbers ):     # initialize result of calculation     calcresult = 0      # looping through indices, 0 last 1     in xrange(0, len(listnumbers), 1):         # complex calculation goes here, changing value of 'calcresult'      # let return result of calculation     return calcresult 

i tested function, , works expected.

normally, myfunction provided listnumbers argument contains 5,000,000 elements in it. may expect, calculation takes time. need function run fast possible

here comes challenge: assume time 5am, , listnumbers contains 4,999,999 values in it. meaning, last value not yet available. value only available @ 6am.

obviously, can following (1st mode): wait until 6am. then, append last value listnumbers, , then, run myfunction. solution works, it take while before myfunction returns our calculated result (as need process entire list of numbers, first element on). remember, our goal results possible past 6am.

i thinking more efficient way solve (2nd mode): since (at 5am) have listnumbers 4,999,999 values in it, let start running myfunction. let process whatever can (remember, don't have last piece of data yet), , -- @ 6am -- 'plug in' new data piece -- , generate computed result. should faster, most of processing done before 6am, hence, have deal new data -- means computed result should available immediately after 6am.

let's suppose there's no way inspect code of myfunction or modify it. there any programming technique / design idea allow take myfunction as is, , (without changing code) can have operate in 2nd mode, rather 1st one?

please not suggest using c++ / numpy + cython / parallel computing etc solve problem. goal here see if there's programming technique or design pattern can used solve such problems.

you use generator input. generator return when there data available process.

update: brilliant comment, wanted remove entry :)

class lazylist(object):     def __init__(self):         self.cnt = 0         self.length = 5000000      def __iter__(self):         return self      def __len__(self):         return self.length      def next(self):         if self.cnt < self.length:             self.cnt += 1             #return data here or wait             return self.cnt #just return counter example         else:             raise stopiteration()      def __getitem__(self, i):         #again, block till have data.         return i+1 #simple counter  myfunction(lazylist()) 

update: can see comments , other solutions loop construct , len call causes lot of headaches, if can eliminate can use lot more elegant solution. for e in li or enumerate pythonic way go.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -