function lengthCompute($value, $n) { for ($i = 0; $i < $n; $i++) { $value = compute($value); } return strlen($value); }
function compute(&$writers, &$target, $min, $max, $prev = []) { $bestResult = INF; for ($i = $min; $i <= $max; $i++) { $new = array_merge($prev, [$i]); $result = compute($writers, $target, $i + 1, $max, $new); if ($result < $bestResult) { $bestResult = $result; } $result = matchWriters($writers, $target, $new); if ($result < $bestResult) { $bestResult = $result; } } return $bestResult; }
$max = 100; $average = NULL; global $min, $max; // allowed, but serve no purpose function compute($p) { global $min, $max; global $average; $average = ($max + $min) / 2; if ($p) { global $result; $result = 3.456; // initializes a global, creating it if necessary } } compute(TRUE); echo "\$average = {$average}\n"; echo "\$result = {$result}\n"; //var_dump($GLOBALS); echo "---------------- Global Variables using \$GLOBALS -------------------\n"; $GLOBALS['done'] = FALSE; var_dump($done); $GLOBALS['min'] = 10; $GLOBALS['max'] = 100; $GLOBALS['average'] = NULL; global $min, $max; // allowed, but serve no purpose function compute2($p) { $GLOBALS['average'] = ($GLOBALS['max'] + $GLOBALS['min']) / 2; if ($p) {
<?php # Problem: Old Magician # Language: PHP # Author: KirarinSnow # Usage: php thisfile.php <input.in >output.out function compute() { fscanf(STDIN, "%d %d\n", $w, $b); if ($b % 2 == 1) { return "BLACK"; } else { return "WHITE"; } } fscanf(STDIN, "%d\n", $cases); for ($i = 1; $i <= $cases; $i++) { $o = compute(); echo "Case #{$i}: {$o}\n"; }
* 12 - Highly divisible triangular number **/ function num_of_divisors($n) { // Too lazy to find better formula, so I borrowed some basic formula for calculating number of divisors from mathblog.dk, will revised it later (hope so). $nod = 0; $sqrt = sqrt($n); for ($i = 1; $i <= $sqrt; $i++) { if ($n % $i == 0) { $nod += 2; } } if ($sqrt * $sqrt == $n) { $nod--; } return $nod; } function compute($step) { // Implement basic triangular number formula. $n = 1; $start = 2; while ($start <= $step) { $tn = $n * ($n + 1) / 2; $start = num_of_divisors($tn); $n++; } echo "First triangle number to have over five hundred divisors: {$tn}\n"; } compute(500);
$connection = $timetable[$lastConnectionIndex]; $route[] = $connection; $lastConnectionIndex = $inConnection[$connection[DEPARTURE_STATION]]; } date_default_timezone_set('Europe/London') . PHP_EOL; echo "Timezone: " . date_default_timezone_get() . PHP_EOL; foreach (array_reverse($route) as $row) { printf("%s %s %s %s %s\n", $row[DEPARTURE_STATION], $row[ARRIVAL_STATION], date("H:i", $row[DEPARTURE_TIME]), date("H:i", $row[ARRIVAL_TIME]), $row[TUID]); } echo "\n"; } $fh = fopen('./tmp_data', 'r'); $timetable = readTimetable($fh); while (($line = fgets($fh, 128)) !== false) { if ($line !== "\n") { $l = explode(' ', $line); $departureStation = $l[0]; $arrivalStation = $l[1]; $departureTime = $l[2]; if (!array_key_exists($arrivalStation, $allStationsAsKeys)) { echo "Station {$arrivalStation} not exist in timetables!" . PHP_EOL; die; } if (!array_key_exists($departureStation, $allStationsAsKeys)) { echo "Station {$departureStation} not exist in timetables!" . PHP_EOL; die; } // list($departureStation, $arrivalStation, $departureTime) = array_map('intval', ); compute($timetable, $departureStation, $arrivalStation, $departureTime); } }
<?php require_once '../de/appwerk/radio/test/RootDBUtilsTest.php'; function compute($file) { $com = 'mysql' . ' --user='******' --password='******' rrradio < ' . $file; print $file . PHP_EOL; $ary = array(); $res = exec($com, $ary); } array_shift($argv); while ($argv) { compute(array_shift($argv)); } exit(1);
} array_push($places_arr, $temp); } } } //print_r($places_arr); $str = $str . ');(._;>;);out meta;'; fwrite($myfile, $str); } $result = upload_check($str, $top, $left, $right, $bottom); $top = $_POST["top"]; $left = $_POST["left"]; $right = $_POST["right"]; $bottom = $_POST["bottom"]; if ($result == "success") { $tem = compute($top, $bottom, $left, $right, $distance, $places_arr, $tables, $tables_val); } /////////////////////////////////////////////////////////////////////////////////////////// log_time("last"); //////////////////////////////////////////////////////////////////////////////////////////////// print_r(json_encode(array("result" => $result, "points" => $tem, "top" => $top, "bottom" => $bottom, "left" => $left, "right" => $right))); } } } } else { ?>
function accum(&$arr, $value) { $arr[compute($value)] = $value; }
echo "Result of calling doit using square closure = {$res}\n-------\n"; echo "----------------- using a use clause, #1 ----------------------\n"; function compute(array $values) { $count = 0; $callback = function () use(&$count) { echo "Inside method >>" . __METHOD__ . "<<\n"; // called {closure} ++$count; }; $callback(); echo "\$count = {$count}\n"; $callback(); echo "\$count = {$count}\n"; } compute([1, 2, 3]); echo "----------------- using a use clause, #2 (instance method) ----------------------\n"; class D { private function f() { echo "Inside method >>" . __METHOD__ . "<<\n"; } public function compute(array $values) { $count = 0; $callback = function ($p1, $p2) use(&$count, $values) { echo "Inside method >>" . __METHOD__ . "<<\n"; // called D::{closure} ++$count; $this->f();
<?php $test = false; $file = $test ? "test.txt" : "input"; $input = explode("\n", file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . $file)); $values = []; echo "Part 1: " . compute(["a" => 0, "b" => 0], $input)["b"] . "\n"; echo "Part 2: " . compute(["a" => 1, "b" => 0], $input)["b"] . "\n"; function jump($val, &$input) { if ($val > 0) { for ($i = 1; $i < (int) $val; $i++) { next($input); } } else { for ($i = 1; $i > (int) $val; $i--) { prev($input); } } } function compute($registers, $input) { while ($row = current($input)) { #echo "$row\n"; $parts = explode(" ", $row); $cmd = array_shift($parts); $reg = str_replace(",", "", array_shift($parts)); $val = $parts ? (int) array_shift($parts) : false; switch ($cmd) { case "hlf": $registers[$reg] /= 2;