Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python programming Add comments in the codes Part 1: Create a list with a minimum of 35 elements using random number generation. Numbers should be

Python programming

Add comments in the codes

image text in transcribedimage text in transcribed
Part 1: Create a list with a minimum of 35 elements using random number generation. Numbers should be in the range of 1 to 5000. The element should be out of order (unsorted). Implement the linear search algorithm as a function, linear(), to find a key entered by the user. Prompt the user, in main(), for a key to search in the list. Pass the list and the key to function linear() from main(). Search should find the first occurrence of the key in the list. If found, display an appropriate message with the key and location of key. Keep track of the number of comparisons it took to find the key. Your program should display the key and the index position where it was found in the list as well as the number of comparisons it took to find the element. Print an appropriate message if the key is not found. Display the list with the search results. NOTE: You may not use any built-in searching functions, methods or operators in Python. Sample Output: Example scenario if key is found: if the key I am looking for is 422, and I find it at index 20, my program should print that I found the key 422 at index 20, and it took me 20 comparisons to find it. Then, I should print the list that it searched for the key in. Example scenario if key is not found: if the key I am looking for is 422 and my program can't find it, I should print that the key was not found, and then print the list that it searched for the key in.Part 2: Sort the list created in Part1 in main(). Pass the sorted list and the same key(s) entered by the user in Part 1 to the function linear(). Find the first occurrence of the key in the list. If found, display an appropriate message with the key and location of key. Keep track of the number of comparisons it took to find the key. Your program should display the key and the index position where it was found in the list as well as the number of comparisons it took to find the element. Print an appropriate message if the key is not found. NOTE: You may use the sort() method of lists in Python for Part 2. Test your program with at least 4 key values, What conclusions can you draw based on the number of comparisons it takes to find the key in a sorted vs. unsorted list

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions