Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a Python program to solve the following problem. The data is stored in a file called inventory.txt . Each line in this file begins
Write a Python program to solve the following problem. The data is stored in a file called inventory.txt Each line in this file begins with the day of the week Sunday Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday followed by a comma followed by the item name apples bananas, meat, or fish followed by another comma followed by the amount of kilograms displayed for that item on that day a positive floatingpoint number followed by another comma followed by the quantity of kilograms purchased by customers for that item on that day a positive floatingpoint number
Any line that deviates from this format is considered an error, including lines with extra spaces between the commaseparated values. Additionally, each unique dayitem pair should appear only once in the file; repeated dayitem pairs are also errors. Missing dayitem pairs are not considered errors, as some days the store may be closed or certain items may not be available. It's also important to check that the purchased kilograms for an item on a specific day are less than the displayed kilograms for that item on that day; if this rule is unmet, this should be considered yet another error in the data file.
Task
Your task is to read the data from inventory.txt If there are no errors when reading the data, the program should prompt the user to provide the desired maximum ratio of unsold items on display call it maxratio This value should be a floatingpoint number between and For each dayitem pair, your program should calculate the ratio of unsold items on display. If this computed ratio is higher than the value in maxratio, your program should retain the available and computed information for this dayitem pair.
To extract the data from inventory.txt your program should have a function named readinventorydata, which takes a single parameter the name of the file containing the data. This function should return a list of dictionaries. Each dictionary should contain the keys dayofweek, itemname, unsoldkg calculated by subtracting the purchased kilograms from the displayed kilograms and ratiounsold calculated by dividing unsoldkg by the displayed kilograms but only if the data read from inventory.txt is valid. If there are any errors while reading data from the file, this function should raise an exception. Your main program should call this function and handle any exceptions, ensuring that the application does not crash but rather prints error messages and exits gracefully see examples of interactions below
To determine which dayitem pair would benefit most from reviewing displayed quantities, you should sort the dayitem pairs with ratiounsold values above the maxratio threshold value. Your program should have a function sortinventorydata that sorts the identified dayitem pairs based on the their corresponding value of ratiounsold from highest to lowest ratiounsold value. Dayitem pairs having same ratiounsold values should be sorted first by the day of the week Sunday being the first day and Saturday the last and then by item in ascending alphabetical order ie apples, bananas, fish, and then finally meat
Note: Your solution must include the two functions specified above, and can include any other functions, if you need so There is no requirement to use any particular sorting algorithm for this assignment. Any algorithm that gives the correct result without excessively slowing down or halting the computation can be used.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started