Spaces:
No application file
No application file
File size: 4,113 Bytes
57f0652 c3d4cac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
'''
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
''' |