On 12/27/06, Ian Bicking <[EMAIL PROTECTED]> wrote:

Shannon -jj Behrens wrote:
>
> Agreed!  Merry Christmas to everyone!
>
> Here is my gift to you all:  write a function that acts like max, but
> instead of returning the item in the list, return the index of the
> item....

Puzzles!

def maxindex(seq):
     return max(zip(seq, itertools.count()))[1]

maxindex([1, 1, 0]) => 1, but I think it should be 0 ;)

> wasn't that fun?
>
> If you want more, write a min function in the same way, but be sure
> you don't duplicate any code. :)

Hmm... well, going for complex rather than terse...

def minindex(seq, index=0):
     if len(seq) == index+1:
         return index
     rest = minindex(seq, index+1)
     return rest if seq[rest] < seq[index] else index

Whee!  I haven't see the Python ternary expression "out in the wild yet".  Fun!

Happy Hacking!
-jj

--
http://jjinux.blogspot.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to