function add($clusters, $grid, $lefti, $righti) { $this->left = $clusters[$lefti]; $this->right = pop($clusters, $righti); # merge columns grid[row][righti] and row grid[righti] into corresponding lefti for ($i = 0; $i < count($grid); $i++) { for ($j = 0; $j < count($grid); $j++) { $grid[$i][$lefti] = min($grid[$i][$lefti], $grid[$i][$righti]); } pop($grid[$i], $righti); } $r = pop($grid, $righti); for ($i = 0; $i < count($grid); $i++) { $grid[$lefti][$i] = min($grid[$lefti][$i], $r[$i]); } return array($clusters, $grid); }
public function buildHash() { $hash = ""; if ($this->getTotalPointsSpent() === 0) { return $hash; } $hash .= $this->treeHashToken . base36_encode($this->json["id"]); $aaHash = collateAdjacentDuplicates(array_map(array($this, "createAAHash"), $this->json["aa"])); if (strpos(end($aaHash), "0")) { $aaHash . pop(); } foreach ($aaHash as &$value) { $repeatedChar = $value[0]; $numRepeats = strlen($value); if ($numRepeats >= 3) { $value = $repeatedChar . $this->repeatHashToken . base36_encode($numRepeats); } } $hash .= implode($aaHash); return $hash; }
function parent() { $s = $this->cell; $k = _drw($s)->eq(0); $a =& $this->cell_store->O; if (!isDrw($k)) { $k = $s; } $m = $this->cell_store->search($k); foreach ($m[0] as $v) { if ($v[0] == $k) { $l = $v[1]; break; } } $w = explode('/', $l); pop($w, 2); for ($i = 0; $i < count($w) - 1; $i++) { ($h = OBJ($a, false)) && ($a =& $h[$w[$i]]); } $this->cell = _drw(OBJ($a, false))->eq(0); return $this; }
function pop($n = 1) { return pop($this->O, $n); }
function push($arr, $value) { array_push($arr, $value); return $arr; } function pop($arr) { array_pop($arr); return $arr; } $fh = fopen($argv[1], "r"); while (!feof($fh)) { $heap = array(); $phrase = ""; $test = trim(fgets($fh)); if ($test != "") { $arr = explode(" ", $test); for ($i = 0; $i < count($arr); $i++) { $heap = push($heap, $arr[$i]); } $ct = count($heap); while ($ct > 0) { $phrase .= $heap[count($heap) - 1] . " "; $heap = pop($heap); $heap = pop($heap); $ct = $ct - 2; } echo trim($phrase) . "\n"; } } fclose($fh);
<?php /** * Created by PhpStorm. * User: just * Date: 24.11.15 * Time: 09:27 */ $stack = []; function pop() { global $stack; return array_pop($stack); } function push($entry) { global $stack; return array_push($stack, $entry); } foreach (['clubs', 'diamonds', 'hearts', 'spades'] as $color) { foreach (array_merge(range(2, 10), ['J', 'D', 'K', 'A']) as $value) { push(['Color' => $color, 'Value' => $value]); } } shuffle($stack); $cardToGive = 12; while ($cardToGive -= 1) { pop(); } $trump = pop(); print_r($trump);
/** * function runAstar * * traverse the grid useing the A* algorithm * * @param Grid $grid will hold the map of the world we are searching * @param int $start_row the row of the start node * @param int $start_col the col of the start node * @param int $goal_row the row of the goal node * @param int $goal_col the col of the goal node * @param int $max_row the last row index * @param int $max_col the last col index * @return bool true if path exsit, false otherwise */ function runAstar($grid, $start_row, $start_col, $goal_row, $goal_col, $max_row, $max_col) { //initialize the open list $open_list = array(); //initialize the closed list $close_list = array(); $b_end_search = false; //if true we must end the search $goal_node =& $grid->nodes[$goal_row][$goal_col]; $start_node =& $grid->nodes[$start_row][$start_col]; $start_node->f = 0; $open_list[] =& $start_node; sortNodes($open_list, 'ASCE'); //while the open list is not empty while (!empty($open_list)) { $q = null; //current_node //find the node with the least f on the open list, call it "q" //pop q off the open list sortNodes($open_list, 'ASCE'); pop($open_list, 0, $q); //generate q's 8 successors and set their parents to q $successors = findSuccessors($grid->nodes, $q->row, $q->col, $max_row, $max_col); foreach ($successors as $key => $successor) { $temp = clone $successor; $temp->parent_node =& $q; //if the successor is an obstacle if (isObstacle($temp)) { continue; // skip this successor if it is an obstacle } if (isGoal($temp, $goal_node)) { $successor->parent_node = $temp->parent_node; $b_end_search = true; break; } $temp->g = $q->g + 1; // $temp->h = sqrt(pow($goal_node->row - $temp->row, 2) + pow($goal_node->col - $temp->col, 2)); $temp->f = $temp->g + $temp->h; // // $successor->g = $q->g + 1; // // $successor->h = sqrt(pow(($goal_node->row - $successor->row), 2) + pow(($goal_node->col - $successor->col), 2)); // $successor->f = $successor->g + $successor->h; //does exist in open list if (doesExist($successor, $open_list) && $successor->f < $temp->f) { continue; } //does exist in close list if (doesExist($successor, $close_list) && $successor->f < $temp->f) { continue; } // $successor->parent_node = $temp->parent_node; $successor->g = $temp->g; $successor->h = $temp->h; $successor->f = $temp->f; $open_list[] =& $successors[$key]; //&$successor; } $close_list[] =& $grid->nodes[$q->row][$q->col]; //&$q; if ($b_end_search == true) { break; } } return $b_end_search ? true : false; //return true if there is a path, false otherwise }
/** * Join conjunctions to surrounding pieces. * * - Mr. and Mrs. * - King of the Hill * - Jack & Jill * - Velasquez y Garcia * * Returns new array of pieces with the conjunctions merged into one piece * with spaces between. * * $additional_parts_count is used for when the comma format contains other parts, * so we know how many there are to decide if things should be considered a * conjunction. * * @access private * @param array $pieces * @param int $additional_parts_count * @return array */ private function join_on_conjunctions($pieces, $additional_parts_count = 0) { if (count($pieces) + $additional_parts_count < 3) { return $pieces; } foreach (array_values(array_filter(array_reverse($pieces), array($this, 'is_conjunction'))) as $conj) { $rootname_pieces = array_values(array_filter($pieces, array($this, 'is_rootname'))); $length = count($rootname_pieces) + $additional_parts_count; if (strlen($conj) == 1 && $length < 4) { continue; } if (!($i = array_search($conj, $pieces))) { continue; } if ($i < count($pieces) - 1) { if ($i == 0) { $nxt = $pieces[$i + 1]; $new_piece = implode(' ', slice($pieces, 0, 2)); if ($this->is_title($nxt)) { $this->C['titles'][] = lc($new_piece); } else { $this->C['conjunctions'][] = lc($new_piece); } $pieces[$i] = $new_piece; $pieces = pop($pieces, $i + 1); continue; } if ($this->is_conjunction($pieces[$i - 1])) { $new_piece = implode(' ', slice($pieces, $i, $i + 2)); $this->C['conjunctions'][] = lc($new_piece); $pieces[$i] = $new_piece; $pieces = pop($pieces, $i + 1); continue; } $new_piece = implode(' ', slice($pieces, $i - 1, $i + 2)); if ($this->is_title($pieces[$i - 1])) { $this->C['titles'][] = lc($new_piece); } $pieces[$i - 1] = $new_piece; $pieces = pop($pieces, $i); $pieces = pop($pieces, $i); } } if ($prefixes = array_values(array_filter($pieces, array($this, 'is_prefix')))) { $i = array_search($prefixes[0], $pieces); if ($next_suffix = array_values(array_filter(slice($pieces, $i), array($this, 'is_suffix')))) { $j = array_search($next_suffix[0], $pieces); $new_piece = implode(' ', slice($pieces, $i, $j)); $pieces = array_merge(slice($pieces, 0, $i), array($new_piece), slice($pieces, $j)); } else { $new_piece = implode(' ', slice($pieces, $i)); $pieces = array_merge(slice($pieces, 0, $i), array($new_piece)); } } return $pieces; }
* 每次从数组索引第一个元素开始 */ function pop($a) { $count = count($a); for ($i = $count - 1; $i > 0; $i--) { for ($j = 0; $j < $i; $j++) { if ($a[$j] < $a[$j + 1]) { swap($a, $j, $j + 1); } } } return $a; } $data = array('2', '5', '4', '9', '0'); $aa = pop($data); var_dump($aa); exit; /* * The best simple exchange sort * Starting from the first posistion of an array, * It is to determine a location value with traversal all array element every time. */ //var_dump(exchange($data)); function exchange($arr) { $count = count($arr); for ($i = 0; $i < $count; $i++) { for ($j = $i + 1; $j < $count; $j++) { if ($arr[$i] > $arr[$j]) { swap($arr, $i, $j);