Searching for most pythonic/least stupid way to do something simple

Gerard Flanagan grflanagan at gmail.com
Tue Mar 16 15:28:17 CET 2010


david jensen wrote:
> ... and of course i screwed up my outcomes... that should read
> outcomes=[[4,3,8,3,5],[3,4,8,3,5],[2,5,8,3,5],[1,6,8,3,5],[0,7,8,3,5]]
> 
> 
> 

abstracting the given algorithm:

def iterweights(N):
     d = 1.0/(N-1)
     for i in xrange(N):
         yield i*d, (N-1-i)*d

def iterparts(x0, x1, N):
     a = min(x0, x1)
     b = max(x0, x1)
     s = 2 * a
     t = b - a
     for m, n in iterweights(N):
         if a == x0:
             yield s*m, s*n+t
         else:
             yield s*n+t, s*m

for p in iterparts(2, 5, 5):
     print p

print

for p in iterparts(5, 2, 5):
     print p

(0.0, 7.0)
(1.0, 6.0)
(2.0, 5.0)
(3.0, 4.0)
(4.0, 3.0)

(7.0, 0.0)
(6.0, 1.0)
(5.0, 2.0)
(4.0, 3.0)
(3.0, 4.0)




More information about the Python-list mailing list