[ create a new paste ] login | about

Link: http://codepad.org/AGlUdT7A    [ raw code | output | fork ]

PHP, pasted on Jan 9:
<?php
function remove_element(/* array */ $a, /* element */ $e) {
  $b = array();
  foreach($a as $m) {
    if ($m != $e) array_push($b, $m);
  }
  return $b;
}

function is_sorted1(/* array */ $a) {
  for($i = 0; $i < count($a)-1; $i++) {
    if ( $a[$i] > $a[$i+1]) return 0;
  }
  return 1;
}

function is_sorted2(/* array */ $a) {
  $j = 0;
  for($i = 0; $i < count($a)-1; $i++) {
    if ( $a[$i] < $a[$i+1]) $j++;
  }
  return $j == count($a)-1 ? 1 : 0;
}

$before = array("11", "a", 2, 3);
$after = remove_element($before, "a");

var_dump($before);
print(is_sorted1($before));
print(is_sorted2($before));

print("\n");

var_dump($after);
print(is_sorted1($after));
print(is_sorted2($after));
?>


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array(4) {
  [0]=>
  string(2) "11"
  [1]=>
  string(1) "a"
  [2]=>
  int(2)
  [3]=>
  int(3)
}
11
array(3) {
  [0]=>
  string(2) "11"
  [1]=>
  int(2)
  [2]=>
  int(3)
}
00


Create a new paste based on this one


Comments: