当时拉钩网ThoughtWorks出了一道面试题(https://www.jinshuju.net/f/EGQL3D),本人用PHP实现了一下,当时忘记了把代码分享出来,今天特来补上。
FizzBuzzWhizz 这到算法题的规则如下:
1,语言不限,Java, C#, Ruby, C++, Js, Python, Scala, objective-C统统可以,小语种也没问题,只要你擅长;
2,强烈建议写单元测试;
3,请展示出你超赞的面向对象/函数式编程功底;
4,建议尽量减少圈复杂度;
5,请提交可运行的代码,及相关构建脚本/说明文档(代码运行平台和环境);
FizzBuzzWhizz
你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是:
1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。
2. 让所有学生拍成一队,然后按顺序报数。
5. 学
生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。
如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。
<?php /**
*
* atuhor cherry.chen
* desc Gamm initial configuration parameters
* date 2014/4/29
*
*/
class GameCfg {
const FIZZ = "Fizz";
const BUZZ = "Buzz";
const WHIZZ = "Whizz";
const ZERO = 0;
const ONE = 1;
const TWO = 2;
const NEWLINE="\n"; //<br/> running with console ,please modified "\n"
} ?>
Game.php 代码如下:
<?php require_once (dirname(__FILE__) . "/../cfg/GameCfg.php"); /**
* author cherry
* desc Game Main Class
* date 2014/4/29
*/
class Game { private $numbers = array();
private $init_array_range = array(); public function __construct($number_arrs, $init_array_range) {
if (count($number_arrs) == 3) {
$this->numbers = $number_arrs;
}
$this->init_array_range = $init_array_range;
} /**
* init data and call the doGame
* @param <type> $init_array_range
*/
public function startGame() {
$init_array_range = $this->init_array_range;
$numbers = $this->numbers;
if (!empty($numbers)) {
$count = count($init_array_range);
for ($i = 0; $i < $count; $i++) {
$this->doGame($numbers, $init_array_range[$i]);
}
}
} /**
* do Game form the pre-rules
* @param <type> $i
*/
private function doGame($numbers, $i) {
$str_contains = $this->numberContains($numbers, $i);
if ($str_contains != null && $str_contains != "") {
echo $str_contains . GameCfg::NEWLINE;
} else {
$msts = $this->numberMod($numbers, $i);
if ($msts != "" && $msts != null) {
echo $msts . GameCfg::NEWLINE;
} else {
echo $i . GameCfg::NEWLINE;
}
}
} /**
* Judge this number satisfy the rule 5 yes or not
* @param <type> $numbers
* @param <type> $i
* @return <type>
*/
private function numberContains($numbers, $i) {
$ars = array();
$intoarrays = $this->intToArray($i);
$number_flag = $this->findFirstNumFlag($numbers, $intoarrays);
if (in_array($numbers[GameCfg::ZERO], $intoarrays) && $number_flag) {
array_push($ars, GameCfg::FIZZ);
} else if (in_array($numbers[GameCfg::ONE], $intoarrays) && $number_flag) {
if (count($ars) == 0) {
array_push($ars, GameCfg::BUZZ);
}
} else if (in_array($numbers[GameCfg::TWO], $intoarrays) && $number_flag) {
if (count($ars) == 0) {
array_push($ars, GameCfg::WHIZZ);
}
}
return implode("", $ars);
} /**
* Judge this number satisfy the rule 3,4 yes or not
* @param <type> $numbers
* @param <type> $i
* @return <type>
*/
private function numberMod($numbers, $i) {
$ars = array();
if ($i % $numbers[GameCfg::ZERO] == 0) {
array_push($ars, GameCfg::FIZZ);
} if ($i % $numbers[GameCfg::ONE] == 0) {
array_push($ars, GameCfg::BUZZ);
} if ($i % $numbers[GameCfg::TWO] == 0) {
array_push($ars, GameCfg::WHIZZ);
}
return implode("", $ars);
} private function intToArray($val) {
$input_arrs = array();
$val = $val . "";
$len = strlen($val); for ($i = 0; $i < $len; $i++) {
array_push($input_arrs, $val{$i});
}
return $input_arrs;
} private function findFirstNumFlag($numbers, $intoarrays) {
return in_array($numbers[GameCfg::ZERO], $intoarrays);
} } ?>
TestGame.php 代码如下:
<?php require_once (dirname(__FILE__) . "/../code/Game.php"); $number_arrs = array(3, 5, 7);
$init_array_range = array(); for ($i = 0; $i < 100; $i++) {
$init_array_range[$i] = $i + 1;
} $game = new Game($number_arrs, $init_array_range);
$game->startGame();
?>
README.txt 说明如下:
1.Before runing this script ,please make sure you have installed PHP enviroment.
2.usage
In Linux: if you decompress the package in the /var/www/html directory
PHP installed path: /usr/local/php
/usr/loca/php/bin/php /var/www/html/game/test/TestGame.php In Windows: if you decompress the package in the D:\demo directory
PHP installed path: D:\programs\php
D:\programs\php\bin\php D:\demo\game/test/TestGame.php
运行效果图如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJ4AAAIJCAIAAADbGxP+AAAKP0lEQVR4nO3d0ZayNhRAYd//pe1F2zUISc4RAkm2+7uqExQmewka/aevt6Beow9AdzEtlmmx/tK+Xq/Xy9Icr/f/UU0LY1osT8hYpsUyLZZpsUyLZVos02KZFsu0WLbEMi2WabFMi2VaLNNimRbLtFj/pfWLFjz7L9BYF2Of07oYhYSmZdgn9FmL8ZHQriT7D/XsiuGrJ6yPNz+jD0Y9+V4W67hcYWkI02KZEMu0WKbFMi2WabFMi2VarGxa3+wuJ7tkYdrluBqFZUisOG14lq494z0fjGVarOzUZ6q0t7Hrw7qltetssifJK+XsOkSHtHad09W0dp1WnLZxMS5cqDcbtEd1N9NiOdFYpsUyLZZpsUyLZVos02KZFuvv39eOPhJ1tk9rZgzTYpkWy7RY5Q9zBh6Qeql98mbd5ZkWy3/bg2VaLNNimRbLilimxTItlmmxTItlWizTYpkWyw8GsPzYB6vQ0roMQdrMaO1J7yfBY5kWK5j9dtriY7X31Ou4FeqW1q6zKZwkjyfV2mj7h8lR3aRDWrvOKZW2dvNt14kF19raFq07+wp5Dqmp3w6ZdhVONJZpsUyLZVos02KZFsu0WNm0viVdTm1dwX/mtbxsWi3HkFhx2vAs7RrynEyLlZ36TJX2NnZ9WLe0dp1N9iR5pZxdh+iQ1q5zuprWrtOK0zYuxoUL9WaD9qjuZlosJxrLtFimxTItlmmxTItlWqw+aX3DOqHCMsJx2SH5KLccoM7qk1YTMi1WNm1tBbixOOwK81h/E3r8j3clQOH+pp1PNm3x5u5RMns6N6oTUmmPdyg+Sribc6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