1

I am new to python and we were given an assignment to create a linear search program that does not use "in" or index. The program compiles but says that every number I input is not in the list. I also have to do the same thing for a binary search but I'm doing things one at a time ha. Any help is appreciated!

PS: How could I show what index it is in without using the "index" function?

def linearSearch(intList,target):
    found = False
    position = 0
    while position < len(intList) and not found:
        if intList[position] == target:
            found = True
        position = position + 1

    return found

linearList = [3,5,9,7,6,12,15,9,1]
numInput = input("What number are you looking for? ")
numFound = linearSearch(linearList, numInput)
if numFound:
    print("The number is in index: ")
else:
    print("The number is not in the list")
1
  • Here is a hint to fix your problem: The input is string type, and you are comparing it with integer
    – karthikr
    Apr 9, 2017 at 3:52

8 Answers 8

1

1) Start position = -1

2) return position

3) You want to position+=1 before if intList[position] == target: and you want to break when you do find the element. You then don't need found

Something is found when linearSearch(linearList, numInput) > 0

Then, your code just doesn't work because the list contains ints whereas input will always return a string. You must use int(input(".."))

3
  • Thanks for the help! I immediately corrected the input from being string to integer right after posting. I am getting an error from the while loop " while position < len(intList) and not found:" says object of type int has no len().
    – NacDan
    Apr 9, 2017 at 4:08
  • You've assigned intList to just an int somehow. Debug your code, on your own, please Apr 9, 2017 at 4:11
  • Hint: Your parameter order is 1) list 2) number to find Apr 9, 2017 at 4:12
1

This method uses list comprehension, and it will account for any duplicates in the list as well. It assigns a list of index/indices where the key occurs in the list. Read more about list comprehension here.

l = [1, 2, 3, 4, 4, 6]
el = 4
search = [i for i in range(len(l)) if el==l[i]]
print(search)

output:

[3, 4]

alternatively,

def LinSearch(target, intList):
     search = [i for i in range(len(intList)) if target==intList[i]]
     return search
0
def linearSearch(intList,target):
    #print (target)
    found = False
    position = 0
    while position < len(intList):
        #print(intList[position])
        if intList[position] == target:
            found = True
            break
        position = position + 1

    return found

linearList = [3,5,9,7,6,12,15,9,1]
numInput = int(input("What number are you looking for? "))
numFound = linearSearch(linearList,numInput)
if numFound:
    print("The number is in index: ")
else:
    print("The number is not in the list")

Please take care of type conversion...

0

Linear Search :

 // funtion which rturns true if item found inside list.
    def linearSearch(list, value):
            for i in range(len(list)):
                if i == value:
                     return True

// Call above function pass list of values and item to search

    list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    item = 10
    print(linearSearch(list, item)) // item to search
0
def target(list,num):

    for x in list:
        if x == str(num):
            print("match found:"+x)
            break
    else:
         print('no match found')


list1 = ['6','4','7','9','0','2','3','1']
inp = input('Enter number to search:')
target(list1,inp)
0
def linear_search(list, key):

    for i, item in enumerate(list):
        if item == key:
            return i
    return -1

print(linear_search([4,5,2,7,1,8],7))

#If key is in the list returns its position in the list, otherwise returns -1.
1
  • Code only answers are discouraged in SO. Explanations add long term value, helps impart information and learning distinct from services), and keeps SO quality high. Future visitors should be able to learn & impart this to their own coding issues. Please consider editing to highlight features of your solution that address the OP's issue, explaining why it is useful or why you choose that approach, or provide insight that the OP missed. Especially for homework questions, where the point is for learning. Sometimes including a link to source documentation for an introduced functions is beneficial Jul 4, 2020 at 18:25
0
 n=int(input("Enter the number:"))
 list,n=[10,11,12,13,14,15],n
 if n in list:
     print("Element found at",list.index(n))
 else:
     print("Not Found")

Advice: In python index starts from 0. To start from 1 format the code like this: list.index(n)+1

0
def linearSearch(array,k):
    
    flag = False
    position = 0
    while position < len(intList) and not flag:
       
        if array[position] == k:
            flag = True
        else:
            position += 1

    return position

array = [3,5,9,7,6,12,15,9,1]
numInput = int(input("What number are you looking for? "))
numFound = linearSearch(linearList,numInput)
if numFound:
    print("The number is in index: ")
else:
    print("The number is not in the list")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.