Advertisement
zucxk

Untitled

May 10th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?php
  2. function replace($arr) {
  3.   if((count($arr)%2)==0) {
  4.       $res=[];
  5.       for($i=0; $i<count($arr); $i++) {
  6.               if($i==(count($arr)/2-1)) $res[] = $arr[$i]+$arr[$i+1];
  7.               else if($i!=(count($arr)/2)) $res[] = $arr[$i];
  8.               return $res;
  9.       }
  10.     }else return $arr;
  11. }
  12.  
  13. print_r(replace([7, 2, 2, 5, 10, 7]));
  14. echo "<br>";
  15. print_r(replace([-5,-5,10]));
  16. echo "<br>";
  17. print_r(replace([45, 23, 12, 33, 12, 453, -234, -45]));
  18. echo "<br>";
  19. print_r(replace([2,8]));
  20. echo "<br>";
  21. print_r(replace([-12, 34, 40, -5, -12, 4, 0, 0, -12]));
  22. echo "<br>";
  23. print_r(replace([9, 0, 15, 9]));
  24. echo "<br>";
  25. print_r(replace([-6, 6, -6]));
  26. echo "<br>";
  27. print_r(replace([26, 26, -17]));
  28. echo "<br>";
  29. print_r(replace([-7, 5, 5, 10]));
  30. echo "<br>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement