1. John Chandler
  2. metaljoe_codekata


metaljoe_codekata / kata4 / common.py


This module contains code extracted from both part1.py and part2.py that can be used in the refactored versions


def parse_file( filename, extraction_function ):
    """ Parses a .dat format file """
    data_file = open( filename, "r" )

    data = []
    for line in data_file:
        extracted_data = extraction_function( line )
        if extracted_data:
            data.append( extracted_data )

    return data

def get_item_with_smallest_difference( data, label_index, value1_index, value2_index ):
    """ Takes a list (data) of lists.
        Returns the 'label' of a row where the difference between two integer items in the row is the smallest out of all rows

    smallest_label = None
    smallest_diff  = None

    for row in data:
        diff = abs( int(row[value1_index]) - int(row[value2_index]) )
        if smallest_diff > diff or smallest_diff is None:
            smallest_label = row[label_index]
            smallest_diff  = diff

    return smallest_label