473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

linear search

The search is always canceled after the first hit. How can I correct this? I dont want to cancel.


Expand|Select|Wrap|Line Numbers
  1. from random import randrange
  2.  
  3. # the max number of values
  4. NumberValue = 100
  5.  
  6. # an empty list for the values
  7. ListOfNumberValue = []
  8.  
  9.  
  10. found = False
  11.  
  12. search = 0
  13.  
  14. print("linear search")
  15.  
  16. runThrough = 1
  17. while runThrough <= NumberValue:
  18.     ListOfNumberValue.append(randrange(1, 201))
  19.     runThrough = runThrough + 1
  20.  
  21.  
  22. print("the values are: ")
  23. for value in ListOfNumberValue:
  24.     print(value, end = " ")
  25. print()
  26.  
  27. lookingFor = int(input("What to look for? "))
  28.  
  29.  
  30. while search < NumberValue and found == False:
  31.     if ListOfNumberValue[search] == lookingFor:
  32.         found = True
  33.     else:
  34.         search = search + 1
  35.  
  36. if found == True:
  37.     print("Value", lookingFor, "is at position", search + 1)
  38. else:
  39.     print("Value", lookingFor, "was not found.")
Nov 29 '20 #1
1 2695
Banfa
9,065 Expert Mod 8TB
This code is only meant to find the first occurance because
  1. It sets a flag as soon as it finds what your search for and uses that flag to halt the loop
  2. It uses a single variable, search, to store the found index which is also the loop counter so it can't store more than 1 result

To make if find multiple results then you need to
  1. Stop the loop using found as a stop condition
  2. Add a veritable, list or array, to store the results so there can be more than 1
  3. Change the output to use the results in the new variable
Nov 29 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
2
by: littlegirl | last post by:
hi guys can some one help me here i have to accept a number and preform a linear search for the numbers and if its not one of the number it has to say its invalid #include <iostream> using...
9
by: Rick | last post by:
I have a large list of objects where each object has a unique (non-overlapping) date range. The list is sorted chronologically. What is the most efficient way to search this list for a single...
2
by: sturgeob | last post by:
Yep. Newby. I am asked to find the closest value to user defined value if not right on the mark. I can get it to determin it I have selected one of the generated numbers, but am at a loss how to...
1
by: geebanga88 | last post by:
HI i have a method that performs a linear search to find a given vowel. The vowel parameter is a given vowel, while vowel is the array with all the vowels. public static void...
26
by: mike-yue | last post by:
The topic comes from a question: Would you rather wait for the results of a quicksort, a linear search, or a bubble sort on a 200000 element array? 1Quicksort 2Linear Search 3Bubble Sort ...
3
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
1
by: Mahjabeen | last post by:
what is parse error? #include<stdio.h> #include<conio.h> void main() { int i,a,key;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.