Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I need to import outside data from a txt file

The data is a file of 10,000 numbers.

Read in the file, clean the data, convert from strings to integers and then sort the data. At the end of this process you should have a list of sorted integers with a len of 10,000.

What do I do from here? Next I have to do 2 functions, 2 searches, but I should be able to figure that out once I get started:

Create a library called mySearches.py. In this module two functions should reside:

bsearch - a function that accepts an integer and a list. The function should conduct a binary search through the list and return the index of the number in the list if the number is found and a -1 if the number is not found. The function should report how many lookups were performed during the search before it returns its value.

lsearch - a function that accepts an integer and a list. The function should conduct a linear search through the list and and return the index of the number in the list if the number is found and a -1 if the number is not found. The function should report how many lookups were performed during the search before it returns its value.

A main program called lookup.py should import both functions from mySearches.py. In the body of the program you should search for three numbers in the data (78700, 3333, 1118). Output should show if the number was found in the list and how many lookups were needed for each kind of search, even if the number is not found in the list.

Any help would be appreciated. In fact, I ask that you guide me through it, not do it for me. Thanks

8 Answers

Hi Casey

See my code below should give you an idea of how to proceed.

sorted_list = []
reader = open("numbers.txt",encoding="utf-8")

contents = reader.read()
contents = contents.split()
reader.close()

for content in contents:
  sorted_list.append(int(content))

def search_list(my_int,my_list):
  for i,num in enumerate(my_list):
    if num == my_int:
      print("The search found your integer in index {}".format(i))

sorted_list.sort()
search_list(78700,sorted_list)

this code above assumes your txt file is like this and named numbers.txt

45100
1200
65
71
42
65
12
78700
3333
1118
7
8

hope this helps

I tried that but it says NameError: 'reader' is not defined

thats strange i have run the exact same code and it works fine. Did you copy the code exactly as mine or did you make amends ?. Post the code you have run thats causing this error.

I've updated it to this:

from mySearches import bsearch, lsearch

infile = 'rands.txt'
f = open(infile,'r')
data = f.read()
f.close

# for i in infile.split():
#  var = int(i)

for content in infile:
  sorted_list.append(int(content))

  def search_list(my_int,my_list):
    for i, num in enumerate(my_list):
      if num == my_int:
        print("The search found your integer in index {}".format(i))

sorted_list.sort()
search_list(78700,sorted_list)
search_list(3333,sorted_list)
search_list(1118,sorted_list)

Now it says sorted_list is not defined. What do I do?

Like this:

''' from mySearches import bsearch, lsearch

infile = 'rands.txt' f = open(infile,'r') data = f.read() f.close

for i in infile.split():

var = int(i)

for content in infile: sorted_list.append(int(content))

def search_list(my_int,my_list): for i, num in enumerate(my_list): if num == my_int: print("The search found your integer in index {}".format(i))

sorted_list.sort() search_list(78700,sorted_list) search_list(3333,sorted_list) search_list(1118,sorted_list)'''

How do I block out code in this forum? It'll be easier to understand

Its says sorted_list is not defined that's because in your code it is not . Look at my earlier code it is the first line where i declared sorted_list as a list.

ok i have taken your code and made a few amends and it works.

#from mySearches import bsearch, lsearch

sorted_list= []  # you missed this line

infile = 'rands.txt'
f = open(infile,'r')
data = f.read()
data =  data.split() # here i split the contents so as to convert the string into a list to prevent errors when converting to an integer.
f.close

# for i in infile.split():
#  var = int(i)

for content in data:  # here you need to loop around the data not infile.
  sorted_list.append(int(content))

def search_list(my_int,my_list):
  for i, num in enumerate(my_list):
    if num == my_int:
      print("The search found your integer in index {}".format(i))

sorted_list.sort()
search_list(78700,sorted_list)
search_list(3333,sorted_list)
search_list(1118,sorted_list)

you can keep code and text separate see mardown sheet. I use the # symbol for comments or to ignore pieces of code. hope it helps

You're missing this: Output should show if the number was found in the list and how many lookups were needed for each kind of search, even if the number is not found in the list. In the output I didn't get how many tries it took, just a mention of what index it was found in.