emotrend

Dictionary

Dec 13th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. <?php
  2.  
  3. $wordPairs = explode(" | ", readline());
  4. $wordsToSearchFor = explode(" | ", readline());
  5. $command = readline();
  6.  
  7. $words = [];
  8. for ($i = 0; $i < count($wordPairs);$i++){
  9.     list($word, $desc) = explode(": ", $wordPairs[$i]);
  10.     $words[$word][] = $desc;
  11. }
  12.  
  13. ksort($words);
  14. foreach ($words as $word => $desc) {
  15.     if ($command === "List") {
  16.         echo $word . " ";
  17.     } else if ($command === "End") {
  18.         if (in_array($word, $wordsToSearchFor)) {
  19.             echo $word . PHP_EOL;
  20.             usort($desc, function ($val1, $val2) use ($desc) {
  21.                return strlen($val2) <=> strlen($val1);
  22.             });
  23.             foreach ($desc as $item) {
  24.                 echo " -$item" . PHP_EOL;
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment