Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $arr = array_map('floatval', explode(" ", readline()));
- while(1) {
- $command = readline();
- if($command == "end") {
- break;
- }
- $comm = explode(" ", $command);
- if($command == "Get sum") {
- echo array_sum($arr) . PHP_EOL;
- }
- switch($comm[0]) {
- case 'Contains':
- if(in_array($comm[1], $arr)) {
- echo "Yes" . PHP_EOL;
- } else {
- echo "No such number" . PHP_EOL;
- }
- break;
- case 'Print':
- $num = $comm[1];
- if($num == "even") {
- printEven($arr);
- } else if($num == "odd") {
- printOdd($arr);
- }
- break;
- case "Filter":
- $condition = $comm[1];
- $num = $comm[2];
- Filter($arr, $condition, $num);
- break;
- }
- }
- echo implode(" ", $arr);
- function Filter($arr = array(), $condition, $num) {
- $length = count($arr);
- $temp = array();
- for($i = 0; $i < $length; $i++) {
- if($condition == "<") {
- if($arr[$i] < $num) {
- $temp[] = $arr[$i];
- }
- } else if($condition == ">") {
- if($arr[$i] > $num) {
- $temp[] = $arr[$i];
- }
- } else if($condition == ">=") {
- if($arr[$i] >= $num) {
- $temp[] = $arr[$i];
- }
- } else if($condition == "<=") {
- if($arr[$i] <= $num) {
- $temp[] = $arr[$i];
- }
- }
- }
- echo implode(" ", $temp) . PHP_EOL;
- }
- function printOdd($arr = array()) {
- $temp = array();
- for($i = 0; $i < count($arr); $i++) {
- if($arr[$i] % 2 != 0) {
- $temp[] = $arr[$i];
- }
- }
- echo implode(" ", $temp) . PHP_EOL;
- }
- function printEven($arr = array()) {
- $temp = array();
- for($i = 0; $i < count($arr); $i++) {
- if($arr[$i] % 2 == 0) {
- $temp[] = $arr[$i];
- }
- }
- echo implode(" ", $temp) . PHP_EOL;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement