''' Purpose: for user-specified dataset, and two user-specified column labels column1 and column2: determines the minimum value min1 in column1 in the dataset determines col2 -- the column2 value whose column1 value equals min1. prints labeled explanatory information about output ''' # get ahold of helpful web functions import url # specify base web folder for datasets CSV_REPOSITORY = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/' # get dataset of interest reply = input( 'Enter the name of a data set: ' ) name = reply.strip() link = CSV_REPOSITORY + name dataset = url.get_dataset( link ) # get the labels for the columns of interest label1 = input( 'Enter a column label for the data set: ' ) label2 = input( 'Enter a column label for the data set: ' ) print() # clean up the labels label1 = label1.strip() label2 = label2.strip() # identify header and data from the dataset header = dataset[ 0 ] # header is the first row of the dataset table = dataset[ 1 : ] # other rows are our table of data values # determine the indices index1 and index2 of label1 and label2 in the header index1 = header.index( label1 ) index2 = header.index( label2 ) # need to find the min value min1 in the column of the table whose index is index1 # to find it, let's first build a list values1 of the values in that column values1 = [] # because we are going to accumulate them # we need to start off with an empty list # need to add the index1 value of each row of the table for row in table : # loop to consider the rows one by one # get the index1 value v from the current row v = row[ index1 ] # add v to the list of values1 values1.append( v ) # print what we know so far print( 'Analyzing', name ) print() print( label1, 'values :', values1 ) print() # now let's find the minimum value min1 in values1 min1 = min( values1 ) # built-in min() can find value min1 for us # print what we now know print( 'min', label1, 'value is', min1 ) print() # now supposed to find the index2 column value for the row whose index1 # column value is min1 # key insight: # the first value in values1 equals the value of the index1 column of the # first row in table # the second value in values1 equals the value of the index1 column of the # second row in table # the third ... # implication: # if we can find the index i into values1 that equals min1, then the row # with index i in table has the index2 value we are looking for i = values1.index( min1 ) # find where min1 occurs in values1 row_of_interest = table[ i ] # find the corresponding row of table # print what we now know print( 'dataset row of interest:', row_of_interest ) # ready to determine the value col2 -- that is, the value in column index2 from # the row of interst col2 = row_of_interest[ index2 ] # grab the value from the column of interest # print what we now know print( label2, ':', col2 )