# Week 11 practice problems, by chaynes@indiana.edu def line_count(file_name): """Return the number of lines in the named file. >>> print file('data.txt').read() 3 5.4 -2 >>> line_count('data.txt') 4 >>> """ #. #. #. #. #. #. #. #. def copy_lines(in_file_name, out_file_name, start_string, stop_string): """Copy lines from the input file to the output file, starting with the first line containing the start string and stopping when a line containing the stop string is read (without writing the stop string line). If start_string is not in the file, do not even open the output file, and if the stop string is not found, write to the end of the input file. >>> print file('argument.txt').read() M: When? O: Just now. M: No you didn't! O: Yes I did! M: You didn't! O: I did! M: You didn't! >>> copy_lines('argument.txt', 'subargument.txt', 'Just', 'I did') >>> print file('subargument.txt').read() M: When? O: Just now. M: No you didn't! >>> """ #. #. #. #. #. #. #. #. #. #. #. #. import csv def print_column(csv_file_name, column_index): """Print, one per line, the values in the indicated column of the table in the Csv file. Use zero-based indexing of the columns. >>> print file('contacts.csv').read() First Name,Last Name,Suffix,Home Street,Home Street 2,Home City,Home State,Home Postal Code,Telex Sally,Porter,,4324 Hedrick Ln,,Jersy City,NJ,23403,HB Sam,Proxmire,,125 Hampton Cr,,Jefferson,NY,32084,BA Sue,Pinter,,32 Hawthorn Dr,,Jackson,MS,13034,HB >>> print_column('contacts.csv', 3) Home Street 4324 Hedrick Ln 125 Hampton Cr 32 Hawthorn Dr >>> """ # Hint: use csv module information in assignment 11. #. #. #. #. #. #. #. #. def differences(lst): """Return a list of numbers that are the differences between each successive pair of values in lst. More precisely, the i_th value in the returned list is the difference between the (i+1)_th and i_th values in the given list. The returned list is thus one shorter than lst, which may be assumed to contain at least two values. (This is the discrete counterpart of the derivative of continuously varying functions in mathematics.) >>> differences([1, 0, 5, 3]) [-1, 5, -2] >>> """ #. #. #. #. #. #. def delete_ends(lst): """Delete the first and last elements of lst. >>> los = list('abcd') >>> delete_ends(los) >>> los ['b', 'c'] >>> """ #. #. def delete_duplicates(lst): """Delete elements from lst so that no two elements have the same value, but every value that was in lst initially still remains (but only once). The values in the resulting list are in the order of their first appearance in the given lst. >>> a = list('adabbaacd') >>> delete_duplicates(a) >>> a ['a', 'd', 'b', 'c'] >>> """ #. #. #. #. #.