The function will prompt the user to enter a search

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ion. 2 CS 116 Fall 2012 Assignment 08 Due at 4:00pm Monday, November 19 4. Linear search on a list is reasonably efficient when what you are searching for is near the beginning of the list. It is possible to improve the long ­term efficiency of a linear search function by moving frequently searched for values closer to the beginning of list. One specific strategy is to mutate the list each time you find a value by moving that value one index position closer to 0 (assuming that the value is not already at the beginning of the list). Write a function interactive_linear_search that consumes a non ­empty list of strings called database. The first k elements in database will be non ­ empty strings that are unique values. The rest of the elements of the list will be empty strings, that are placeholders for values that may be added to database later. The function will prompt the user to enter a search value. The prompting will continue until the user enters the empty string (i.e. just presses the enter key). During the user interaction, one of the following scenarios will occur: • If the search value exists in database o a message indicating where the value was found is printed to the screen o database is modified to move the value that has just been found, one index position closer to the beginning of the list • If the search value does not exist in database and there is room (i.e. database contains at least one empty string) o a message indicating that the value was not found but it has been added to the list, is printed to the screen o database list is modified to include this new value at the first available position (i.e. replacing the empty string that is closest to the beginning of the list). • If the search value does not exist in database but there is no room (i.e. no empty strings in database) o a message indicating that the list is full is printed to the screen o database is unchanged Note that the user might search for a value and not find it, but then search for the value a second time and find it in database. 3 CS 116 Fall 2012 Assignment 08 Due at 4:00pm Monday, November 19 For example, suppose you have a list called db with the values [“a”, “b”, “c”, “”, “”, “”], and then had the following interaction after calling interactive_linear_search(db). The messages printed to the screen are in bold, the values entered by the user are in italics, and the user en...
View Full Document

Ask a homework question - tutors are online