#1
  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. #2
  3. 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.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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
    [edit]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.
    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
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Minneapolis, MN
    Posts
    356
    Rep Power
    13
    You could just sort the list and grab the last one.

    Code:
    mylist = [1 , 3 , 9 , 2]
    mylist.sort()
    biggest = mylist[-1:][0]
    badger badger badger badger
    badger badger badger badger
    MUSHROOM MUSHROOM
  8. #5
  9. 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.

IMN logo majestic logo threadwatch logo seochat tools logo