[Solved]Python 3x Question Write Function Linelengths Filename Function Returns List Tuples One E Q37100727
Python 3.x question Write the function line_lengths(filename).The function returns a list of tuples – one for every line in thefile. specific information on the photo
Only allowed: while loop, if statements, len(), append(),type(), isinstance(), elif, else, return, break, continue, def ,self, None, is ,and any arithmetic or boolean comparison operators,any file methods.
Restrictions: do not use for loops, in keyword, with keyword,enumerate, slices or slicing , lambda functions( sorted() and so on)
Write the function line lengths(filename). The function returns a list of tuples – one for every line in the file. Each tuple contains the length of the line as its first element, and the line itself as its second. For example, if one of the lines in the file is “hello”, the corresponding tuple should be (6, “hello n”). If there are no lines in the file, an empty list object is returned. Be sure to include the function prototype. If the file cannot be opened because it does not exist, you must catch the FileNotFoundError object and throw a new exception FileNotFoundError with the contents of the exception being the filename. Any other exception for failure to open a file is not caught. If the parameter filename is not a string type, then a TypeError exception is thrown with the contents of the exception being a string “parameter filename is not a string”. Example function returns the list of tuples The file has the lines (8, ‘cushion|n’), (10, ‘delicious |n’), (5, ‘four n’), (4, ‘vann’)] cushion delicious oul van See appendix for open(), close), readlines), type) and isinstance ) to support this question. Hint: readline() already includes a new line character. Restrictions Only Allowed: while loops, if statements, function len), type(), isinstance ), list method append ) string method split), format O. keywords elif, else, return, break, continue, def, self, None, try, raise, except, is, import sys, and any arithmetic or boolean comparison operators, any file methods from the reference page. Remember: Do not use a for loop, do not use the in keyword, do not use the with keyword, do not use enumerate, do not use slices or slicing e.g. [1:], do not use lambda functions e.g. sorted). Marks will be deducted Show transcribed image text Write the function line lengths(filename). The function returns a list of tuples – one for every line in the file. Each tuple contains the length of the line as its first element, and the line itself as its second. For example, if one of the lines in the file is “hello”, the corresponding tuple should be (6, “hello n”). If there are no lines in the file, an empty list object is returned. Be sure to include the function prototype. If the file cannot be opened because it does not exist, you must catch the FileNotFoundError object and throw a new exception FileNotFoundError with the contents of the exception being the filename. Any other exception for failure to open a file is not caught. If the parameter filename is not a string type, then a TypeError exception is thrown with the contents of the exception being a string “parameter filename is not a string”. Example function returns the list of tuples The file has the lines (8, ‘cushion|n’), (10, ‘delicious |n’), (5, ‘four n’), (4, ‘vann’)] cushion delicious oul van See appendix for open(), close), readlines), type) and isinstance ) to support this question. Hint: readline() already includes a new line character. Restrictions Only Allowed: while loops, if statements, function len), type(), isinstance ), list method append ) string method split), format O. keywords elif, else, return, break, continue, def, self, None, try, raise, except, is, import sys, and any arithmetic or boolean comparison operators, any file methods from the reference page. Remember: Do not use a for loop, do not use the in keyword, do not use the with keyword, do not use enumerate, do not use slices or slicing e.g. [1:], do not use lambda functions e.g. sorted). Marks will be deducted
Expert Answer
Answer to Python 3.x question Write the function line_lengths(filename). The function returns a list of tuples – one for every lin… . . .
OR

