December 12th, 2007, 06:32 PM
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:
for i in range (1, len(y)):
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??
December 12th, 2007, 08:09 PM
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:
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 instead of mx = 0
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
Last edited by Sharkey; December 12th, 2007 at 09:07 PM.
December 13th, 2007, 01:06 AM
Gah, you guys are writing way too much code:
Whoops, the requirement was not to use max(). Oh well, disregard this post[/edit]
y = [1, 4, 32, 16, 27]
m = max(y)
p = y.index(m)
print m, p
Last edited by Scorpions4ever; December 13th, 2007 at 01:10 AM.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
December 17th, 2007, 08:26 PM
You could just sort the list and grab the last one.
mylist = [1 , 3 , 9 , 2]
biggest = mylist[-1:]
badger badger badger badger
badger badger badger badger
December 17th, 2007, 10:34 PM
God I love Python.
Originally Posted by crustymonkey