2. (5 points) Code review. Your classmates Song and Xu wrote a Python function check to validate the format of an input file

1 Appendix: Python built-in types Strings A text line is of type str. Far example, line Two BIG nanes. with a for loop We can continuation
class list (object) I list new empty 1ist | list(iterable) new list initial ized from iterables items IMethods defined here: 2. (5 points) Code review. Your classmates Song and Xu wrote a Python function check to validate the format of an input file (plain text). Each row in a valid input file must contain exactly three entries, separated by TAB, the last two entries being two non-negative numbers. check returns True iff all rows are valid. Please review and comment on their code (shown below). The input argument origin.data is a list of lines from the input file. You can assume that the following code contains no syntax errors. Refer to Appendix 1 for relevant Python API information. ### #### # Check uploaded file’s format ####### # ############# ######## ##### ################ # ## #### # check (orig in_data) def recorder- for rows in origin_data: row rows.split () recorder.append(row) for data in recorder: if len (data) = 3: return False return True 1 Appendix: Python built-in types Strings A text line is of type str. Far example, line Two BIG nanes. with a for loop We can iterate over the string liser for c in line: print (e) elass str(object) I str(object str I str(bytes or_buffer [, encoding [, errors]]) str Create a neu string object froa the given object. I Methods defined here: I isdigit(…) S.isdigitO-bool Return True if all characters in S are digits and there is at least one character in S, False otbervise. isnuneric(.. S.isnumericO-bool Return True if there are only nuserie characters in S False othervine. split…) s.split(sep one, sazsplit-1) -list of strings Return a list of the vords in S, using sep as the deliniter string. If saxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and espty strings are removed from the result. strip.. S.strip([chars] ) -str Return a copy of the string S vith leading and trailing whitespace resoved. If chars is given and not None, reove characters in chars instead Lists We can create a list of 3 strings, for example, lst [Tuo Big Names:’ Steve Jobs, Sincna Tathan. . The length of lst can be obtained by len (1st), which is 3 We can iterate over the list 1st with a for loop CONTD… Page 11 of 17 action to Software Engineering Final Exam Spring 2018 ntre for x in 1st: print (x) class list (object) I list new empty 1ist | list(iterable) new list initial ized from iterable’s items IMethods defined here: I append(…) L.append (object) -> None — append object to end I extend(…) L.extend(iterable) None extend list by appending elements from the iterable -> I index…) L.index (value, [start, [stopl]) -integer return first index of value Raises ValueError if the value is not present



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *