Lecture 37: The lab that you got because you complained about LAB8

posted Apr 9, 2013, 5:58 AM by Samuel Konstantinovich   [ updated Apr 9, 2013, 11:02 AM ]
You are getting a file in.txt that contains 10000 arithmetic expressions. There are only four operations, +,-,*, and /. There are no divide by zero problems. 

The format of the file is:
expressions:
7043.7 + 8569.0
5821.8 + 2492.9
7752.3 * 8418.6
1276.7 / 9034.3
...
9428.0 / 8537.3
5211.3 * 8576.4
7865.4 + 1698.8
1673.2 / 7535.9
883.8 * 6028.5

Your job is to make a new file out.txt in this format: 
results:
7043.7 + 8569.0 = 15612.7
5821.8 + 2492.9 = 8314.7
7752.3 * 8418.6 = 65263512.78
1276.7 / 9034.3 = 0.14131698084
...
5211.3 * 8576.4 = 44694193.32
7865.4 + 1698.8 = 9564.2
1673.2 / 7535.9 = 0.222030547114
883.8 * 6028.5 = 5327988.3

There are about 100,000 expressions. You have two options:
A. You can distribute the problems to all of the students in my classes. If you each take 620 problems, you can post them all on facebook, merge them together and make the correct answer file. This requires a consensus otherwise you will end up with well over 1500 problems each. It also requires that you maintain the correct order of problems when you merge, which can be tricky. Finally one person has to actually do the merging which is a bit of a pain. Honestly you should write a program to do the merging, but if you could do that then you could do option B.

B. You can use Python to open the out.txt file in 'w' mode. Then use   file.write(string) to put the answers there.


example how to write to a file:
f=open('a.txt','w')
f.write("This is a line of txt\n")
for i in range(100):
   f.write(str(i)+","+str(i**2))
f.close()

ċ
in.txt
(1638k)
Samuel Konstantinovich,
Apr 9, 2013, 6:02 AM
Comments