Last active
January 17, 2020 11:16
-
-
Save zhuomingliang/9109582 to your computer and use it in GitHub Desktop.
Inverse fizzbuzz
This file contains hidden or 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 FizzBuzz { | |
| public function __construct($number) { | |
| $this->number = $number; | |
| } | |
| public function __toString() { | |
| if ($this->number < 1) return ''; | |
| if ($this->number % 3 == 0) { | |
| if ($this->number % 5 == 0) { | |
| return 'FizzBuzz'; | |
| } | |
| return 'Fizz'; | |
| } | |
| if ($this->number % 5 ==0 ) { | |
| return 'Buzz'; | |
| } | |
| return ''; | |
| } | |
| } | |
| class InverseFizzBuzz { | |
| public function __construct($list) { | |
| $this->list = $list; | |
| } | |
| public function sequence() { | |
| $sequences = array(); | |
| for ($n = 1; $n <= 100; $n++) { | |
| // Find the first FizzBuzz from the list | |
| if ($this->list[0] != (string) (new FizzBuzz($n))) | |
| continue; | |
| if ($sequence = $this->_sequence($this->list, $n)) | |
| $sequences[] = $sequence; | |
| } | |
| if ($sequence = $this->_min($sequences)) { | |
| return $sequence; | |
| } | |
| return null; | |
| } | |
| private function _sequence($list, $n) { | |
| $sequence = array($n); // $n is first sequence; | |
| array_shift($list); | |
| $length = count($list); | |
| for ($i = 1; $i <= $length; $i++) { | |
| if ( count($list) == 0) break; | |
| for ($j = $n + 1, $end = $n + 15; $j <= $end; $j++ && $j <= 100) { // the next fizzbuzz will be between $n + 1 and $n + 15 | |
| $sequence[] = $j; // get the next sequence | |
| $fizzbuzz = (string) new FizzBuzz($j); | |
| if ($fizzbuzz && $list[0] != $fizzbuzz) break; | |
| if ($list[0] == $fizzbuzz) { | |
| array_shift($list); | |
| if (count($list) == 0) // list is empty, break the loop | |
| break; | |
| $end += 15; // else get the next end number | |
| } | |
| } | |
| } | |
| if (count($list) > 0) { | |
| return null; | |
| } | |
| return $sequence; | |
| } | |
| private function _min($sequences) { | |
| if (count($sequences) == 0) return null; | |
| $n = count($sequences[0]); | |
| $sequence = $sequences[0]; | |
| foreach ($sequences as $value) { | |
| if (count($value) < $n) { | |
| $sequence = $value; | |
| $n = count($value); | |
| } | |
| } | |
| return $sequence; | |
| } | |
| } | |
| while($line = trim(fgets(STDIN))) { | |
| $ifb = new InverseFizzBuzz(explode(' ', $line)); | |
| $ifb->sequence(); | |
| //var_dump($ifb->sequence()); | |
| } | |
| ?> |
This file contains hidden or 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 InverseFizzBuzz { | |
| public $list; | |
| public $fblist; | |
| public function __construct($list) { | |
| $this->list = $list; | |
| $this->fblist = $this->_getFBList(0, 100); | |
| } | |
| public function sequence() { | |
| $sequences = array(); | |
| for ($n = 1; $n <= 100; $n++) { | |
| $fizzbuzz = array_key_exists($n, $this->fblist) ? $this->fblist[$n] : null; | |
| // Find the first FizzBuzz from the list | |
| if ($this->list[0] != $fizzbuzz) | |
| continue; | |
| if ($sequence = $this->_sequence($this->list, $n)) | |
| $sequences[] = $sequence; | |
| } | |
| if ($sequence = $this->_min($sequences)) { | |
| return $sequence; | |
| } | |
| return null; | |
| } | |
| private function _getFBList($start, $end) { | |
| return array_map(function ($n) { | |
| if ( $n % 15 == 0 ) return 'FizzBuzz'; | |
| else if ( $n % 3 == 0) return 'Fizz'; | |
| else return 'Buzz'; | |
| }, array_filter(range($start, $end), function ($n) { | |
| return $n && ($n % 3 == 0 || $n % 5 == 0); | |
| })); | |
| } | |
| private function _sequence($list, $n) { | |
| $sequence = array($n); // $n is first sequence; | |
| array_shift($list); | |
| $length = count($list); | |
| for ($i = 1; $i <= $length; $i++) { | |
| if ( count($list) == 0) break; | |
| for ($j = $n + 1, $end = $n + 15; $j <= $end && $j <= 100; $j++) { // the next fizzbuzz will be between $n + 1 and $n + 15 | |
| $sequence[] = $j; // get the next sequence | |
| $fizzbuzz = array_key_exists($j, $this->fblist) ? $this->fblist[$j] : null; | |
| if ($fizzbuzz && $list[0] != $fizzbuzz) break; | |
| if ($list[0] == $fizzbuzz) { | |
| array_shift($list); | |
| if (count($list) == 0) // list is empty, break the loop | |
| break; | |
| $end += 15; // else get the next end number | |
| } | |
| } | |
| } | |
| if (count($list) > 0) { | |
| return null; | |
| } | |
| return $sequence; | |
| } | |
| private function _min($sequences) { | |
| if (count($sequences) == 0) return null; | |
| $n = count($sequences[0]); | |
| $sequence = $sequences[0]; | |
| foreach ($sequences as $value) { | |
| if (count($value) < $n) { | |
| $sequence = $value; | |
| $n = count($value); | |
| } | |
| } | |
| return $sequence; | |
| } | |
| } | |
| while($line = trim(fgets(STDIN))) { | |
| $ifb = new InverseFizzBuzz(explode(' ', $line)); | |
| $ifb->sequence(); | |
| //var_dump($ifb->sequence()); | |
| } | |
| ?> |
This file contains hidden or 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 InverseFizzBuzz { | |
| public $list; | |
| public $fblist; | |
| public function __construct($list) { | |
| $this->list = $list; | |
| $this->fb = implode('', $this->_getFBList(0, 200)); | |
| } | |
| public function sequence() { | |
| $fb = '/' . implode('\s*', array_map(function ($str) { | |
| if ( $str == 'FizzBuzz' ) return 'G'; | |
| else if ( $str == 'Fizz' ) return 'F'; | |
| else if ( $str == 'Buzz' ) return 'B'; | |
| else return ' '; | |
| }, $this->list)) . '/'; | |
| preg_match_all($fb, $this->fb, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); | |
| return $this->_min($matches); | |
| } | |
| private function _getFBList($start, $end) { | |
| return array_map(function ($n) { | |
| if ( $n == 0 ) return ' '; | |
| if ( $n % 15 == 0 ) return 'G'; | |
| else if ( $n % 3 == 0) return 'F'; | |
| else if ( $n % 5 == 0) return 'B'; | |
| else return ' '; | |
| }, range($start, $end)); | |
| } | |
| private function _min($sequences) { | |
| if (count($sequences) == 0) return null; | |
| $sequence = $sequences[0][0]; | |
| $n = strlen($sequences[0][0][0]); | |
| array_shift($sequences); | |
| foreach ($sequences as $_sequence) { | |
| if (($i = strlen($_sequence[0][0])) < $n) { | |
| $sequence = $_sequence[0]; | |
| $n = $i; | |
| } | |
| } | |
| return range($sequence[1], strlen($sequence[0]) + $sequence[1] - 1); | |
| } | |
| } | |
| while($line = trim(fgets(STDIN))) { | |
| $ifb = new InverseFizzBuzz(explode(' ', $line)); | |
| $ifb->sequence(); | |
| //var_dump($ifb->sequence()); | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inverse_fizzbuzz.php
real 0m0.318s
user 0m0.000s
sys 0m0.030s
Inverse_fizzbuzz2.php
real 0m0.230s
user 0m0.015s
sys 0m0.016s
Inverse_fizzbuzz3.php
real 0m0.177s
user 0m0.000s
sys 0m0.015s