-1

I need to write a program that does linear search on a character within a sentence. I have to do it without using any inbuilt functions except for print().

The program should output what index the character is at.

If the character isn't in the sentence it should output -1 as the index.

My code outputs the following:

Enter a sentence: Hello
Enter a character: e
The character is -1.
The character is 1 on the index.

Even though it should only output:

Enter a sentence: Hello
Enter a character: e   
The character is 1 on the index.

Below is my code:

def linear_search(intList,target):
    found = False
    count = 0
    while count < len(intList):
        if intList[count] == target:
            print("The character is", count, "on the index.")
            found = True
            break
        if intList[count] != target:
            print("The character is -1.")
            count = count + 1

    return count

sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)

Many thanks for your help!

Luuklag
  • 3,897
  • 11
  • 38
  • 57
James
  • 55
  • 7
  • If any of the answers solve your problem, it's good practice to accept them. Accepting will also give you a small rep boost – Alec Mar 23 '19 at 05:58

2 Answers2

1

The problem is in the while loop.

The lines are:

if intList[count] != target:
        print("The character is -1.")
        count = count + 1

Here, if the character is not the same as the target, it will immediately print out "The character is -1". However, you want to do it after looking through every element in the string, not when it just hits one character that isn't the same.

What you want is for it to print at the end, so instead, your linear_search function should look more like this:

def linear_search(intList,target):
    found = False
    count = 0
    while count < len(intList):
        if intList[count] == target:
            print("The character is", count, "on the index.")
            found = True
            return count

        count = count + 1

   print("The character is -1")
   return -1

Another way to do it in less code and without inbuilt functions would be to use a for loop, like this:

def linear_search(intList, target):
    for char in intList:
        if char == target:
           print("The character is", count, "on the index.")
           return count
        count += 1
    print("The character is -1")
    return -1
asong24
  • 33
  • 6
1

Your problem is that you output the undesired result before checking the entire string. You can solve this problem pretty easily by simply checking if the character was found after the conclusion of the while loop.

def linear_search(intList,target):
        found = False
        count = 0
        while count < len(intList):
            if intList[count] == target:
                print("The character is", count, "on the index.")
                found = True
                break
            else: count += 1
        if not found:
            print("The character is -1.")

    return count

sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)
Alec
  • 8,529
  • 8
  • 37
  • 63