SHARE
TWEET

Untitled

a guest Sep 18th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function anagrams(string $word, array $words, bool $isVerbose=FALSE): array {
  3.   // Your code here
  4.   $l1  = strlen($word);
  5.   $ret = [];
  6.   if( $isVerbose === TRUE )
  7.     echo "WORD COUNT: " . $l1 . "\n";
  8.   for ($i=0; $i<count($words); $i++){
  9.     $cur = $words[$i];
  10.     $isAppending = TRUE;
  11.     if ( strlen($cur) !== $l1 ){
  12.       if( $isVerbose === TRUE )
  13.         echo "SKIPPING: " . $cur + "\n";
  14.       continue;
  15.     }  
  16.     $lcur = str_split($cur);
  17.     for($j=0; $j<$l1; $j++){
  18.       $toFind = $word[$j];
  19.       $key    = array_search($toFind, $lcur);
  20.       if ($key !== FALSE) {
  21.           unset($lcur[$key]);
  22.           if ($isVerbose === TRUE){
  23.             echo "\tNEST".$j.": \n";
  24.             var_dump($lcur);
  25.           }  
  26.       }  
  27.       else{
  28.         $isAppending = FALSE;
  29.         if ($isVerbose === TRUE)
  30.           echo "BREAKING\n";
  31.         break;
  32.       }  
  33.     }//end j-for
  34.     if($isAppending === TRUE && count($lcur) === 0){
  35.       array_push($ret, $cur);
  36.     }  
  37.     else if($isVerbose === TRUE ){
  38.       echo "isAppending: " . $isAppending . "\n";
  39.       var_dump($lcur);
  40.       echo "\n";
  41.     }  
  42.   }//end i-for
  43.   return $ret;
  44. }//end anagrams
  45.  
  46. /**********************************************************************
  47. Test cases below:
  48. To Run:
  49.    cd ~/.../code-wars/anagarms/
  50.    php index.php
  51. **********************************************************************/
  52.  
  53. $passes = 0;
  54. $total  = 1;
  55. if (['a'] === anagrams('a', ['a', 'b', 'c', 'd'])){
  56.    $passes += 1;
  57. }
  58. else
  59.    echo "FAILED TEST 1\n";
  60.  
  61. $total  += 1;
  62. if( ['carer', 'arcre', 'carre'] === anagrams('racer', ['carer', 'arcre', 'carre', 'racrs', 'racers', 'arceer', 'raccer', 'carrer', 'cerarr']) ){
  63.    $passes += 1;
  64. }
  65. else{
  66.    echo "FAILED TEST 2\n";
  67.    var_dump(anagrams('racer', ['carer', 'arcre', 'carre', 'racrs', 'racers', 'arceer', 'raccer', 'carrer', 'cerarr'], TRUE));
  68. }
  69.  
  70. $total  += 1;
  71. if (['aabb', 'bbaa'] === anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])){
  72.    $passes += 1;
  73. }
  74. else
  75.    echo "FAILED TEST 3\n";
  76.  
  77. $total  += 1;
  78. if (['carer', 'racer'] === anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])){
  79.    $passes += 1;
  80. }
  81. else
  82.    echo "FAILED TEST 4\n";
  83.  
  84. $total  += 1;
  85. if ([] === anagrams('laser', ['lazing', 'lazy',  'lacer'])){
  86.    $passes += 1;
  87. }
  88. else
  89.    echo "FAILED TEST 5\n";
  90.  
  91. echo $passes . " OUT OF " . $total . " TESTS PASSED\n";
  92. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top