9.22 City Search (Part B, Linear Search) In this part, we will search through the data, implementing linear search. In the next part, we will employ binary search. Implement linear_search Add the following function definition to your program: def linear_search (search_list, value_to_find) : ***Uses linear search to find the position of an item in a list. Parameters: search_list (list): The list. value_to_find (str): The item to search for. Returns: int: The position of the item in the list, or None if not found. pass Write code in linear_search to do the following: • Loop over the items in your list • Compare each item, in turn, to value_to_find If an item matches the value you are trying to find, return its position in the list immediately. You do not need to explicitly return None, but you should make sure that your code does not return anything if the item is not in the list. Complete main In main, add code to do the following: Prompt the user to enter name of a city. • Call your linear search function to get the position of the city entered by the user. • Print the result. Sample Input/Output Sample output when the file provided is cities-small.txt and the city provided is Rohnert Park Number of lines in file: 5 0: Santa Rosa 1: Petaluma 2: Rohnert Park 3: Windsor 4: Healdsburg (Linear Search) The position of Rohnert Park is 2 Sample output when the file provided is cities-small.txt and the city provided is Cotati Number of lines in file: 5 0: Santa Rosa 1: Petaluma 2: Rohnert Park 3: Windsor 4: Healdsburg (Linear Search) The position of Cotati is None

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need a better understanding of this question 

9.22 City Search (Part B, Linear Search)
In this part, we will search through the data, implementing linear search. In the next part, we will employ binary search.
Implement linear_search
Add the following function definition to your program:
def linear_search (search_list, value_to_find):
***Uses linear search to find the position of an item in a list.
Parameters:
search_list (list): The list.
value_to_find (str): The item to search for.
Returns:
int: The position of the item in the list, or None if not found.
pass
Write code in linear_search to do the following:
•
Loop over the items in your list
• Compare each item, in turn, to value_to_find
• If an item matches the value you are trying to find, return its position in the list immediately. You do not need to explicitly return None,
but you should make sure that your code does not return anything if the item is not in the list.
Complete main
In main, add code to do the following:
• Prompt the user to enter name of a city.
•
Call your linear search function to get the position of the city entered by the user.
• Print the result.
Sample Input/Output
Sample output when the file provided is cities-small.txt and the city provided is Rohnert Park
Number of lines in file: 5
0: Santa Rosa
1: Petaluma
2: Rohnert Park
3: Windsor
4: Healdsburg
(Linear Search) The position of Rohnert Park is 2
Sample output when the file provided is cities-small.txt and the city provided is Cotati
Number of lines in file: 5
0: Santa Rosa
1: Petaluma
2: Rohnert Park
3: Windsor
4: Healdsburg
(Linear Search) The position of Cotati is None
Transcribed Image Text:9.22 City Search (Part B, Linear Search) In this part, we will search through the data, implementing linear search. In the next part, we will employ binary search. Implement linear_search Add the following function definition to your program: def linear_search (search_list, value_to_find): ***Uses linear search to find the position of an item in a list. Parameters: search_list (list): The list. value_to_find (str): The item to search for. Returns: int: The position of the item in the list, or None if not found. pass Write code in linear_search to do the following: • Loop over the items in your list • Compare each item, in turn, to value_to_find • If an item matches the value you are trying to find, return its position in the list immediately. You do not need to explicitly return None, but you should make sure that your code does not return anything if the item is not in the list. Complete main In main, add code to do the following: • Prompt the user to enter name of a city. • Call your linear search function to get the position of the city entered by the user. • Print the result. Sample Input/Output Sample output when the file provided is cities-small.txt and the city provided is Rohnert Park Number of lines in file: 5 0: Santa Rosa 1: Petaluma 2: Rohnert Park 3: Windsor 4: Healdsburg (Linear Search) The position of Rohnert Park is 2 Sample output when the file provided is cities-small.txt and the city provided is Cotati Number of lines in file: 5 0: Santa Rosa 1: Petaluma 2: Rohnert Park 3: Windsor 4: Healdsburg (Linear Search) The position of Cotati is None
2
3
4
o
6
7
8 if
9
10
11
12
13
14
15
22
def readfile(filename):
16
with open (filename, "r") as file:
lines file.readlines ()
return lines
def print_list(list_to_print):
for i, item in enumerate(list_to_print):
print(i, ": ", item, sep="")
"__main__":
name
==
filename=input()
file_contents = readfile(filename)
file_contents = [line.rstrip() for line in file_contents]
print("Number of lines in file:", len(file_contents))
print_list(file_contents)
Transcribed Image Text:2 3 4 o 6 7 8 if 9 10 11 12 13 14 15 22 def readfile(filename): 16 with open (filename, "r") as file: lines file.readlines () return lines def print_list(list_to_print): for i, item in enumerate(list_to_print): print(i, ": ", item, sep="") "__main__": name == filename=input() file_contents = readfile(filename) file_contents = [line.rstrip() for line in file_contents] print("Number of lines in file:", len(file_contents)) print_list(file_contents)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education