-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatCheckerFiles.py
More file actions
327 lines (281 loc) · 11 KB
/
formatCheckerFiles.py
File metadata and controls
327 lines (281 loc) · 11 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# This is a long one -- our answer is 20 lines of code, but
# yours will probably be longer. That's because it's one of the
# more authentic problems we've done so far. This is a real
# problem you'll start to face if you want to start creating
# useful programs.
#
# One of the reasons that filetypes work is that everyone
# agrees how they are structured. A ".png" file, for example,
# always contains "PNG" in the first four characters to
# assure the program that the file is actually a png. If these
# standards were not set, it would be hard to write programs
# that know how to open and read the file.
#
# Let’s define a new filetype called ".cs1301".
# In this file, every line should be structured like so:
#
# number assignment_name grade total weight
#
# In this file, each component will meet the following
# description:
#
# - number: an integer-like value of the assignment number
#
# - assignment_name: a string value of the assignment name
#
# - grade: an integer-like value of a student’s grade
#
# - total: an integer-like value of the total possible
# number of points
#
# - weight: a float-like value ranging from 0 to 1
# representing the percent of the student’s grade this
# assignment is worth. All the weights should add up to 1.
#
# Each component should be separated with exactly one space.
# A good sample file is available to view as
# "sample.cs1301".
#
# Write a function called format_checker that accepts a
# filename and returns True if the file contents accurately
# conform to the described format. Otherwise the function
# should return False. In other words, it should return True
# if:
#
# - Each line has five elements separated by spaces, AND
# - The first, third, and fourth elements are integers, AND
# - The fifth element is a decimal number, AND
# - All the fifth elements add to 1.
#
# You can make changes to test.cs1301 to test your function,
# or test it with sample.cs1301. Right now, running it on
# sample.cs1301 should return True, and on test.cs1301
# should return False.
#
# Hint 1: .split() will likely help separate each line into
# its components.
# Hint 2: .split() returns a list. So, if you were to do
# something like say split_line = line.split(), then
# split_line[0] would give the first item, split_line[1] would
# give the second item, etc.
# Hint 3: If you're having trouble, try breaking it down by
# parts. First check the file to see if it has the right
# number of items per line, then whether the items are of
# the correct type, then whether the fifth elements add to
# 1. Remember, you know how to do each individual check
# (checking types, adding numbers, finding list lengths) --
# the hard part is knitting this all together into one bigger
# solution.
# saved file code sample_1.cs1301:
with open("sample_1.cs1301", "w") as sample_1:
sample_1.write('''1 assignment_1 85 100 0.05
2 assignment_2 80 100 0.05
3 assignment_3 95 100 0.05
4 assignment_4 95 100 0.05
5 assignment_5 80 100 0.05
6 exam_1 85 86 0.1
7 assignment_6 100 100 0.05
8 assignment_7 97 100 0.05
9 exam_2 89 100 0.1
10 assignment_8 93 100 0.05
11 assignment_9 99 100 0.05
12 exam_3 92 100 0.1
13 final_exam 95 100 0.25''')
with open("sample_2.cs1301", "w") as sample_2:
sample_2.write('''Hey kid!
Did you know that dragons love tacos?''')
# Write your function here!
def format_checker(format):
with open(format, "r") as my_sample_file:
# use try/except to catch errors and overwrite them.
try:
adval5 = 0
for line in my_sample_file:
line_a = line.strip("\n")
line_b = line_a.split(" ") # Creates a list
[val_1, val_2, val_3, val_4, val_5] = line_b # unpack list to b
adval5 += float(val_5) # all the floats add to 1.0
line_c = val_1 + val_3 + val_4 # convert item 1, 3 and 4 to int
int(line_c)
if float(adval5) == 1.0: # check if they ad to 1.0
return True
else:
return False
except:
return False
my_sample_file.close()
# Test your function below. With the original values of these
# files, these should print True, then False:
print(format_checker("sample_1.cs1301"))
print(format_checker("sample_2.cs1301"))
print()
# Write a function called "reader" that reads in a ".cs1301"
# file described in the previous problem. The function should
# return a list of tuples representing the lines in the file like so:
#
# [(line_1_number, line_1_assignment_name, line_1_grade, line_1_total, line_1_weight),
# (line_2_number, line_2_assignment_name, line_2_grade, line_2_total, line_2_weight)]
#
# All items should be of type int except for the name (string)
# and the weight (float). You can assume the file will be in the
# proper format -- in a real program, you would use your code
# from the previous problem to check for formatting before
# trying to call the function below.
#
# Hint: Although you could use readlines() to read in all
# the lines at once, they would all be strings, not a list.
# You still need to go line-by-line and convert each string
# to a list.
# create file here:
# write to file:
with open("sample.cs1301", "w") as sample_file:
sample_file.write('''1 assignment_1 85 100 0.25
2 test_1 90 100 0.25
3 exam_1 95 100 0.5''')
# Write your function here!
def reader(my_grade_file):
tuple_result = []
with open(my_grade_file, "r") as new_read_file:
for lines in new_read_file:
all_lines = lines.split(" ")
tuples_list = int(all_lines[0]), all_lines[1], int(all_lines[2]), int(all_lines[3]), float(all_lines[4])
tuple_result.append(tuples_list)
return tuple_result
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print:
# [(1, 'assignment_1', 85, 100, 0.25), (2, 'test_1', 90, 100, 0.25), (3, 'exam_1', 95, 100, 0.5)]
print(reader("sample.cs1301"))
print()
# Write a function called write_1301 which will write a file
# in the format described in Coding Problem 4.4.2. The
# sample.cs1301 file has been included to refresh your
# memory. Your function should accept two arguments:
# A string of a filename to write to, and a list of tuples.
# You can assume that each tuple will have the following
# format:
#
# (int, str, int, int, float)
#
# Each tuple will represent a line in the file, and each
# item in the tuple should correspond to the
# assignment number, the assignment name, the student's
# grade, the total possible number of points, and the
# assignment weight respectively.
# Write your function here!
def write_1301(filename, tupleList):
outputFile = open(filename, "w")
b = []
for item in tupleList:
for val in item:
b.append(val)
print(b[0], b[1], b[2], b[3], b[4], file=outputFile)
b = []
outputFile.close()
# The code below will test your function. It's not used
# for grading, so feel free to modify it! You may check
# output.cs1301 to see how it worked.
tuple1 = (1, "exam_1", 90, 100, 0.6)
tuple2 = (2, "exam_2", 95, 100, 0.4)
tupleList = [tuple1, tuple2]
write_1301("output.cs1301", tupleList)
# Another method>>>
def write_1301(filename, tuple_list):
# Like the last problem, this problem can be done
# very easily, or it can be more difficult. Don't
# worry if it took you a while to get -- it just
# takes practice!
#
# To start, we want to open the file in write
# mode:
file_writer = open(filename, "w")
# Next, we want to go through each tuple in the
# list. These tuples will each become a line in
# the file, so we'll use 'line' as our loop
# variable:
for line in tuple_list:
# line will be a tuple with five items, and
# we want to print each one to the file
# separated by spaces. However, they're not
# all strings, so we can't use
# " ".join(tuple_list).
#
# Instead, we'll just do this one part at a
# time. To make this easier to follow, let's
# unpack the tuple first:
number, name, grade, max_grade, weight = line
# Now let's print each of these parts to the
# file, separated by spaces:
file_writer.write(str(number) + " ")
file_writer.write(name + " ")
file_writer.write(str(grade) + " ")
file_writer.write(str(max_grade) + " ")
file_writer.write(str(weight) + "\n")
# Then, once we're all done, we close the file.
file_writer.close()
tuple1 = (1, "exam_1", 90, 100, 0.6)
tuple2 = (2, "exam_2", 95, 100, 0.4)
tupleList = [tuple1, tuple2]
write_1301("output.cs1301", tupleList)
print()
# Write a function called get_grade that will read a
# given .cs1301 file and return the student's grade.
# To do this, we would recommend you first pass the
# filename to your previously-written reader() function,
# then use the list that it returns to do your
# calculations. You may assume the file is well-formed.
#
# A student's grade should be 100 times the sum of each
# individual assignment's grade divided by its total,
# multiplied by its weight. So, if the .cs1301 just had
# these two lines:
#
# 1 exam_1 80 100 0.6
# 2 exam_2 30 50 0.4
#
# Then the result would be 72:
#
# (80 / 100) * 0.6 + (30 / 50) * 0.4 = 0.72 * 100 = 72
# Write your function here!
# Over write previous file to get correct output:
with open("sample.cs1301", "w") as gradeFile:
gradeFile.write('''1 assignment_1 85 100 0.05
2 assignment_2 80 100 0.05
3 assignment_3 95 100 0.05
4 assignment_4 95 100 0.05
5 assignment_5 80 100 0.05
6 exam_1 85 100 0.1
7 assignment_6 100 100 0.05
8 assignment_7 97 100 0.05
9 exam_2 89 100 0.1
10 assignment_8 93 100 0.05
11 assignment_9 99 100 0.05
12 exam_3 92 100 0.1
13 final_exam 95 100 0.25''')
def reader(my_grade_file):
tuple_result = []
with open(my_grade_file, "r") as new_read_file:
for lines in new_read_file:
all_lines = lines.split(" ")
tuples_list = int(all_lines[0]), all_lines[1], int(all_lines[2]), int(all_lines[3]), float(all_lines[4])
tuple_result.append(tuples_list)
return tuple_result
new_read_file.close()
def get_grade(grade_file):
sum_grades = 0
grades_list = reader(grade_file)
for item in grades_list:
calc_sum = int(item[2]) / int(item[3]) * float(item[4])
sum_grades += calc_sum
return sum_grades * 100
grades_list.close()
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: 91.55
print(get_grade("sample.cs1301"))