Advertisement
Guest User

Untitled

a guest
Feb 5th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2. $arr = explode(' ', readline());
  3. $input = readline();
  4.  
  5. while ($input != "3:1") {
  6.     $commands = explode(" ", $input);
  7.     $command = $commands[0];
  8.     $startIndex = intval($commands[1]);
  9.     $endIndex = intval($commands[2]);
  10.     $word = "";
  11.     // merge 0 3
  12.     // merge 3 4
  13.     if ($startIndex < 0 || $startIndex > count($arr)) {
  14.         $startIndex = 0;
  15.     }
  16.     if ($endIndex > count($arr) - 1 || $endIndex < 0) {
  17.         $endIndex = count($arr) - 1;
  18.     }
  19.     if ($command == "merge") {
  20.         for ($i = $startIndex; $i <= $endIndex; $i++) {
  21.             $word .= $arr[$i];
  22.         }
  23.         array_splice($arr, $startIndex, $endIndex - $startIndex + 1,$word);  
  24.     } elseif ($command == "divide") {
  25.         $divided = [];
  26.         $divide = intval($commands[2]);
  27.         $word = $arr[$startIndex];
  28.         $parts = strlen($word) / $divide;
  29.         for ($i = 0; $i < $divide; $i++) {
  30.             if ($i == $divide - 1) {
  31.                 $divided  [] = substr($word, $i * $parts);
  32.             } else {
  33.                 $divided  [] = substr($word, $i * $parts, $parts);
  34.             }
  35.         }
  36.        array_splice($arr, $startIndex, 1,implode(" ", $divided));
  37.     }
  38.     $input = readline();
  39. }
  40. echo implode(" ", $arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement