0

I have wriiten a code for linear search in python language. The code is working fine for single digit numbers but its not working for double digit numbers or for numbers more than that. Here is my code.

def linear_search(x,sort_lst):
    i = 0
    c= 0
    for i in range(len(sort_lst)):
        if sort_lst[i] == x :
             c= c+1
    if (c > 0):
      print ("item found")
    else : 
      print ("not found")
sort_lst= input("enter an array of numbers:")
item= input("enter the number to searched :")
linear_search(item,sort_lst)

any suggestions ?

3 Answers3

0

Replace

sort_lst= input("enter an array of numbers:")

with:

print 'enter an array of numbers:'
sort_lst= map(int, raw_input().strip().split(' '))
Debanik Dawn
  • 797
  • 5
  • 28
0

If all you want is a substring search, you can just use this

print("item found" if x in sort_lst else "not found")

If you want to get more complicated, then you need to convert your input from a string to an actual list.

(assuming space separated values)

sort_lst= input("enter an array of numbers:").split()

Then, that's actually a list of strings, not integers, but as long as you compare strings to strings, then your logic should work

Note: the print statement above will still work in both cases

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

This may be a case of confusion between behavior in python 2.x and python 3.x, as the behavior of the input function has changed. In python 2, input would produce a tuple (12, 34) if you entered 12, 34. However, in python 3, this same function call and input produces "12, 34". Based on the parenthesis in your prints and the problem you're having, it seems clear you're using python 3 :-)

Thus when you iterate using for i in range(len(sort_lst)):, and then looking up the element to match using sort_lst[i], you're actually looking at each character in the string "12, 34" (so "1", then "2", then ",", then " ", etc.).

To get the behavior you're after, you first need to convert the string to an actual list of numbers (and also convert the input you're matching against to a string of numbers).

Assuming you use commas to separate the numbers you enter, you can convert the list using:

sorted_int_list = []
for number_string in sort_list.split(","):
    sorted_int_list = int(number_string.strip())

If you are familiar with list comprehensions, this can be shortened to:

sorted_int_list = [int(number_string.strip()) for number_string in sort_list.spit(",")]

You'll also need:

item = int(item.strip())

To convert the thing you're comparing against from string to int.

And I'm assuming you're doing this to learn some programming and not just some python, but once you've applied the above conversions you can in fact check whether item is in sorted_int_list by simply doing:

is_found = item in sorted_int_list
if is_found:
    print ("Found it")
else:
    print ("Didn't find it :-(")

Notes:

"12, 34".split(",") produces ["12", " 34"], as the split function on strings breaks the string up into a list of strings, breaking between elements using the string you pass into it (in this case, ","). See the docs

" 12 ".strip() trims whitespace from the ends of a string

augray
  • 3,043
  • 1
  • 17
  • 30
  • this is not working for me. as per getting "12" and "13" together is working with this list but if I am giving item item as 12 then its displaying item not found. – nishant singh Sep 12 '17 at 04:21