Question: 2 (3.5 pts.). For this problem, you will write two functions, call them read_high_low() and write_high_low(). Function read_high_low() is passed the name, fname of a


2 (3.5 pts.). For this problem, you will write two functions, call them read_high_low() and write_high_low(). Function read_high_low() is passed the name, fname of a file with one integer per line and an integer, threshold. As it reads the file, it puts values less than threshold in a list, say, low, and values greater than or equal to threshold in another list, say high. It returns low and high. Before returning this function outputs a table showing, for both low and high, the number of elements in each, their largest element, their smallest element, and the average of the values they contain. This table should be well formatted; use the table shown for the upcoming example as a pattern. As an example, suppose that read_high_low() is called and passed 100 for threshold and the name 'data.txt' for fname. The following is the content of data.txt. 37 143 51 23 205 321 12 427 82 165 43 Min In this case, this function returns the following pair of lists, low followed by high. [37, 51, 23, 12, 82, 43] [143, 205, 321, 427, 165) And it outputs the following table. number Max average values = 100: 5 427 143 252.2 Function write_high_low() is passed the names of two files, hfname and lfname, and two lists of integers, low and high such that every element in low is less than all the elements in high. It writes the contents of low to 1fname, one integer per line, and the contents of high to hfname, also one integer per line. (Try to avoid a blank line at the end of a file. You can do this by looping over the slice of the list with everything but the last element, including an ' ' in the format of the item written. Then, after the loop, write the last element in the list and omit ' ' from the format for it. Consider defining an auxiliary function that outputs the contents of one list to the appropriate file. The body write_high_low () then consists of just two calls to this auxiliary function Define both functions and any auxiliary functions) in the same file, and include in this file test code that calls read_high_low() passing data.txt" (listed above and available for download from where you got this assignment) and a threshold of 100. Assign the values returned by this call to say, lo and hi. Then call write_high_low() passing 10, high, and the names of the two files to which these value are written, say, "low_data.txt' and 'high_data.txt" With this test data, the contents of low_data.txt are 37 51 23 12 82 43 And the contents of high_data.txt are 143 205 321 427 165 Put this test code within the scope of a conditional that tests that the file is being execute as a script and not imported as a module. 2 (3.5 pts.). For this problem, you will write two functions, call them read_high_low() and write_high_low(). Function read_high_low() is passed the name, fname of a file with one integer per line and an integer, threshold. As it reads the file, it puts values less than threshold in a list, say, low, and values greater than or equal to threshold in another list, say. high. It returns low and high. Before returning this function outputs a table showing, for both low and high, the number of elements in each, their largest element, their smallest element, and the average of the values they contain. This table should be well formatted; use the table shown for the upcoming example as a pattern. As an example, suppose that read_high_low() is called and passed 100 for threshold and the name 'data.txt' for frame. The following is the content of data.txt 37 143 51 23 205 321 12 427 82 165 43 In this case, this function returns the following pair of lists, low followed by high. [37, 51, 23, 12, 82, 43] [143, 205, 321, 427, 165] And it outputs the following table. number Max Min average values 100: 5 427 143 252.2 Function write_high_low() is passed the names of two files, hfname and 1 [name, and two lists of integers, low and high such that every clement in low is less than all the elements in high. It writes the contents of low to 1 fname, one integer per line, and the contents of high to hfname, also one integer per line. (Try to avoid a blank line at the end of a file. You can do this by looping over the slice of the list with everything but the last element, including an ' ' in the format of the item written. Then, after the loop, write the last element in the list and omit " " from the format for it.) Consider defining an auxiliary function that outputs the contents of one list to the appropriate file. The body write_high_low) then consists of just two calls to this auxiliary function Define both functions and any auxiliary functions) in the same file, and include in this file test code that calls read_high_low() passing data.txt' (listed above and available for download from where you got this assignment) and a threshold of 100. Assign the values returned by this call to say, lo and hi. Then call write_high_low() passing lo, high, and the names of the two files to which these value are written, say, "low_data.txt' and 'high_data.txt'. With this test data, the contents of low_data.txt are 37 51 23 12 82 43 And the contents of high_data.txt are 143 205 321 427 165 Put this test code within the scope of a conditional that tests that the file is being execute as a script and not imported as a module
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
