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:
for line_number, line in enumerate(source, start=2): if not line.strip(): continue fields = line.strip().split(",") if len(fields) != 2: continue time_s = float(fields[0]) force_n = float(fields[1])
Potential malformed records include "1.0,7.2,extra" and "1.5,error". Which revision satisfies the complete specification?
Keep the first two fields of every nonblank record and replace unconvertible values with zero.
Keep skipping incorrect field counts, but report the line number when a numeric conversion fails.
Check field counts and numeric conversions, but skip any record that fails either check.
Reject incorrect field counts and failed numeric conversions, reporting the line number for either failure.
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.
Replacing a report only after validation
An engineering analysis script reads measurements.txt and produces report.txt, which already contains a previous valid report. The requirements are:
A successful run replaces the entire previous report with a new header and mean force.
If any input record is malformed, the previous report remains unchanged.
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.