An engineering analysis script reads measurements.txt and produces report.txt, which already contains a previous valid report. The requirements are:
The input fits in memory. Assume no filesystem errors occur, and the two filenames identify different files. Which sequence satisfies all three requirements?
Open the report in "w", then validate all measurements, calculate the mean, and write the replacement.
Validate all measurements, calculate the mean, then open the report in "w" and write the replacement.
Validate all measurements, calculate the mean, then open the report in "a" and write the replacement.
Open the report in "a", append accepted measurements during validation, then append their calculated mean.
Unlock full course notes by signing up
Averaging accepted force records
An instrument exports a UTF-8 text file with the following physical lines. An entry labelled blank represents an empty line; all other entries show the literal line contents without their newline characters.
The processing specification says to skip the header, ignore blank lines, and exclude records whose force field is exactly NA. All remaining force fields are converted to numbers. Calculate the arithmetic mean of the accepted force measurements. Answer in N, to one decimal place.
Counting characters and separated fields
A sensor export contains exactly the text represented by this Python string literal. The spaces inside the literal belong to the file, and the last record has no final newline.
The following Python 3 code processes the file:
Complete the table with the values of characters and fields for each record. Enter exact integers, counting spaces as characters.
Rejecting malformed records explicitly
A Python parser processes engineering records after checking the header time_s,force_N. The specification permits blank lines, but every nonblank data line must contain exactly two comma-separated fields that both convert with float(). Any malformed record must stop processing and identify its physical line number.
The current implementation is:
Potential malformed records include "1.0,7.2,extra" and "1.5,error". Which revision satisfies the complete specification?
Reconciling written characters and stored bytes
An engineer generates a temperature report with the following Python 3 code. All writes succeed, and the file is closed before its size is measured.
With this newline setting, each \n passed to write is stored as \r\n. In UTF-8, ° occupies two bytes; every other character used in this output occupies one byte. The file contains no additional prefix bytes.
Find the final value of reported, measured in characters, and the resulting file size, measured in bytes. Give both answers as exact integers.