Discussion:
[Tutor] generate random number list and search for number
Quiles, Stephanie
2015-08-11 03:18:59 UTC
Permalink
Hello everyone,

Totally lost here. i have to create a program that asks user to input a value and then search for this value in a random list of values to see if it is found. Please see below for more detailed info.


This problem requires you to use Python. We want to test out the ordered sequential search algorithm (Listing 5.2). Here’s how we will do it. We’ll need to have a routine that will generate some random numbers for us to place into a list. We’ll need to have a main function to drive the program. And we’ll need to have Listing 5.2 to actually run through our list to check for some value that we will be looking for.
• First, we’ll need to import time to use the clock routine, and we’ll need to use the randrange routine from random.
• Next, we’ll want our main( ) routine to call our createRandnums( ) routine and have this routine return our list of random numbers. Once we have the list, we’ll start the time clock and then pass the list, along with a value to “search for” to the orderedSequentialSearch() routine. This routine will return True or False as to whether or not it found the value in the list. Once the routine has completed, we’ll want to stop the clock in main. By subtracting “end – start”, we’ll be able to figure out how long the routine took to run.
• The createRandnums routine will accept two parameters (how many numbers do you want, and what is the upper limit on the randrange routine), and this will simply run a loop to create the numbers and append them to a list. Once the list is complete, we can then use the sort routine on the list to arrange the numbers in order. The function will now return the sorted list to main( ). The main( ) routine will then record the clock time, call the search routine, receive back a True or False value from the search, stop the clock, and print our whether or not the value was in the list, and how long the search took.
Some comments: why am I placing an upper bound on the randrange function? Well, consider if we want to generate 100 random numbers. If we run a loop 100 times, and each time through the loop we say something like x=randrange(1,101), we’ll be getting numbers from 1 – 100 inclusive. Now since the loop will run 100 times, and we’re only picking numbers from 1 – 100, we’re bound to get a number of repeats in the list. But if we, say, still run the loop 100 times, but we say x=randrange(1,300), we’ll still be generating 100 numbers (because the loop runs 100 times), but our spread of numbers should be better.
We’ll need to figure out what value to look for in the list. We could ask the user to supply a value.
You need to run the program for lists of 100, 1000, and 10000 random numbers.

A sample of how you might proceed follows:
This program will generate a list of random numbers and
test the sequential search algorithm
Enter number of random integers to create in list: 100
Enter high-end random number to generate: 300
Enter number to find within list: 23

Here are the results:
Item to find in random list of 100 values: 23
Sequential search: Found= False Time: 9.91956746077e-006

Here is the thing I am totally lost. Here is what i have so far… i created the main function and i ask the user to search for a number. I have assigned a range of 100. but here is one of my questions how do i assign to search within a value range(for example, i want the program to spit out 100 numbers between 1-300). how would i go about doing this? also how do i call the orderedSequentialSearch so i can check to see if value is in list?

import time
import random

def orderedSequentialSearch(alist, item):
pos = 0
found = False
stop = False
while pos < len(alist) and not found and not stop:
if alist[pos] == item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos = pos + 1

return found

def main():
list_range = 100
find = int(input("enter search number: "))
for num in range(1, list_range + 1):
if random.randint(1, list_range) != 0:
print(random.randint(1, list_range))
if random.randint(1, list_range) == find:
print("Number Found.")
else:
print("not found")

if __name__ == '__main__':
main()

Thank you so much in advance!

Stephanie





_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/ma
Alan Gauld
2015-08-11 08:28:47 UTC
Permalink
Post by Quiles, Stephanie
Totally lost here.
Most of the instructions are clearly listed. Just follow through and do
what it suggests one step at a time.
Post by Quiles, Stephanie
• First, we’ll need to import time to use the clock routine,
So import time
Post by Quiles, Stephanie
and we’ll need to use the randrange routine from random.
and import random.

Well done I see you did that.
Post by Quiles, Stephanie
• Next, we’ll want our main( ) routine to call our createRandnums( ) routine
and have this routine return our list of random numbers.
It says 'our' routine so you need to create it.
Post by Quiles, Stephanie
• The createRandnums routine will accept two parameters
(how many numbers do you want, and what is the upper limit on
the randrange routine), and this will simply run a loop to
create the numbers and append them to a list. Once the list
is complete, we can then use the sort routine on the list to
arrange the numbers in order. The function will now return
the sorted list to main( ).
So that's your next task.
write the function definition and create a main() that simply
calls createRandnums(quantity, limit)

To check it works print the result.

Forget about the rest of the assignment until you get that
bit working.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://m
Steven D'Aprano
2015-08-11 12:29:52 UTC
Permalink
Hi Stephanie,

My comments below, interleaved between yours.
Post by Quiles, Stephanie
Here is the thing I am totally lost. Here is what i have so far… i
created the main function and i ask the user to search for a number.
You're not totally lost! Only a little bit lost. You've actually done
really well to get to the point you are now.
Post by Quiles, Stephanie
I
have assigned a range of 100. but here is one of my questions how do i
assign to search within a value range(for example, i want the program
to spit out 100 numbers between 1-300).
Remember how the instructions say to write a function createRandnums()?
You haven't done that, so you need to. It will take two parameters, the
count of random numbers to return, and the highest value to allow. Since
it will return a list of numbers, we need to start with an empty list,
do some stuff to fill the list, then return it:


def createRandnums(count, highest):
alist = []
stuff
return alist


where you replace "stuff" with your actual code.

You should start by grabbing the code from your main() function that
loops 100 times, and put it in the createRandnums function instead. Each
time you generate a random number between 1 and highest, instead of
printing it, you append it to the list:

alist.append(the_number)

Finally, instead of hard-coding that the loop will ALWAYS be 100, you
want to loop "count" times instead:

# loop 10 times:
for i in range(10): ...

# loop 20 times:
for i in range(20): ...

# loop "count" times, whatever value count happens to be:
for i in range(count): ...

Does that help?
Post by Quiles, Stephanie
how would i go about doing
this? also how do i call the orderedSequentialSearch so i can check to
see if value is in list?
result = orderedSequentialSearch(the_random_numbers, the_value_to_search_for)
if result:
print "the number is found"
else:
print "No number for you!"



Hope this helps!
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python
Loading...