Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For Python 3.7+. Step 1: Copy and paste this code. Get familiar with what it does. You will use this as a base: def loadItems():

For Python 3.7+.

Step 1: Copy and paste this code. Get familiar with what it does. You will use this as a base:

def loadItems():

fp = open("A1input.txt")

itemsList = []

for line in fp.readlines():

itemsList.append(line.strip().split(","))

fp.close()

return itemsList

def displayItems(itemsList):

for item in itemsList:

print(item)

itemList = loadItems() # invoke function to read the file into a list, return the list

displayItems(itemList) # takes list as input and prints it

Step 2: Create a .txt in the same directory called A1inputs.txt. Copy the following in:

B001,book,Patriot Games, 15.95

B002,book,Origin, 19.95

C001,cloth,Armani Suit, 800.00

B003,book,Animal Farm, 9.97

B004,book,Grant, 22.50

E001,elect,EyePhone 10,795.00

E002,elect,First Alert Smoke Alarm, 29.95

F001,food,Moose Drool Ale 6-pack, 9.95

C002,cloth,Pants, 39.95

B005,book,Prairie Fires, 18.95

E003,elect,Sony Portable Radio, 15.00

C003,cloth,Vasque Hiking Boots, 109.00

C004,cloth,Wool Hat, 14.00

F002,food,Jumbo shrimp, 12.50

E004,elect,HP Laptop, 350.00

Step 3: After you did Step 1 and 2, I want you to finish the rest of my assignment, which is to create 2 additional functions and then call them in the global code. The first function should build a "reverse" dictionary consisting of each item type as key and all items in that category as the value. The second function should create an output file and write each item to it by iterating through the dictionary and writing by category + item.

Additional instructions for the first function I want you to make:

image text in transcribed

Additional instructions for the second function I want you to make:

image text in transcribed

Step 4: Use the following global code to call your two functions after you're done:

DS = buildDict(itemList) # builds and returns the reverse dictionary from the input list

writeFile(DS) # creates output file and writes records to it

Reverse Dictionary The 'buildDict()' function reads each inventory item from the input list and takes one of the following actions: 1. if the 'category of the item read is not already in the dictionary, that category is added as a key and the inventory number, description, and price are made into a list and inserted as a value of that category's key 2. if the category' of the item read is already in the dictionary, the inventory number, description, and price are made into a list and appended to that key's value. The resulting dictionary has as many entries as categories. Each entry is a list of lists. The master list includes everything for the corresponding category, while each sub-list is the data for one particular inventory item. An example of the dictionary for the 'book'category: {book: [['B001', Patriot Games', 15.95], [B002', 'Origin, 19.95), ... [B006', Future Shock, 8.95]]} Output File The output file is a text file consisting of comma-separated data. The file name you create is "Aloutput.txt" (example: "smithfAloutput.txt"), written to your default drive and directory. There is one line per data item. Each line consists of (1) category, (2) inventory number, (3) description, and (4) price. The reverse dictionary will supply these to your function. Iterating through the reverse dictionary will yield lines like the ones below. When writing to the file write out the dictionary key followed by inventory number, description, and price (brackets '[' and ']' will not be written to the output file). The output is a set of comma-separated text strings. book,B001,Patriot Games, 15.95 book,B002,Origin, 19.95 book,B003,Animal Farm, 9.97 book,B004, Grant, 22.50 book,B005,Prairie Fires, 18.95 book,B005, Ragtime, 17.25 book,B006,Future Shock, 8.95 cloth, C001,Armani Suit, 800.00 cloth, C002,Pants, 39.95 cloth, C003, Vasque Hiking Boots, 109.00 cloth, C004, Wool Hat, 14.00 cloth, C005, Wrangler Jeans, 24.50 cloth, C006,Nike T-shirt, 19.00 cloth, C007, Gore-Tex Gloves, 39.00 cloth, C008, North Face Fleece Jacket, 89.95 cloth, C009, Nationals Logo Sweatshirt, 49.00 cloth, C010, New Balance Trail Runners', 69.95 elect,E001, EyePhone 10, 795.00 elect,E002,First Alert Smoke Alarm, 29.95

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

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

Describe how the data that is stored in a table is organized.

Answered: 1 week ago

Question

=+18.7. Reconsider Problem 12.12.

Answered: 1 week ago