zoukankan      html  css  js  c++  java
  • ThoughtWorks FizzBuzzWhizz 代码实现

    当时拉钩网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. 让所有学生拍成一队,然后按顺序报数。

    3. 学生报数时,如果所报数字是第一个特殊数(3)的倍数,那么不能说该数字,而要说Fizz;如果所报数字是第二个特殊数(5)的倍数,那么要说Buzz;如果所报数字是第三个特殊数(7)的倍数,那么要说Whizz。
    4. 学生报数时,如果所报数字同时是两个特殊数的倍数情况下,也要特殊处理,比如第一个特殊数和第二个特殊数的倍数,那么不能说该数字,而是要说FizzBuzz, 以此类推。如果同时是三个特殊数的倍数,那么要说FizzBuzzWhizz。
    5. 学 生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。 如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。
     
    我实现这个这个需求用到了一下几个文件:
    GameCfg.php 是这个算法用到的配置文件,在这里主要是定义一些常量之类的
    Game.php 是这个算法实现的主体
    TestGame.php 主要是测试这个算法的文件
    README.txt 运行这个脚本的说明
     
    GameCfg.php 代码如下:
    <?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="
    "; //<br/> running with console ,please modified "
    "
    }
    
    ?>

    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:programsphp
                  D:programsphpinphp D:demogame/test/TestGame.php

    运行效果图如下:

    完整代码可以到这里下载:http://url.cn/Nq9aJg

  • 相关阅读:
    LeetCode 16 3sum closest
    LeetCode 15 3Sum
    LeetCode 14 Longest Common Prefix
    jQuery数组遍历
    jQuery DOM对象与jQuery对象转换
    jQuery选择器
    jQuery类和样式操作
    js节点操作
    js全选反选
    innerHTML和innerTEXT的区别
  • 原文地址:https://www.cnblogs.com/stackflow/p/3918334.html
Copyright © 2011-2022 走看看