Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Dictionary exercise Purpose: Use User Defined Types and use Dictionary in

ID: 3894769 • Letter: P

Question

Python Dictionary exercise

Purpose: Use User Defined Types and use Dictionary instead of list

Create a User Defined Type to manage this data, (data excerpt):

Specification:

* Read the data file, and for each product in the data file, sort it in an alphabetic list, based on the first letter of the product. For example, store "Shampoo,7.35" in the 'S' list.

* After reading in the entire data file, sort each alphabetic list.

* After sorting each list, place each list in a Master List, starting with list [A] and ending in list [Z].

* Write a searching algorithm to search for an item in the Master List. For example, find "Shampoo" in the Master List [S] position.

Notes:

1. There are duplicates in the data file, but have different prices. For example, Salmon,16.39,Salmon.14.72. Your program doesn't need to handle the duplicates, but you can if you want.

2. One student suggested that putting all the data into one big list, and then doing a Binary Search to find the data would be a more efficient algorithm, which is true. However, this exercise is intended to teach handling lists rather than sorting techniques. Insidently, a BinarySearch would take about 8 searches to find "Shampoo", where the searching method, outlined in this assignment, would take about 16 searches.

Explanation / Answer

ScreenShot

---------------------------------------------------

Program

#import for csv read and math function
import csv
import math
#function binary search
def binarySearch(mydict,string):
    #find middle index of dictionary
    m=math.ceil(len(mydict)/2)
    #compare middle =search then display price
    if (list(mydict.keys())[m]==string):
        print("Product Found and it's value is",mydict[string])
    #otherwise until left
    elif(list(mydict.keys())[m]<string):
        for i in range(0,m-1):
            if(list(mydict.keys())[i]==string):
                print("Product Found and it's value is",mydict[string])
                break
    #Otherwise until right
    elif(list(mydict.keys())[m]>string):
        for i in range(m,len(mydict)-1):
            if(list(mydict.keys())[i]==string):
                print("Product Found and it's value is",mydict[string])
                break

     #not found         
    else:
        print("Product not found");
#main Function
def main():
#dictionary declaration
mydict = {}
#file read into dictionary
with open('C:/Users/deept/Desktop/UPRODUCTS.csv') as csvfile:
      readCSV = csv.reader(csvfile, delimiter=',')
      for row in readCSV:
          pName = row[0]
          pPrice = row[1]
          mydict[pName]=pPrice
#print dictionary before sort
print("Dictionary Before sort:")
print(mydict)
#print dictionary after sort
print("Dictionary after sort:")
mydict=dict(sorted(mydict.items(), key=lambda x: x[0]))
print(mydict)
#call search function
binarySearch(mydict,'Shampoo')
#main
if __name__== "__main__":
main()        

-----------------------------------------------

Output

Dictionary Before sort:
{'Shampoo': '7.35', 'Roast beef': '20.67', 'Shrimp': '17.92', 'Egg Nog': '11.82', 'Popsicles': '3.77'}
Dictionary after sort:
{'Egg Nog': '11.82', 'Popsicles': '3.77', 'Roast beef': '20.67', 'Shampoo': '7.35', 'Shrimp': '17.92'}
Product Found and it's value is 7.35