### Thread: Finding the max value in a list and its position in the list

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2007
Posts
1
Rep Power
0

#### Finding the max value in a list and its position in the list

hi guys, ive been set this question but i am to write a code which doesn't use the built in python term of max() to find the max value in the list.

The question is this:
Consider the problem of finding the largest number in a list. Think first of a systematic way of doing the problem by hand.
Suppose the numbers are in a list y. The following fragment of code shows one way to find which of the numbers is the maximum, and the maximum value:

mx=y [0];j=0
for i in range (1, len(y)):
if y[i]>mx:
mx=y[i]; j=i

Study the code above to see how it works. Combine it with instructions to fill y with numbers typed on the keyboard to make a function which asks the user to enter some numbers and prints out largest value entered and its position in the input list.

So for my input of numbers i have used this:

y=([float(raw_input("enter a number from 1 to 100: ")), float(raw_input("enter a number from 1 to 100: ")),float(raw_input("enter a number from 1 to 100: ")), float(raw_input("enter a number from 1 to 100: ")),float(raw_input("enter a number from 1 to 100: "))])
enter a number from 1 to 9: 1
enter a number from 1 to 9: 2
enter a number from 1 to 9: 3
enter a number from 1 to 9: 4
enter a number from 1 to 9: 5

Just an example. But when i put the fragment of code given above into python i dont get anything. Can anyone help please??
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2007
Location
NYC
Posts
46
Rep Power
8
When you post code to a forum, wrap it in "CODE" tags so that the forum displays it in a fixed width font with indenting preserved, like it was in your editor. Just select the text and find the "CODE" button.

I just entered your code, and it asked me for 5 numbers. It then found the max, but you wouldn't know about it because the code doesn't print it to the screen. For this, simply put "print mx" as your last line.

There is a tidier way to do this though:

Code:
```y = input("Enter 5 numbers separated by commas")
mx = 0
for num in y:
if num > mx:
mx = num

print "The largest number was", mx```
EDIT: I just realized that this of course does not work if all the numbers in your list are negative. Although all you'd have to do is set mx = y[0] instead of mx = 0
Last edited by Sharkey; December 12th, 2007 at 09:07 PM.
3. Gah, you guys are writing way too much code:
Code:
```#!/usr/bin/env python

y = [1, 4, 32, 16, 27]
m = max(y)
p = y.index(m)
print m, p```
Whoops, the requirement was not to use max(). Oh well, disregard this post[/edit]
Last edited by Scorpions4ever; December 13th, 2007 at 01:10 AM.
4. You could just sort the list and grab the last one.

Code:
```mylist = [1 , 3 , 9 , 2]
mylist.sort()
biggest = mylist[-1:][0]```
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2007
Location
NYC
Posts
46
Rep Power
8
Originally Posted by crustymonkey
You could just sort the list and grab the last one.

Code:
```mylist = [1 , 3 , 9 , 2]
mylist.sort()
biggest = mylist[-1:][0]```
God I love Python.