[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# URGENT: find smallest difference (either PHP, Java or Python)

Posted on 2012-08-26
Medium Priority
810 Views
Hi Experts,
----------------------------------------------------------------------------------------------------------------
The file football.txt 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). Need a java program to print the name of the team with the smallest difference in ‘for’ and ‘against’ goals.
----------------------------------------------------------------------------------------------------------------
football.txt
0
Question by:Dushan De Silva

LVL 17

Author Comment

ID: 38335181
My main objective of this question is to identify the activities we will be able to write the code and demonstrate some of the practices and techniques that we would use in best practices under industry. I'm mainly be looking for the thought process and how we can complete this task. But points goes to the first/best post.

Thanks a lot Experts.
0

LVL 18

Assisted Solution

Garry Glendown earned 500 total points
ID: 38335341
Here's a quick shell/awk hack that outputs the team name ...

``````#!/bin/sh
cat football.txt | ( awk '
{
if (\$1!="Team" && \$2) {
DIFF=\$7-\$9
if (DIFF<0) DIFF*=-1;
print DIFF ":" \$2;
}
}
' ) | sort -n | cut -d: -f2 |  head -1
``````
0

LVL 25

Assisted Solution

ID: 38335481
There are many approaches, what really determines the best is what is to be done with the data.  If it purely just to find the lowest value, a simplistic approach is best (see method 1 below) ... but as the need to use the same data for multiple things happens, the approaches can get more complex to avoid double handling.
Using PHP here are some:
``````echo '<pre>';

\$lowfa = 99999;     // method 1 - impossibly high value
\$lowfaTeam = "";    // method 1
\$teams = array();   // method 2
\$dimteam = array(); // method 3

if ( \$file = fopen("/Apps/DATA/football.txt", "r") ) {
\$line = fgets(\$file);
while( !feof(\$file) ) {
// so many ways to divide the sting into value ... went
// with basic split on spaces
\$vals = preg_split("/\s+/", \$line);

// many ways to test whether it is a line of teams stats
// ... went with detecting the '-' between the for and away
// amounts.
if ( isset(\$vals[8]) && \$vals[8] == "-" ) {

\$FandA = abs(\$vals[7] - \$vals[9]);

if ( \$FandA < \$lowfa ) {     // method 1
\$lowfaTeam = \$vals[2];   // method 1
\$lowfa = \$FandA;         // method 1
}                            // method 1

\$teams[\$vals[2]] = \$FandA;   // method 2

\$dimteam[] = array('team'    => \$vals[2],  // method 3
'for'     => \$vals[7],  // method 3
'against' => \$vals[9],  // method 3
'FandA'   => \$FandA);   // method 3
}
\$line = fgets(\$file);
}
} else {
die('ERROR - Unable to open file');
}

echo "-- Method 1 - Lowest (absolute) For&Against",PHP_EOL;
echo \$lowfaTeam," : ",\$lowfa,PHP_EOL;

echo PHP_EOL,PHP_EOL;

echo "-- Method 2a - All (absolute) For&Against asc",PHP_EOL;
asort(\$teams);
foreach ( \$teams as \$k=>\$v ) {
echo \$k," : ",\$v,PHP_EOL;
}

echo PHP_EOL,PHP_EOL;

echo "-- Method 2b - Lowest (absolute) For&Against",PHP_EOL;
asort(\$teams);
\$lowest = array_slice(\$teams, 0, 1);
foreach ( \$lowest as \$k=>\$v ) {
echo \$k," : ",\$v,PHP_EOL;
}

echo PHP_EOL,PHP_EOL;

echo "-- Method 1a - All (absolute) For&Against asc",PHP_EOL;
\$FandAsort = array();
foreach (\$dimteam as \$k => \$row) {
\$FandAsort[\$k]  = \$row['FandA'];
}
array_multisort(\$FandAsort, SORT_ASC, \$dimteam);
foreach ( \$dimteam as \$k=>\$row ) {
echo \$row['team']," : ",\$row['FandA'], '  (for = ',\$row['for'],', against = ',\$row['against'],')',PHP_EOL;
}
``````
0

LVL 111

Accepted Solution

Ray Paseur earned 500 total points
ID: 38336237
Here is how I would do it.
http://www.laprbass.com/RAY_temp_dushan911.php

``````<?php // RAY_temp_dushan911.php
error_reporting(E_ALL);
echo "<pre>";

// A CLASS TO HOLD EACH TEAM'S RECORD
class Soccer
{
// THE F:A DIFFERENCE IS STORED HERE
public \$diff;

public function __construct(\$r, \$t, \$p, \$w, \$l, \$d, \$f, \$a, \$s, \$original)
{
\$this->r = str_replace('.', NULL, \$r);
\$this->t = \$t;
\$this->p = \$p;
\$this->w = \$w;
\$this->l = \$l;
\$this->d = \$d;
\$this->f = \$f;
\$this->a = \$a;
\$this->s = \$s;
\$this->original = \$original;

// COMPUTE THE DIFFERENCE IN F:A
\$this->diff = abs(\$this->f - \$this->a);
}

// RETURN THE STRING
public function __toString()
{
}
}

// A FUNCTION TO SORT THE TEAMS BY F:A DIFFERENCE
function cmp_diff(\$a, \$b)
{
if     (\$a->diff == \$b->diff) return 0;
return (\$a->diff <  \$b->diff) ? -1 : 1;
}

// THE DATA SOURCE
\$url = 'http://filedb.experts-exchange.com/incoming/2012/08_w35/599134/football.txt';
\$str = file_get_contents(\$url);

// REMOVE EXTRANEOUS INFORMATION
\$arr = explode('<pre>', \$str);
\$str = str_replace('</pre>', NULL, \$arr[1]);

// MAKE AN ARRAY FROM THE ROWS OF DATA
\$arr = explode(PHP_EOL, \$str);

// SAVE THE COLUMN TITLES
\$top = \$arr[1];

// EXAMINE EACH ELEMENT OF THE ARRAY
foreach (\$arr as \$str)
{
// SAVE THE ORIGINAL
\$new = \$str;

// REMOVE EXCESS BLANKS
\$str = preg_replace('# +#', ' ', \$str);
\$str = trim(\$str);

// IF THE ROW STARTS WITH A NUMBER, IT IS USEFUL
\$str = trim(\$str);
if (!preg_match('#\d{1,2}#', \$str)) continue;

// CREATE AN ARRAY OF OBJECTS
\$x = explode(' ', \$str);
\$out[]
= new Soccer
( \$x[0]
, \$x[1]
, \$x[2]
, \$x[3]
, \$x[4]
, \$x[5]
, \$x[6]
, \$x[8]
, \$x[9]
, \$new
)
;
}

// SORT THE ARRAY OF OBJECTS
usort(\$out, 'cmp_diff');

// SHOW THE ONE WITH THE LEAST DIFFERENCE BETWEEN F & A
echo \$top . PHP_EOL;
echo \$out[0] . PHP_EOL;

// JUST FOR FUN SHOW THE ONE WITH THE GREATEST DIFFERENCE
echo end(\$out);
``````
HTH, ~Ray
0

LVL 17

Author Comment

ID: 38336283
Thanks a lot Garry-G, lwadwell and Ray for your great example codes.

Hi Ray,
I'm getting following for the given code.
--------------------------------------------------------------------------------------------------------------
Notice:  Undefined offset:  1 in C:\AppServ\www\dush\2.php on line 55
--------------------------------------------------------------------------------------------------------------
0

LVL 111

Expert Comment

ID: 38336292
Maybe you are not using the test data set that was posted here?  Please see line 44 for the URL of the file I relied on when writing the code.
0

LVL 29

Assisted Solution

pepr earned 500 total points
ID: 38338652
(This solution is repeated from the http:Q_27843121.html#a38338602)

In my opinion, if the criterion can be evaluated independently on the evaluation of the other teams, then the resutls should not be collected to the array and then sorted. It is enough to remember the best candidate. Here in Python:
``````import re

def getTheXTeam(fname, rex=re.compile(r'''^\s*\d+\.
\s+(?P<Team>.+?)
\s+(?P<P>\d+)
\s+(?P<W>\d+)
\s+(?P<L>\d+)
\s+(?P<D>\d+)
\s+(?P<F>\d+)
\s+-
\s+(?P<A>\d+)
\s+(?P<Pts>\d+)
\s*\$''', re.VERBOSE)):

diff =  10000     # init -- impossible result in reality
team_name = None  # init

with open(fname) as f:              # open in text mode for reading
for line in f:                  # loop throught the text file lines
print line.rstrip()
m = rex.match(line)
if m:                       # if it is the line with results
f = int(m.group('F'))   # convert the wanted numbers to int
a = int(m.group('A'))

if abs(f - a) < abs(diff):      # if smaller difference
diff = f - a                # remember the result
team_name = m.group('Team') # and the team
return team_name, diff

team_name, result = getTheXTeam('football.txt')

print team_name, 'is the team with the result', result
``````
It echoes the lines and prints the result at the end.
``````c:\tmp\_Python\Dushan911\Q_27842934>python d.py
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>
Aston_Villa is the team with the result -1
``````
0

LVL 17

Author Closing Comment

ID: 38339233
Thanks a lot all!!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches viewers about errors in exception handling.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month18 days, left to enroll