Craigslist test task

few weeks ago, i’ve found an interesting job offer at Craigslist

the main thing was that they had a test task which consist of:

Using baseball-reference.com write a program (in the language of your choice) that retrieves every major league team’s season record since 1900. Have your program output the data in a CSV format where each line is a team year of the following pattern “,,,”. Order of teams and years does not matter, but there should be no duplicates.

example:

Boston Red Sox,2008,95,67
Boston Red Sox,2007,96,66

i don’t know why i’ve decided to make it i’m not a fan of baseball.

but anyway i’ve made it and will show you how:

my first step was to go and analyze the site baseball-reference.com

after that, i’ve decided to use PHP with prepared parser class.

include("phpHTMLParser.php");
 
$content = file_get_contents("http://www.baseball-reference.com/postseason/");
 
$parser = new phpHTMLParser("$content");
 
$HTMLObject = $parser->parse_tags(array("A"));
 
$aTags = $HTMLObject->getTagsByName("A");
 
 
$file = fopen('baseball.csv','w');
fwrite($file,"");
fclose($file);
 
echo "
	<center>Download CSV file:<br/>
	<a href='baseball.csv'>download</a></center>
	<hr><br/>
";
 
foreach ($aTags as $a) {
   if ($a->href != "") {
 
	$url = $a->href;
	$title = $a->innerHTML;
 
	$out = substr($url,-10);
	$info = strpos($title,"(");
	$i = 1900;
 
	while ($i<2008) {
 
		$i++;
 
		$count = $i.".shtml";
 
		if ($out == $count && $info == true) {
 
 
			$team_pos = strpos($title, '(', 1);
 
			$def = strpos($title, '-');
			$w_count = $def - $team_pos - 1;
			$win_pos = substr($title,$team_pos+1,$w_count);
 
			$com = strpos($title, ',');
			$l_count = $com - $def - 1;
			$lose_pos = substr($title,$def+1,$l_count);
 
			$team = substr($title,0,$team_pos-1);
 
			$team = str_replace('*','',$team);
 
			echo $team . "," . $i . "," . $win_pos . "," . $lose_pos . "<br/>";
 
			$file = fopen('baseball.csv','a');
			$content = "$team,$i,$win_pos,$lose_pos\n";
			fwrite($file,$content);
 
		}
	}
 
   }
}
 
fclose($file);

so, as you see the code is simple stupid, just a loop with checking for their custom pattern

you may see example and download it for your needs at my workshop

they didn’t choose me and i can’t boast you with “A new MacBook Pro, a 30″ monitor, an iPhone and 5k $” :)

so, don’t know where is my bad, probably i should make everything much more simplier with few regex lines.

or they didn’t choose me cas i’m from Ukraine :) not from united states

however, i like to do some interesting tasks at my free time when i have it (=

i’m open to hear some critics and suggestions, but keep in mind that it was a task for 10-15 minutes ;)

Trackback URL

, , ,

No Comments on "Craigslist test task"

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

Subscribe to Comments