Last active
August 29, 2015 14:05
-
-
Save hidenorigoto/15c85b9b70406b9574b4 to your computer and use it in GitHub Desktop.
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 | |
class Lessons | |
{ | |
public function main($input) | |
{ | |
$submissions = $this->processInput($input); | |
$results = $this->mainProcess($submissions); | |
$output = $this->printResult($results); | |
return $output; | |
} | |
/** | |
* クラス振り分け処理 | |
* | |
* @param $submissions | |
* @return array | |
*/ | |
public function mainProcess($submissions) | |
{ | |
// 振り分け結果格納用変数 | |
$classes = []; | |
// 申込順に処理 | |
foreach ($submissions as $submisison) | |
{ | |
// 第1希望を取り出す | |
$kibouYoubiId = $submisison['kiboujun'][0]; | |
// その曜日に割り当てる | |
$classes[$kibouYoubiId][] | |
= $submisison['bangou']; | |
} | |
return $classes; | |
} | |
/** | |
* 入力値の加工 | |
* | |
* @param $input | |
* @return array | |
*/ | |
public function processInput($input) | |
{ | |
// 申込状況のデータ構造格納用変数 | |
$submissions = []; | |
// 申込単位に分割する( | で分割) | |
$tempList1 = explode("|", $input); | |
// 1つ1つの申込を、 | |
// 社員番号と希望順に分割する( _ で分割) | |
foreach ($tempList1 as $temp1) | |
{ | |
$temp2 = explode("_", $temp1); | |
$syainbangou = $temp2[0]; | |
// 希望順を分割する(文字列を配列に) | |
$kibouList = str_split($temp2[1]); | |
$submissions[] = [ | |
'bangou' => $syainbangou, | |
'kiboujun' => $kibouList, | |
]; | |
} | |
return $submissions; | |
} | |
/** | |
* @param $classes | |
* @return string | |
*/ | |
public function printResult($classes) | |
{ | |
// 結果文字列変数 | |
$output = ""; | |
ksort($classes); | |
foreach ($classes as $weekId => $syainList) | |
{ | |
sort($syainList, SORT_NUMERIC); | |
$line = $weekId . '_' . implode(":", $syainList); | |
if ($output != "") { $output .= "|"; } | |
$output .= $line; | |
} | |
return $output; | |
} | |
} |
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 | |
<?php | |
/** | |
* This file is part of the Nagoya.Lessons | |
* | |
* @license http://opensource.org/licenses/bsd-license.php BSD | |
*/ | |
namespace Nagoya\Lessons; | |
class Lessons | |
{ | |
public function main($input) | |
{ | |
$submissions = $this->processInput($input); | |
$results = $this->mainProcess($submissions); | |
$output = $this->printResult($results); | |
return $output; | |
} | |
/** | |
* クラス振り分け処理 | |
* | |
* @param $submissions | |
* @return array | |
*/ | |
public function mainProcess($submissions) | |
{ | |
// 振り分け結果格納用変数 | |
$classes = []; | |
// 未処理の申込を順に処理 | |
while (true) | |
{ | |
if (count($submissions) == 0) break; | |
$submission = array_shift($submissions); | |
// すべての希望を処理していたら、あきらめる | |
if (count($submission['kiboujun']) == 0) continue; | |
// 希望を取り出す | |
$kibouYoubiId = array_shift($submission['kiboujun']); | |
// 曜日がいっぱいか? | |
if (isset($classes[$kibouYoubiId]) && count($classes[$kibouYoubiId]) >= 4) { | |
// 未処理リストの最後尾に追加 | |
array_push($submissions, $submission); | |
} else { | |
// その曜日に割り当てる | |
$classes[$kibouYoubiId][] | |
= $submission['bangou']; | |
} | |
} | |
return $classes; | |
} | |
/** | |
* 入力値の加工 | |
* | |
* @param $input | |
* @return array | |
*/ | |
public function processInput($input) | |
{ | |
// 申込状況のデータ構造格納用変数 | |
$submissions = []; | |
// 申込単位に分割する( | で分割) | |
$tempList1 = explode("|", $input); | |
// 1つ1つの申込を、 | |
// 社員番号と希望順に分割する( _ で分割) | |
foreach ($tempList1 as $temp1) | |
{ | |
$temp2 = explode("_", $temp1); | |
$syainbangou = $temp2[0]; | |
// 希望順を分割する(文字列を配列に) | |
$kibouList = str_split($temp2[1]); | |
$submissions[] = [ | |
'bangou' => $syainbangou, | |
'kiboujun' => $kibouList, | |
]; | |
} | |
return $submissions; | |
} | |
/** | |
* @param $classes | |
* @return string | |
*/ | |
public function printResult($classes) | |
{ | |
// 結果文字列変数 | |
$output = ""; | |
ksort($classes); | |
foreach ($classes as $weekId => $syainList) | |
{ | |
sort($syainList, SORT_NUMERIC); | |
$line = $weekId . '_' . implode(":", $syainList); | |
if ($output != "") { $output .= "|"; } | |
$output .= $line; | |
} | |
return $output; | |
} | |
} |
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 | |
public function processInput($input) | |
{ | |
// 申込状況のデータ構造格納用変数 | |
$submissions = []; | |
// 申込単位に分割する( | で分割) | |
$tempList1 = explode("|", $input); | |
// 1つ1つの申込を、 | |
// 社員番号と希望順に分割する( _ で分割) | |
foreach ($tempList1 as $temp1) | |
{ | |
$temp2 = explode("_", $temp1); | |
$syainbangou = $temp2[0]; | |
// 希望順を分割する(文字列を配列に) | |
$kibouList = str_split($temp2[1]); | |
$submissions[] = [ | |
'bangou' => $syainbangou, | |
'kiboujun' => $kibouList, | |
]; | |
} | |
return $submissions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment