Finding the shortest sequence for the inverse FizzBuzz problem and the fizzbuzz shortest sequence

Source: Internet
Author: User

Finding the shortest sequence for the inverse FizzBuzz problem and the fizzbuzz shortest sequence

Problem description

FizzBuzz Problems: If a natural number greater than 0 can be divided into 3, "Fizz" will be output; if it can be divided into 5, "Buzz" will be output; if it can be divided into 3 and 5, "FizzBuzz" will be output "; otherwise, output yourself.

Shortest sequence of inverse FizzBuzz Problems: A non-digital output sequence with a FizzBuzz problem is known, and the shortest continuous digital sequence of the sequence can be obtained. For example, the shortest sequence of "Fizz" is "3", and the shortest sequence of "Fizz Buzz" is "9 10" instead of "3 4 5 ".

 

Programming implementation

1 <? Php 2 3/** 4 * @ author cenze 5*6 * anti-FizzBuzz problem finding the shortest sequence 7*8 * $ inverseFizzBuzz = new InverseFizzBuzz ([9 * 'fizz ', 10 * 'fizz ', 11 * 'buzz', 12 *]); 13 * $ inverseFizzBuzz-> sequence (): 14 * Array ([0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10) 15 */16 class InverseFizzBuzz 17 {18 // FizzBuzz problem assignment 19 const FizzBuzz = [20 'fizz '=> 3, 21 'bucket' => 5, 22 'maid => 15 23]; 24 25 // predefined question range 26 Private $ range = [27 1, 28 100 29]; 30 31 // The waiting sequence 32 private $ list = []; 33 34 // statistical 35 private $ listItemsCount = 0; 36 37 public function _ construct ($ list = [], $ range = []) 38 {39 $ this-> init ($ list, $ range ); 40} 41 42/** 43 * Waiting for sequence and interval initialization 44*45 * @ param array $ list 46 * Waiting for sequence 47 * @ param array $ range 48 * problem Interval 49 * @ param bool $ check 50 * check whether attribute Initialization is completed 51*52 * @ return void 53 */5 4 private function init ($ list = [], $ range = [], $ check = false) 55 {56 if ($ list) {57 $ this-> list = $ this-> inverseList ($ list); 58 $ this-> listItemsCount = count ($ this-> list); 59 if (! $ This-> listItemsCount) {60 exit ('invalid list. '); 61} 62} 63 64 if ($ range) {65 $ this-> range = $ range; 66} 67 68 if ($ check) {69 if (! $ This-> list) {70 exit ('Property list uninitialized. '); 71} 72} 73} 74 75/** 76 * reverse the sequence to be solved 77*78 * @ param array $ list 79 * sequence to be solved 80*81 * @ return mixed 82 */83 private function inverseList ($ list) 84 {85 $ inverseList = []; 86 foreach ($ list as $ item) {87 if (! Array_key_exists ($ item, self: FizzBuzz) {88 return null; 89} 90 $ inverseList [] = self: FizzBuzz [91 $ item 92]; 93} 94 95 return $ inverseList; 96} 97 98/** 99 * check whether the shortest sequence exists in the 100*101 * @ param array $ list102 * sequence to be searched 103 * @ param array $ range104 * problem interval 105*106*107 * @ return mixed108 */109 public function sequence ($ list = [], $ range = []) 110 {111 $ this-> init ($ list, $ range, true); 112 113 $ sequence S = $ this-> findSequences (); 114 if (! $ Sequences) {115 return 'found no sequences. '; 116} 117 118 $ sequence = $ this-> shortestSequence ($ sequences); 119 // After the shortestSequence is serialized, 120 return range ($ sequence [0], $ sequence [$ this-> listItemsCount-1]); 121} 122 123/** 124 * searches for the shortest sequence (not serialized) 125*126 * @ param array $ sequences127 * sequence set, including the shortestSequence of the shortestSequence ($ sequences) of the minimum sequence 128*129 * @ return array130 */131 private function) 132 {133 // The first entry is the shortestSequence minimum of 134 $ by default. = $ Sequences [0]; 135 // each sequence is regarded as a path with a corresponding length. ShortestSequenceLength = $ sequences [0] [$ this-> listItemsCount-1]-$ sequences [0] [0]; 136 // array_reduce () define a callback to search for the shortest sequence 138 $ findShortestSequence = function ($ shortestSequence, $ currentSequence) use (& $ shortestSequenceLength) {139 $ currentSequenceLength = $ currentSequence [$ this-> listItemsCount-1]-$ currentSequence [0]; 140 if ($ currentSequenceLength <$ shortestSequenceLength) {141 $ second = $ currentSequenceLength; 142 return $ currentSequence; 143} else {144 return $ shortestSequence; 145} 146}; 147 148 return array_reduce ($ sequences, $ findShortestSequence, $ shortestSequence ); 149} 150 151/** 152 * search the sequence of the specified project from the specified position 153*154 * @ param int $ item155 * specified project 156 * @ param int $ start157 * Start search location 158*159 * @ return int160 */161 private function findItemSequenceFromPosition ($ item, $ start) 162 {163 if ($ start % $ item = 0) {164 return $ start; 165} 166 167 return $ start + ($ item-$ start % $ item ); 168} 169 170/** 171 * search for the first sequence starting from the specified position 172*173 * @ param int $ start174 * start position 175*176 * @ return mixed177 */178 private function findSequenceFromPosition ($ start) 179 {180 $ listSequence = []; 181 for ($ I = 0; $ I <$ this-> listItemsCount; $ I ++) {182 $ itemSequence = $ this-> findItemSequenceFromPosition ($ this-> list [$ I], $ start); 183 if ($ itemSequence> $ this-> range [1]) {184 return null; 185} else {186 $ listSequence [] = $ itemSequence; 187 $ start = $ itemSequence + 1; 188} 189 190 return $ listSequence; 192} 193 194/** 195 * find multiple sequences, including the shortest sequence 196*197 * @ return void198 */199 private function findSequences () 200 {201 $ sequences = []; 202 // use the first item as the initial search position to search in the specified range, gradually search 203 for ($ start = $ this-> list [0]; $ start <= $ this-> range [1]; $ start + = $ this-> list [0]) {204 // you only need to find the first sequence starting at the specified position. Then, you can find the 205 if ($ sequence = $ this-> findSequenceFromPosition ($ start) {206 $ sequences [] = $ sequence; 207} 208} 209 210 return $ sequences; 211} 212}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.