Function to determine list max without itertools
Chris Angelico
rosuav at gmail.com
Wed Apr 17 19:33:20 EDT 2019
On Thu, Apr 18, 2019 at 9:31 AM Sayth Renshaw <flebber.crue at gmail.com> wrote:
>
>
> I have created a function that takes a list as an argument.
> Without using itertools I want to compare each item in the list to find the max.
>
> However instead of the max I keep getting the last item in the list. Where is my logic wrong here?
>
> def maximum(listarg):
> items = list(listarg)
> myMax = 0
> for index, item in enumerate(items):
> for otheritem in items[index + 1 :]:
> if item < otheritem:
> myMax = otheritem
> elif item > otheritem:
> myMax = item
> else:
> myMax = myMax
>
> Seems like it should work but doesn't.
>
I'd recommend rethinking your algorithm first, and then thinking about
debugging your actual code. You should be able to find the largest in
a collection without making many many passes over the items - a single
pass should be sufficient.
ChrisA
More information about the Python-list
mailing list