<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. * * Author: Nguyen Thanh Nhan * Email: [email protected] * Phone: (+84)909824222 */ error_reporting(E_ALL); class algotest { public $path; public $total_engineers; public $total_drinks; public $engineer = array(); public $drink = array(); public $scoreboard; public $enable_output; private $io; public function __construct($p = NULL) { // setup the default variables $this->total_engineers = 0; $this->total_drinks = 0; if (isset($p)) { $this->path = $p; $this->__readFile(); } else { //$this->__readArgs(); $this->__readArgs(); } } /* * read file */ public function __readFile() { $this->io = fopen($this->path, "r") or die("the path that you've assigned is invalid.({$this->path})"); flock($this->io, 2); } public function __readArgs() { $this->io = fopen('php://stdin', 'r'); flock($this->io, 2); } public function __readData($data) { $this->io = file_get_contents($argv[1]); } /* * set global total drinks number */ private function __setTotalDrinks($value) { $this->total_drinks = $value; } /* * set global total engineers number */ private function __setTotalEngineers($value) { $this->total_engineers = $value; } /* * convert from string to array using seperator */ public function __getValueBySeperator($string, $seperator) { $string_array = explode(",", $string); return $string_array; } /* * We read each line to: * 1. define first line to set the total engineer and total drinks ( seperator by a space ) ( total engineer is always even number ) */ public function __readline($maxline) { $current_row = 0; while ($line = fgets($this->io, 4096)) { // read to the end of the file try { ++$current_row; if (strlen($line) > 2) { if ($current_row == 1) { // handling the first row $element = explode(" ", $line); if (isset($element[0]) == false || isset($element[1]) == false) die("The input file is not valid"); $this->__setTotalEngineers(intval($element[0])); $this->__setTotalDrinks(intval($element[1])); if ($this->total_engineers % 2 != 0) die("Total engineers must be an even number"); if (is_integer($this->total_engineers) == false || is_integer($this->total_drinks) == false) die("Total engineers or total drinks must be an integer."); for ($i = 0; $i < $this->total_engineers; $i++) { for ($x = 0; $x < $this->total_drinks; $x++) { $this->engineer[$i][$x] = -1; } } for ($i = 0; $i < $this->total_drinks; $i++) { $this->drink[$i] = ""; } } else if ($current_row > 1 && $current_row <= ($this->total_drinks + 1)) { // rows between total drink range $element = explode("\t", $line); // get element by seperator tab $element[1] = str_replace("\n", "", $element[1]); $this->drink[$element[0]] = trim($element[1]); // push the value into the drink array } else if ($current_row > ($this->total_drinks + 1) && $current_row <= ($this->total_engineers + $this->total_drinks + 1)) { // row between engineer ranges $element = explode("\t", $line); // get element by seperator tab $element[1] = str_replace("\n", "", $element[1]); $drinks = explode(",", $element[1]); foreach ($drinks as $key => $value) { $this->engineer[$element[0]][$key] = trim($value); } } $this->__output($element[0] . " " . $element[1] . "<br/>"); } } catch (Exception $e) { echo "The input file is not well-formed: " . $e->getMessage(); } } } /* * We need to assign drink number into 2 dimension array which first dimension is id of engineer * second dimension indicates id the engineer's drink of choice * eg. * Engineer[0][0] = 1 * Engineer[0][1] = 0 * this engineer has chosen his first drink is 1. second drink of his is 0. And so on. */ public function __displayDrinkToEngineer() { for ($i = 0; $i < $this->total_engineers; $i++) { echo "e[" . $i . "]="; for ($x = 0; $x < $this->total_drinks; $x++) { if ($this->engineer[$i][$x] != NULL) echo $this->engineer[$i][$x] . ","; } echo "<br/>"; } } public function __setupScoreBoard() { //Array for chosen engineers $total_chosen = 0; $flag1 = 0; for ($i = 0; $i < $this->total_engineers / 2; $i++) { $chosen[$i] = 0; for ($o = 0; $o < 2; $o++) $this->scoreboard[$i][$o] = ""; } //Loop for the upper group of engineers for ($i = 0; $i < $this->total_engineers / 2; $i++) { //Loop for taking preference score between 2 Engineers for ($j = $this->total_engineers - 1; $j > ($this->total_engineers / 2 - 1); $j--) { //Starting score $score = 0; //Loop for every drink in the upper group of engineers for ($k = 0; $k < $this->total_drinks; $k++) { //Checking for chosen engineers if ($total_chosen > 0) { for ($x = 0; $x < $total_chosen; $x++) { if ($j == $chosen[$x]) { $flag1 = 1; } } if ($flag1 == 1) { $flag1 = 0; break; } } if ($this->engineer[$i][$k] == -1) break; //Loop for taking preference score between 2 drinks for ($l = 0; $l < $this->total_drinks; $l++) { if ($this->engineer[$i][$k] == $this->engineer[$j][$l]) { //check position if ($k == $l) { $score += ( $this->total_drinks - $k) * ($this->total_drinks - $k); $this->__output("+" . ($this->total_drinks - $k) * ($this->total_drinks - $k) . "<br/>"); //Debug break; } else if ($k < $l) { $score += ( $this->total_drinks - $k); $this->__output("+" . ($this->total_drinks - $k) . "<br/>"); //Debug break; } else { break; } } else { if ($l == ($this->total_drinks) - 1) { $score += ( $this->total_drinks - $k); $this->__output("+" . ($this->total_drinks - $k) . "<br/>"); //Debug do o day no' + sai o.o ko sai dau .... break; } } } } $this->__output("E[" . $i . "] and E[" . $j . "] Preference Score:" . $score . "<br/>"); //Debug purpose //Score update if ($score >= $this->scoreboard[$i][0]) { $this->scoreboard[$i][0] = $score; $this->scoreboard[$i][1] = $j; } } //Add the chosen $chosen[$i] = $this->scoreboard[$i][1]; $this->__output("E[" . $i . "] choose E[" . $this->scoreboard[$i][1] . "] with Highest Preference Score:" . $this->scoreboard[$i][0] . "<br/>"); // o day may` lam` mat' 1 do`ng debug $total_chosen++; } } public function __getEngineerDrinkNames($EngineerID) { $text = ""; foreach ($this->engineer[$EngineerID] as $key => $value) { if ($value != -1) $text .= $this->drink[$value] . "[{$value}],"; } $text = substr($text, 0, -1); return $text; } public function __output($string) { if ($this->enable_output == true) { // tat ca deu c echo $string; } else return ""; } } $algotest = new algotest("inputfile.txt"); $algotest->enable_output = true; //initialization $algotest->__readline(1000); $algotest->__output("<html><head><title>asdasd</title></head><body>"); $algotest->__output("<h1>DEBUG</h1>"); //echo("Total Engineers: {$algotest->total_engineers}<br/>"); //echo("Total Drinks: {$algotest->total_drinks}<br/><br/>"); //$algotest->__displayDrinkToEngineer(); //echo("<h3>OUTPUT</h3>"); echo $algotest->__setupScoreBoard(); $algotest->__output("<br/>"); for ($i = 0; $i < ($algotest->total_engineers / 2); $i++) { echo($i . " " . $algotest->scoreboard[$i][1] . "<br/>"); } $algotest->__output("<br/>"); $algotest->__output("</body>"); $algotest->__output("</html>"); ?>