Last active
August 29, 2015 13:57
-
-
Save ounziw/9563282 to your computer and use it in GitHub Desktop.
nagoya php vol.4 の課題 http://nabetani.sakura.ne.jp/hena/ord18notfork/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
class Cart { | |
protected $capabilitylist; | |
protected $queuelist; | |
// x が来たら、それ以降は $jammedlist に並ぶ | |
protected $jammedlist; | |
/** | |
* 初期設定 | |
* | |
* @param array $startqueue | |
* @param array $startcapability | |
*/ | |
public function __construct($startqueue=array(0,0,0,0,0), $startcapability=array(2,7,3,5,2)) { | |
$this->capabilitylist = $startcapability; | |
$this->queuelist = $startqueue; | |
$this->jammedlist = array_pad(array(),count($startqueue),0); | |
} | |
/** | |
* メインの処理をする関数 | |
* | |
* @param string $inputdata | |
* @return string | |
*/ | |
public function process($inputdata) { | |
$inputlist = str_split($inputdata); | |
foreach ($inputlist as $input) { | |
if (is_numeric($input)) { | |
$this->addCustomer($input); | |
} elseif ($input == 'x') { | |
$this->addJammed(); | |
} elseif ($input == '.') { | |
$this->reduceCustomer(); | |
} else { | |
echo 'Input Error!'; | |
} | |
} | |
$total = $this->addArray($this->queuelist, $this->jammedlist); | |
return implode(',', $total); | |
} | |
/** | |
* 客が来た時の処理 | |
* | |
* @param int/string $num | |
*/ | |
protected function addCustomer($num) { | |
$shortest = $this->shortestQueue(); | |
if ($this->jammedlist[$shortest] >= 1) { | |
$this->jammedlist[$shortest] += $num; | |
} else { | |
$this->queuelist[$shortest] += $num; | |
} | |
} | |
/** | |
* x が来た時の処理 | |
*/ | |
protected function addJammed() { | |
$shortest = $this->shortestQueue(); | |
$this->jammedlist[$shortest] += 1; | |
} | |
/** | |
* 列は $queuelist と $jammedlist の合計 | |
* | |
* @return int | |
*/ | |
protected function shortestQueue() { | |
$arr = $this->addArray($this->queuelist, $this->jammedlist); | |
return $this->shortest($arr); | |
} | |
/** | |
* 最小の値を探し、その値が最初に来る位置を返す | |
* | |
* @param array $array | |
* @return int | |
*/ | |
public function shortest($array) { | |
$shortest = min($array); | |
return array_search($shortest, $array); | |
} | |
/** | |
* 列から客を減らす。$queuelistのみ。$jammedlistからは引かない。 | |
*/ | |
protected function reduceCustomer() { | |
$len = count($this->queuelist); | |
for($i=0;$i<$len;$i++) { | |
$this->queuelist[$i] -= min($this->queuelist[$i], $this->capabilitylist[$i]); | |
} | |
} | |
/** | |
* 配列の要素毎に足す | |
* @param array $arr1 | |
* @param array $arr2 | |
* @return array | |
*/ | |
public function addArray($arr1,$arr2) { | |
$len = max(count($arr1), count($arr2)); | |
$total = array_pad(array(), $len, 0); | |
for($i=0;$i<$len;$i++) { | |
$total[$i] = $arr1[$i] + $arr2[$i]; | |
} | |
return $total; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
class cartTest extends PHPUnit_Framework_TestCase { | |
protected $cart; | |
function setUp() { | |
$this->cart = new Cart(); | |
} | |
/** | |
* @dataProvider shortestprovider | |
*/ | |
function testshortest($expected,$data) { | |
$output = $this->cart->shortest($data); | |
$this->assertEquals($expected,$output); | |
} | |
public function shortestprovider() | |
{ | |
return array( | |
array(0,array(1,1,1,1,1)), | |
array(1,array(2,1,1,1,1)), | |
array(4,array(1,1,1,1,0)), | |
); | |
} | |
/** | |
* @dataProvider queueprovider | |
*/ | |
function testprocess($expected,$data) { | |
$output = $this->cart->process($data); | |
$this->assertEquals($expected,$output); | |
} | |
public function queueprovider() | |
{ | |
return array( | |
array('1,0,0,0,0','31.'), | |
array('6,6,0,0,0','832.6'), | |
array('3,5,6,8,0','.5.568'), | |
array('16,12,17,18,12','7677749325927'), | |
array('7,6,4,10,5','987x654x.32'), | |
array('9,9,9,9,9','99569x33'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment