how does random.sample work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noob92
    New Member
    • Dec 2006
    • 5

    how does random.sample work

    I'm using python to write a program that, when given a list of random numbers, sorts them from lowest to highest then it asks the user for a number and it checks that number's place in the sorted list if the number is present, but i keep getting a syntax error on an "if" statement line near the end. is it because i'm not allowed to look up a number in the sorted list? here's my program with a list of example numbers (highlighted in bold is where i get the syntax error):
    Code:
    import random
    def sort_array2_4():
        unsorted_array = random.sample(xrange(1000), 10)
    a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
    def bubblesort(a):
        for swap in range(len(a)-1,0,-1):
             for index in range(swap)
                 if a[index] > a[index + 1]:
                   a[index], a[index + 1] = a[index + 1], a[index]
        return a
    while(more==True):
        num=str(input("Enter a number to search for in list.\n")
        [B]if (num in bubblesort(a)):[/B]
            print num + "is in list at number: ", bubblesort(a).index(num)
        else:
            print num + "is not recognized in list"
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Great job! you were very close. next time include error message, ok?


    Code:
    import random
    def sort_array2_4():
        unsorted_array = random.sample(xrange(1000), 10)
    a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
    def bubblesort(a):
        for swap in range(len(a)-1,0,-1):
             for index in range(swap):
                 if a[index] > a[index + 1]:
                   a[index], a[index + 1] = a[index + 1], a[index]
        return a
    more = True  # must assign before reference
    while(more==True):
        num=input("Enter a number to search for in list.\n")
        # one too few parens caused syntax error no next line
        # need the int anyway
        b = bubblesort(a)  # use lots of variables for debugging
        print b   # debug "trap"
        if (num in b):
            # print will convert to printable, so you don't have to
            print num, "is in list at number: ", a.index(num)
        else:
            print num, "is not recognized in list"

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Maybe you know this but the Python list object has an in place sort method:
      Code:
      a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
      a.sort()
      print a
      >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]

      Comment

      • Loismustdie129
        New Member
        • Aug 2006
        • 194

        #4
        Originally posted by bvdet
        Maybe you know this but the Python list object has an in place sort method:
        Code:
        a=[93,84,200,513,46,1,45,334,92,96,156,82,92,46,]
        a.sort()
        print a
        >>> [1, 45, 46, 46, 82, 84, 92, 92, 93, 96, 156, 200, 334, 513]

        I was looking at the code that noob92 wrote and I was confused at what the sort_array2_4() code did. Can anyone explain that to me.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by Loismustdie129
          I was looking at the code that noob92 wrote and I was confused at what the sort_array2_4() code did. Can anyone explain that to me.
          The random module provide the function sample() defined here:

          sample( population, k)

          Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

          So the code:
          Code:
          import random
          def sort_array2_4():
              unsorted_array = random.sample(xrange(1000), 10)
          creates a 10 element list of random numbers from 0 to 999. If you copied the lines that does the work and run it you would get someting like:

          >>> import random
          >>> unsorted_array = random.sample(x range(1000), 10)
          >>> print unsorted_array
          [461, 231, 423, 725, 696, 203, 8, 692, 745, 886]

          Of course, after that the function does nothing (well actually, after that it returns None):

          >>> def sort_array2_4() :
          ... unsorted_array = random.sample(x range(1000), 10)
          ...
          >>> print sort_array2_4()
          None

          A bubble sort routine incrementally "floats" the biggest value to the top of the list. The lines:
          Code:
                  if a[index] > a[index + 1]:
                      a[index], a[index + 1] = a[index + 1], a[index]
          swaps the two values from left to right when the left one is bigger.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by Loismustdie129
            I was looking at the code that noob92 wrote and I was confused at what the sort_array2_4() code did. Can anyone explain that to me.
            Also, when you see something like
            import random

            The first thing to do is go to Start->Programs->Python 2.4->Python Manuals
            and click Global Module Index. Scroll 'till you find the name that is being imported, click the link and scroll 'till you see the method or fuction being called.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Loismustdie129
              I was looking at the code that noob92 wrote and I was confused at what the sort_array2_4() code did. Can anyone explain that to me.
              Barton made some excellent comments. One thing I will point out is that noob92 never made the function call in his sample code. The function call may look something like this:
              Code:
              import random
              
              def unsorted_list(n=10):
                  return random.sample(xrange(1000), n)
              
              def bubblesort(a):
                  for swap in range(len(a)-1,0,-1):
                       for index in range(swap):
                           if a[index] > a[index + 1]:
                             a[index], a[index + 1] = a[index + 1], a[index]
                  return a
              
              a = unsorted_list(14)
              print a
              
              b = bubblesort(a)
              print b

              Comment

              • Loismustdie129
                New Member
                • Aug 2006
                • 194

                #8
                Originally posted by bvdet
                Barton made some excellent comments. One thing I will point out is that noob92 never made the function call in his sample code. The function call may look something like this:
                Code:
                import random
                
                def unsorted_list(n=10):
                    return random.sample(xrange(1000), n)
                
                def bubblesort(a):
                    for swap in range(len(a)-1,0,-1):
                         for index in range(swap):
                             if a[index] > a[index + 1]:
                               a[index], a[index + 1] = a[index + 1], a[index]
                    return a
                
                a = unsorted_list(14)
                print a
                
                b = bubblesort(a)
                print b

                Thank you now I understand.

                Comment

                Working...