MartinGeorgiev

Problem 2. On the Way to Annapurna

Aug 2nd, 2019 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2.  
  3. $shops = [];
  4. $input = readline();
  5.  
  6. while ("END" !== $input) {
  7.     //Add->PeakSports->Map,Navigation,Compass
  8.     //                ->OneItem
  9.     $args = explode("->", $input);
  10.     $command = $args[0];
  11.     $shopName = $args[1];
  12.  
  13.     if ($command === "Add") {
  14.         $items = $args[2];
  15.         $items = explode(",", $items);
  16.         if (count($items) > 1) {
  17.             foreach ($items as $item) {
  18.                 $shops[$shopName][] = $item;
  19.             }
  20.         } else {
  21.             $shops[$shopName][] = $args[2];
  22.         }
  23.     } else {
  24.         //Remove->Groceries
  25.         unset($shops[$shopName]);
  26.     }
  27.  
  28.  
  29.     $input = readline();
  30. }
  31. uksort($shops, function ($shop1, $shop2) use ($shops) {
  32.  
  33.     $coun1 = count($shops[$shop1]);
  34.     $coun2 = count($shops[$shop2]);
  35.  
  36.     if ($coun1 === $coun2) {
  37.         return $shop2 <=> $shop1;
  38.     }
  39.     return $coun2 <=> $coun1;
  40.  
  41. });
  42.  
  43. echo "Stores list:" . PHP_EOL;
  44. foreach ($shops as $name => $products) {
  45.     echo $name . PHP_EOL;
  46.     foreach ($products as $product) {
  47.         echo "<<$product>>" . PHP_EOL;
  48.     }
  49. }
Add Comment
Please, Sign In to add comment