Question

In: Computer Science

In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...

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: ··141········878·invalid·item·number

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

▪ 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·······-922·invalid·item·number

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

----The input file is data-small.txt:

100

17 254

3 87

48 643

42 387

71 724

3 811

42 80

86 254

0 -107

97 135

86 518

35 91

24 146

82 312

73 348

2 842

3 671

141 878

22 361

45 405

3 580

-2 -224

73 341

52 828

69 662

41 759

53 924

39 175

0 805

-1 127

40 70

178 -652

26 486

38 814

0 -144

61 -610

72 162

97 711

91 522

14 660

33 520

19 895

91 664

20 776

5 490

72 356

62 290

94 636

80 352

96 606

----The output file is result-small.txt:

17 254

3 87

48 643

42 387

71 724

3 811

42 80

86 254

0 -107 invalid item number

97 135

86 518

35 91

24 146

82 312

73 348

2 842

3 671

141 878 invalid item number

22 361

45 405

3 580

-2 -224 invalid item number

73 341

52 828

69 662

41 759

53 924

39 175

0 805 invalid item number

-1 127 invalid item number

40 70

178 -652 invalid item number

26 486

38 814

0 -144 invalid item number

61 -610 invalid quantity

72 162

97 711

91 522

14 660

33 520

19 895

91 664

20 776

5 490

72 356

62 290

94 636

80 352

96 606

Number of Valid Orders = 42

Summary:

2 842

3 2149

5 490

14 660

17 254

19 895

20 776

22 361

24 146

26 486

33 520

35 91

38 814

39 175

40 70

41 759

42 467

45 405

48 643

52 828

53 924

62 290

69 662

71 724

72 518

73 689

80 352

82 312

86 772

91 1186

94 636

96 606

97 846

----Additional information and requirement:

1. Your program should contain at least three (3) top-level functions (including the “main” function). You probably will end up having more than 3 functions.

2. You cannot have any import statement in your program.

3. You cannot have any global variables

4. You cannot have any class definition in your program (this assignment is NOT about object-oriented programming).

5. You cannot have anything other than top-level function definitions in your program (not even a call to the main function – the tester will call it).

6. Since you only know the number of items when you run the program, you will need some sort of lists or matrices with dynamic storage to hold data.

7. Since there can be potentially up to 1 billion transactions in the input data file. You cannot read them all into a list and process them later (doing so may cause an out-of-memory error), you must process them as you read them in.

8. Your program must be a Python 3.x program.

9. You can assume that the input file contains no format error

This is my Python Code:

in_file = "data-small.txt"
out_file = "result-small.txt"
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 = []
while data:
data = data.split()
first = int(data[0])
second = int(data[1])
if ((first > 0) and (first <= count) and (second > 0)):
print("%5d %10d"%(first,second))
fout.write("%5d %10d"%(first,second))
fout.write('\n')
i=0
for d in summary_data:
if d[0]>first:
break
i+=1
summary_data.insert(i, [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('\n')
elif (first <= 0):
print("%5d %10d %s"%(first,second,"invalid item number"))
fout.write("%5d %10d %s"%(first,second,"invalid item number"))
fout.write('\n')
elif (second <= 0):
print("%5d %10d %s"%(first,second,"invalid quantity"))
fout.write("%5d %10d %s"%(first,second,"invalid quantity"))
fout.write('\n')
data = fin.readline()
fout.write('\n')
fout.write("%s %d"%("Number of valid orders =",valid))
fout.write('\n')
fout.write('\n')
fout.write("Summary:\n") #summary of the valid orders
for d in summary_data:
fout.write("%5d %10d\n"%(d[0],d[1]))
fout.close()
fin.close()
main(in_file,out_file)

Question: I want it to print out the output like below:

----The output file is result-small.txt:

17 254

3 87

48 643

42 387

71 724

3 811

42 80

86 254

0 -107 invalid item number

97 135

86 518

35 91

24 146

82 312

73 348

2 842

3 671

141 878 invalid item number

22 361

45 405

3 580

-2 -224 invalid item number

73 341

52 828

69 662

41 759

53 924

39 175

0 805 invalid item number

-1 127 invalid item number

40 70

178 -652 invalid item number

26 486

38 814

0 -144 invalid item number

61 -610 invalid quantity

72 162

97 711

91 522

14 660

33 520

19 895

91 664

20 776

5 490

72 356

62 290

94 636

80 352

96 606

Number of Valid Orders = 42

Summary:

2 842

3 2149

5 490

14 660

17 254

19 895

20 776

22 361

24 146

26 486

33 520

35 91

38 814

39 175

40 70

41 759

42 467

45 405

48 643

52 828

53 924

62 290

69 662

71 724

72 518

73 689

80 352

82 312

86 772

91 1186

94 636

96 606

97 846

**Note**: Please do it in python as soon as possible**

Solutions

Expert Solution

#i've added a dictionary in your code for the summary data
in_file = "data-small.txt"
out_file = "result-small.txt"
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)
summary_data = {} #Use a dictonary for the 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('\n')
if first in summary_data.keys(): #first we check if already a item number exists in our dictionary
summary_data[first]+=second # We then add the order amount to the existing value
else: # otherwise we insert the order number and order value 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('\n')
elif (first <= 0):
print("%5d %10d %s"%(first,second,"invalid item number"))
fout.write("%5d %10d %s"%(first,second,"invalid item number"))
fout.write('\n')
elif (second <= 0):
print("%5d %10d %s"%(first,second,"invalid quantity"))
fout.write("%5d %10d %s"%(first,second,"invalid quantity"))
fout.write("%s %d"%("\nNumber of valid orders =\n",valid))
print("\nNumber of valid orders =",valid,"\n")
fout.write("Summary:\n") #summary of the valid orders
print("Summary:")
for key,value in sorted(summary_data.items()): #we then iterate the sorted dictionary of sumary data and display records
fout.write("%5d %10d\n"%(key,value))
print("%5d %10d"%(key,value))
fout.close()
fin.close()
main(in_file,out_file)


Related Solutions

For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and plaintext a. Show the plaintext as a 4x4 matrix b. Show the result after adding RoundKey0 c. Show the result after SubBytes d. Show the result after ShiftRows e. Show the result after MixColumns f. Show the result after first round
Write a program in python that corrects misspelled words. The input for the program is either...
Write a program in python that corrects misspelled words. The input for the program is either a string or a list of strings. The output should be a string or a list depending on the input and should have the spellings corrected. The speller should be able to correct at least the words listed below. You are free to use any data structures we have covered so far including lists and dictionaries. Your program should be in autocorrect.py. Here is...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions. Name your output file “output.txt”. Ensure the program consists of a main function and at least one other function. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen: output the text of the file to screen output the number of words in the file...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT