KaiquanMah commited on
Commit
c3d4cac
·
verified ·
1 Parent(s): 57f0652

Rename Week 6 Files and errors/15. Fix and copy calculations to Week 6 Files and errors/[Fixed]15. Fix and copy calculations

Browse files
Week 6 Files and errors/{15. Fix and copy calculations → [Fixed]15. Fix and copy calculations} RENAMED
@@ -76,3 +76,108 @@ if fname in content:
76
  NameError: name 'content' is not defined
77
  '''
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  NameError: name 'content' is not defined
77
  '''
78
 
79
+
80
+
81
+ ############################################
82
+
83
+
84
+ # approach 2 - worked
85
+ # commented out fix_and_copy()
86
+ # some exercises want us to execute, some exercises dont want us to execute the function - so that was confusing
87
+
88
+ def fix_and_copy():
89
+ with open('calculations.csv', 'r') as source_file, open('corrected_calculations.csv', 'w') as dest_file:
90
+ for line in source_file:
91
+ # Strip whitespace and split the line
92
+ parts = line.strip().split('=')
93
+ left_side, result = parts
94
+
95
+ addends = left_side.split('+')
96
+
97
+ lhsdigit = addends[0].isdigit()
98
+ rhsdigit = addends[1].isdigit()
99
+ resultdigit = result.isdigit()
100
+
101
+ if lhsdigit == False:
102
+ lhs = int(result) - int(addends[1])
103
+ fixed_line = f"{lhs}+{addends[1]}={result}"
104
+ elif rhsdigit == False:
105
+ rhs = int(result) - int(addends[0])
106
+ fixed_line = f"{addends[0]}+{rhs}={result}"
107
+ else:
108
+ result = int(addends[0]) + int(addends[1])
109
+ fixed_line = f"{addends[0]}+{addends[1]}={result}"
110
+
111
+
112
+ # If we reach here, we've found the correct fix
113
+ dest_file.write(fixed_line + '\n')
114
+
115
+
116
+ #fix_and_copy()
117
+
118
+
119
+
120
+
121
+
122
+
123
+ '''expected and actual output
124
+
125
+ File calculations.csv contents:
126
+ VUQ+9=16
127
+ 9+C=15
128
+ 9+OFVI=17
129
+ 5+3=B
130
+ ZWW+9=18
131
+ TAEFD+6=7
132
+ 4+8=BKFE
133
+ WJN+9=14
134
+ 1+PKOY=5
135
+ 6+UBAO=15
136
+ 7+9=WJE
137
+ 5+J=10
138
+ W+6=9
139
+ 4+7=UHQ
140
+ RLAI+3=6
141
+ 7+9=GWI
142
+ 8+9=OPJU
143
+ 1+7=EHUDK
144
+ BJFLD+9=13
145
+ 8+JL=12
146
+ 2+KJDWE=10
147
+ 9+9=LBTGY
148
+ BL+6=13
149
+ 9+FYVO=11
150
+
151
+
152
+ Calling function...
153
+ fix_and_copy()
154
+
155
+
156
+ File corrected_calculations contents:
157
+ 7+9=16
158
+ 9+6=15
159
+ 9+8=17
160
+ 5+3=8
161
+ 9+9=18
162
+ 1+6=7
163
+ 4+8=12
164
+ 5+9=14
165
+ 1+4=5
166
+ 6+9=15
167
+ 7+9=16
168
+ 5+5=10
169
+ 3+6=9
170
+ 4+7=11
171
+ 3+3=6
172
+ 7+9=16
173
+ 8+9=17
174
+ 1+7=8
175
+ 4+9=13
176
+ 8+4=12
177
+ 2+8=10
178
+ 9+9=18
179
+ 7+6=13
180
+ 9+2=11
181
+
182
+
183
+ '''