Searching and Sorting

Searching and Sorting

Instructions

Some data regarding products (product id, price and number of units in stock) are listed on your company’s web site. You can imagine the products to be something that you are interested in (they tell us that making assignments relevant makes them more interesting).

You can read this data directly from the web site using the following function (and you don’t even need to cite it). You have done this before — use what you learned in assignment 1 to make this work for this new case.

import urllib.request #this loads a library you will need. Put this line at the top of your file.

def readWordList():
response = urllib.request.urlopen(“http://www.cs.queensu.ca/home/cords2/data121.txt”)
html = response.readline()
data = html.decode(‘utf-8’).split()
return data

This file is accessing a web site and is reading the data in to the variable “html”. In order to put the data into the proper format, we need to “decode” the data. You don’t need to understand fully what’s going on here, but you can read more on the web if you wish here: https://docs.python.org/3/howto/urllib2.html.

I have given you this function so that you do NOT have to read the file from your own machine. You do NOT have to download the file and save it. The above code will read it directly from the web. Of course, this means that you must have internet access for your program to work.

After running the readHtml function, the variable “data” will contain a list of strings. You should be working with numbers – so you’ll need to change the data types at some point in your program.

You may only read the file once. All data manipulation must be done with the single data structure that you create when you read in the data.

The data file contains lines in the format: Part ID, Price, Number of Units in Stock.

Write a program that provides the user with the following functionality. You must provide a menu for the user and your program should be user friendly. The menu should start up when your program runs. This assignment is for you to get practice with searching and sorting so your solutions must use searching and sorting algorithms. The use of built-in functions such as “sort”, “index”, “count” and “in” are not permitted. (You may still use “in” in for statements such as “for i in range()”). You may use the searching and sorting algorithm implementations given in class.

1. Output the 5 most expensive items in the data set — print the item id and the price of the item. UPDATE: There are multiple items at the same price. You should include all items at the same price — so you are to output the 5 most expensive prices and the items associated with these prices.

2. Output the items with the least number of units in stock. (See update above! Same rule applies).

3. Ask the user for a part id. If the item is found in the data set, output the item id, the price and the number of units in stock for that time. If the item is not in the data set, print “Sorry, item is not available”. Remember, the use of the “in” operator is NOT allowed.

4. Print the part ids with prices between $5.00 and $10.00 (inclusive).

Order from us and get better grades. We are the service you have been looking for.