Kata Four: Data Munging

The file football.dat contains the results from the English Premier League for 2001/2. The columns labeled 'F' and 'A' contain the total number of goals scored for and against each team in that season (so Arsenal scored 79 goals against opponents, and had 36 goals scored against them). Write a program to print the name of the team with the smallest difference in 'for' and 'against' goals.

by davidelrizzo

HTML

Source <a
href="http://sunsite.tut.fi/rec/riku/soccer_data/tab/93_94/table.eng0.01_02.html">sunsite.tut.fi/rec/riku/soccer_data/tab/93_94/table.eng0.01_02.html</a>

<pre>
       Team            P     W    L   D    F      A     Pts
    1. Arsenal         38    26   9   3    79  -  36    87
    2. Liverpool       38    24   8   6    67  -  30    80
    3. Manchester_U    38    24   5   9    87  -  45    77
    4. Newcastle       38    21   8   9    74  -  52    71
    5. Leeds           38    18  12   8    53  -  37    66
    6. Chelsea         38    17  13   8    66  -  38    64
    7. West_Ham        38    15   8  15    48  -  57    53
    8. Aston_Villa     38    12  14  12    46  -  47    50
    9. Tottenham       38    14   8  16    49  -  53    50
   10. Blackburn       38    12  10  16    55  -  51    46
   11. Southampton     38    12   9  17    46  -  54    45
   12. Middlesbrough   38    12   9  17    35  -  47    45
   13. Fulham          38    10  14  14    36  -  44    44
   14. Charlton        38    10  14  14    38  -  49    44
   15. Everton         38    11  10  17    45  -  57    43
   16. Bolton          38     9  13  16    44  -  62    40
   17. Sunderland      38    10  10  18    29  -  51    40
   -------------------------------------------------------
   18. Ipswich         38     9   9  20    41  -  64    36
   19. Derby           38     8   6  24    33  -  63    30
   20. Leicester       38     5  13  20    30  -  64    28
</pre>

CSS

body { font:90%/1.5em sans-serif; }
pre { margin:1em; font:90%/1.5em monospace; }
p { font-weight:bold; color:red; }
p:before { content:"Answer: "; color:black; }

JavaScript

// clean the data so everyone can enjoy
function munge(rawData) {
    rawData = rawData.replace(/ +\d+\. +/g,""); //strip leading number
    rawData = rawData.replace(/-+/g,"");        //strip hyphens
    rawData = rawData.replace(/ +\n/g,"\n");    //strip ending whitespace (just in case)
    rawData = rawData.replace(/ +/g,",");       //turn into comma delimited string
    var data = rawData.split("\n");
    for( i in data ) {
        if ( !data[i].match(/\w+,\d+,\d+,\d+,\d+,\d+,\d+,\d+/) ) {  
            //delete line if it doesn't match the pattern of team scores
            delete data[i];
        } else {
            var lineArray = data[i].split(",");
            var teamObj = {};
            teamObj.Team = lineArray[0];
            teamObj.Plays = Number(lineArray[1]);
            teamObj.Wins = Number(lineArray[2]);
            teamObj.Losses = Number(lineArray[3]);
            teamObj.Draws = Number(lineArray[4]);
            teamObj.For = Number(lineArray[5]);
            teamObj.Against = Number(lineArray[6]);
            teamObj.Pts = Number(lineArray[7]);
            data[i] = teamObj;
        }
    }
    return data;
}

// Get smallest difference between two given values
function getMinDiff(data,val1,val2) {
    for( i in data ) {
        data[i].Diff = Math.abs(data[i][val1]-data[i][val2]);
    }
    data.sort( function(a,b){return a.Diff-b.Diff;} );
    return data[0]; // returns only the first team found, could be improved to return all with equal diff
}

var data = munge( $("pre").text() );
$('body').append("<p>" + getMinDiff(data,"For","Against").Team + "</p>");