TurkuPythonExercises / Week 6 Files and errors /[Fixed]15. Fix and copy calculations
KaiquanMah's picture
Rename Week 6 Files and errors/15. Fix and copy calculations to Week 6 Files and errors/[Fixed]15. Fix and copy calculations
c3d4cac verified
'''
The file calculations.csv contains totals. However, the file is corrupt - each invoice contains an error.
The errors can be either in one of the addends or in the result (but only in one of them). As a result of the error, one of the original numbers in the calculation has been replaced by random letters.
So, for example, if the calculation had originally been
2+4=6
...it could now be in the form, for example
2+XY=6
or
ZRQ+4=6
or even
2+4=UIJMN
Write a function
fix_and_copy()
which reads the invoices from the file calculations.csv, corrects them to their original form and then writes the corrected invoices in the same order to the file corrected_calculations.csv
'''
# approach 1
def fix_and_copy():
with open('calculations.csv', 'r') as source_file, open('corrected_calculations.csv', 'w') as dest_file:
for line in source_file:
# Strip whitespace and split the line
parts = line.strip().split('=')
left_side, result = parts
addends = left_side.split('+')
lhsdigit = addends[0].isdigit()
rhsdigit = addends[1].isdigit()
resultdigit = result.isdigit()
if lhsdigit == False:
lhs = int(result) - int(addends[1])
fixed_line = f"{lhs}+{addends[1]}={result}"
elif rhsdigit == False:
rhs = int(result) - int(addends[0])
fixed_line = f"{addends[0]}+{rhs}={result}"
else:
result = int(addends[0]) + int(addends[1])
fixed_line = f"{addends[0]}+{addends[1]}={result}"
# If we reach here, we've found the correct fix
dest_file.write(fixed_line + '\n')
# Call the function
fix_and_copy()
'''error
Error in program execution
line 135, in
fix_and_copy()
File "/tmp/untrusted/test064f7b918-53d7-4381-bdac-1616c59e0764/test.py", line 107, in fix_and_copy
with open('calculations.csv', 'r') as source_file, open('corrected_calculations.csv', 'w') as dest_file:
File "/tmp/untrusted/test064f7b918-53d7-4381-bdac-1616c59e0764/test.py", line 13, in open
if fname in content:
NameError: name 'content' is not defined
'''
############################################
# approach 2 - worked
# commented out fix_and_copy()
# some exercises want us to execute, some exercises dont want us to execute the function - so that was confusing
def fix_and_copy():
with open('calculations.csv', 'r') as source_file, open('corrected_calculations.csv', 'w') as dest_file:
for line in source_file:
# Strip whitespace and split the line
parts = line.strip().split('=')
left_side, result = parts
addends = left_side.split('+')
lhsdigit = addends[0].isdigit()
rhsdigit = addends[1].isdigit()
resultdigit = result.isdigit()
if lhsdigit == False:
lhs = int(result) - int(addends[1])
fixed_line = f"{lhs}+{addends[1]}={result}"
elif rhsdigit == False:
rhs = int(result) - int(addends[0])
fixed_line = f"{addends[0]}+{rhs}={result}"
else:
result = int(addends[0]) + int(addends[1])
fixed_line = f"{addends[0]}+{addends[1]}={result}"
# If we reach here, we've found the correct fix
dest_file.write(fixed_line + '\n')
#fix_and_copy()
'''expected and actual output
File calculations.csv contents:
VUQ+9=16
9+C=15
9+OFVI=17
5+3=B
ZWW+9=18
TAEFD+6=7
4+8=BKFE
WJN+9=14
1+PKOY=5
6+UBAO=15
7+9=WJE
5+J=10
W+6=9
4+7=UHQ
RLAI+3=6
7+9=GWI
8+9=OPJU
1+7=EHUDK
BJFLD+9=13
8+JL=12
2+KJDWE=10
9+9=LBTGY
BL+6=13
9+FYVO=11
Calling function...
fix_and_copy()
File corrected_calculations contents:
7+9=16
9+6=15
9+8=17
5+3=8
9+9=18
1+6=7
4+8=12
5+9=14
1+4=5
6+9=15
7+9=16
5+5=10
3+6=9
4+7=11
3+3=6
7+9=16
8+9=17
1+7=8
4+9=13
8+4=12
2+8=10
9+9=18
7+6=13
9+2=11
'''