2011-02-23 15:19:01
<?php
 
class bullshit {
 
    public $total_engineers;
    public $total_drinks;
    public $engineer = array();
    public $drink = array();
    public $path;
    private $io;
    public $enable_output = true;
 
    public function __construct($p) {
        $this->total_engineers = 0;
        $this->total_drinks = 0;
        $this->path = $p;
    }
 
    public function __readfile() {
        $this->io = fopen($this->path, "r") or die("aaa");
        flock($this->io, 2);
    }
 
    private function __setTotalDrinks($value) {
        $this->total_drinks = $value;
    }
 
    private function __setTotalEngineers($value) {
        $this->total_engineers = $value;
    }
 
    public function __getValueBySeperator($string, $seperator) {
        $string_array = explode(",", $string);
        return $string_array;
    }
 
    public function __readline($line) {
        $current_row = 0;
        for ($i = 0; !feof($this->io); $i++) {
 
 
 
            if ($current_row < $line) {
                if ($current_row == 0) {
                    $element = fgetcsv($this->io, 1000, " ");
                    $this->__setTotalEngineers($element[0]);
                    $this->__setTotalDrinks($element[1]);
                } else if ($current_row > 0 && $current_row < ($this->total_drinks + 1)) {
                    $element = fgetcsv($this->io, 1000, "\t");
                    array_push($this->drink, $element[1]);
                } else if ($current_row >= ($this->total_drinks + 1) && $current_row <= ($this->total_engineers + $this->total_drinks + 1)) {
                    $element = fgetcsv($this->io, 1000, "\t");
                    array_push($this->engineer, $element[1]);
                }
 
                if ($this->enable_output) {
                    echo $element[0] . " " . $element[1] . "<br/>";
                }
            }
 
            $current_row++;
        }
    }
 
    public function __assignDrinkNameToEngineer() {
        foreach ($this->engineer as $Name => $value) {
            if (strlen($value) > 0) {
 
                echo "Engineer[" . $Name . "] = ";
 
                $drink = $this->__getValueBySeperator($value, ",");
                foreach ($drink as $N => $DrinkValue) {
                    if (strlen($DrinkValue) > 0) {
                        echo $this->drink[$DrinkValue] . "$N($DrinkValue) ,";
                        $this->engineer[$Name][$N] = $drink[$N];
                    }
                }
                echo "<br/>";
            }
        }
    }
 
}
 
$bullshit = new bullshit("test.txt");
$bullshit->enable_output = true;
 
$bullshit->__readfile();
$bullshit->__readline(100);
//
 
 
echo "<h1>DEBUG</h1>";
echo "Total Engineers: {$bullshit->total_engineers}<br/>";
echo "Total Drinks: {$bullshit->total_drinks}<br/>";
echo "---------------------<br/>";
$bullshit->__assignDrinkNameToEngineer();
echo "---------------------<br/>";
$score = 0;
$score1 = 0;
$score2 = 0;
$score3 = 0;
 
 
//Loop for the upper group of engineers
for ($i = 0; $i < $bullshit->total_engineers / 2; $i++) {
 
    //Loop for taking preference score between 2 Engineers
    for ($j = $bullshit->total_engineers - 1; $j > $bullshit->total_engineers / 2 - 1; $j--) {
        //Starting score
        $score = 0;
        //Loop 3
        for ($k = 0; $k < $bullshit->total_drinks; $k++) {
            //Loop 4
            for ($l = 0; $l < $bullshit->total_drinks; $l++) {
                if ($bullshit->engineer[$i][$k] == $bullshit->engineer[$j][$l]) {
                    //check position
                    if ($k == $l) {
                        $score += ( $bullshit->total_drinks - $k) * ($bullshit->total_drinks - $k);
                        echo("+" . ($bullshit->total_drinks - $k) * ($bullshit->total_drinks - $k) . "<br/>"); //Debug
                        break;
                    } else if ($k < $l) {
                        $score += ( $bullshit->total_drinks - $k);
                        echo("+" . ($bullshit->total_drinks - $k) . "<br/>"); //Debug
                        break;
                    } else {
                        break;
                    }
                } else {
                    if ($l == $bullshit->total_drinks - 1) {
                        $score += ( $bullshit->total_drinks - $k);
                        echo("+" . ($bullshit->total_drinks - $k) . "<br/>"); //Debug
                        break;
                    }
                }
            }
        }
        //Debug purpose
        echo("E[" . $i . "] and E[" . $j . "] Preference Score:" . $score . "<br/>");
        //Score update
        if ($score >= $scoreboard[$i][0]) {
            $scoreboard[$i][0] = $score;
            $scoreboard[$i][1] = $j;
        }
    }
}
//End all loop
echo ("<br><br><br><br>");
for ($i = 0; $i < ($bullshit->total_engineers / 2); $i++) {
    echo($i . " " . $scoreboard[$i][1] . "<br/>");
}
?>
Invalid Email or Password