Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs

In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science major; so she recommended outsourcing the contract for writing the computer program to you. However, the timeline is short; and you only have less than three weeks to finish the job. The following is a description on the requirements:

General requirements:

o The program you write must be a Python 3.x program, it cannot be written in any other language, not even in Python 2.x. You have to provide source code, and the company will run your program on a standard Python 3 implementation. Your program should produce no errors and/or warnings during execution.

o Your program will be a console program. There is no need to produce a window-based graphical user interface.

o Your program will have a main function named main. It takes two parameters. The first one being the name of the input data file, and the second is the name of the output result file; both are strings. You can assume that both the input and output files can be open without any problem. Therefore, there is no need to check for the result of opening the files.

Input requirements:

o The input data file is guaranteed to contain only numbers and plus or minus sign, as well as the space and newline characters.

o The first line has only one (1) number, representing the number of items the system should manage. This number is guaranteed to be less than or equal to ten thousand (10,000).

o Each of the subsequence line is a transaction line which contains exactly two (2) numbers, separated by space characters. The first is an integer representing the item number (the items are numbered from 1 onward); and the second is an integer representing the quantity ordered by the branches. The two numbers are separated by one or more of space characters.

o The number of transaction lines is not known in advance but guaranteed to be less than or equal to 1 billion (1,000,000,000).

Output requirements:

o For each order line read, you need to echo the line to the output, using the format '%5d %10d'.

o If the order data is not valid, you need to output the reason why it is not valid on the same line of the echo output, using the format '%5d %10d %s'. All invalid orders must be echoed but ignored in the processing.

o The following explain the error conditions (in the following, n represents the number of items involved, and in the example outputs represents a space character):

If the item number is not between 1 and n, inclusively, the item is invalid: 141878invaliditemnumber

If the quantity is not a positive value, it is invalid: 61-610invalidquantity

If a line has multiple errors, only one error needed to be reported. The following shows the priority of errors, from high to low:

Invalid item.

Invalid quantity.

The following example shows the echoed line in case of multiple errors: -12-922invaliditemnumber

o After you have echoed all order lines, you should output the number of valid orders in a separate line. There should be a blank line before and after this line. The format should be '%10d'.

o After that, you need to output a summary for all items, if and only if there are any orders for the item. The format, again, should be '%5d %10d'.

o All lines in the output file should have no trailing space or tab characters

This is how I started it but it is not working.

def main(in_file,out_file):

fin = open(in_file,"r")

fout = open(out_file,"w")

valid = 0

count = fin.readline()

count = int(count)

print("Number of items : ",count)

#data = fin.readline()

summary_data = {}

for data in fin:

data = data.split()

first = int(data[0])

second = int(data[1])

i=0

if ((first > 0) and (first <= count) and (second > 0)):

print("%5d %10d"%(first,second))

fout.write("%5d %10d"%(first,second))

fout.write(' ')

if first in summary_data.keys():#This will check if already an

#item exist

summary_data[first]=second #This add the order amount.

else: #Otherwise we insert the order number to the summary data

summary_data[first]=second

valid +=1

elif (first > count):

print("%5d %10d %s"%(first,second,"invalid item number"))

fout.write("%5d %10d %s"%(first,second,"invalid item number"))

fout.write(' ')

elif (first <= 0):

print("%5d %10d %s"%(first,second,"invalid item number"))

fout.write("%5d %10d %s"%(first,second,"invalid item number"))

fout.write(' ')

elif (second <= 0):

print("%5d %10d %s"%(first,second,"invalid quantity"))

fout.write("%5d %10d %s"%(first,second,"invalid quantity"))

fout.write("%s %d"%(" Number of valid orders= ", valid))

print(" Number of valid orders= ", valid)

fout.write("Summary: ")#Summary of valid orders

print("Summary")

for key, value in sorted(summary_data.items()):#iterate the sorted dictionary.

fout.write("%5d %10d "%(key,value))

print("%5d %10d "%(key,value))

fout.close()

fin.close()

main(in_file,out_file)

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

2 The role of economic theory in economics.

Answered: 1 week ago