PHPã§ã¯é åã§ã¯ãªããªãã¸ã§ã¯ãã«ç¶æ ãæããã
ã¢ããã³ãã«ã¬ã³ãã¼ãæ¸ãããã³ã¡ã³ãã«é¢ç½ã課é¡ãããã¾ããã
ãPython - ãããç°¡åãªã¢ã«ã´ãªãºã ãphpã§æ¸ããªãã¦ã¤ãããã®ãã¤ã
php ã® array ã¨åç §ã®é¢ä¿ãã¯ã½ã§ç¡ããªãã http://qiita.com/methane/items/41e1376c41d8c15e8894 ãããæ®éã«æ¸ãã¦ã¿ã¦ãã ããã
é¢ç½ãããããã¾ãããã
æè¿ããã¶ãPHPæåå¤ãã§ãããå®ã¯Pythonã好ç©ãªã®ã§ãã¯ãã¯ã§ãã
ã¨ãã£ã¦ãããã®ã¨ã³ããªã¼ã¯ãphp ã® array ã¨åç §ã®é¢ä¿ãã¯ã½ã§ç¡ããã¨ã¯è¨ã£ã¦ãªãã¦ããããéã«ããã¯è ã£ã¦ãããé¿ããããªãã¸ã§ã¯ãã§å²ãã§ãããã¨ãã話ã ã£ãã®ã§...(^^ ãã®ã¾ã¾ãã£ã¦ãPythonã®æ§è½ã«ã¯ãªããªãã¨ããã£ã¦ããã®ã§ãé åãç´æ¥ä½¿ãã®ã¯ã¤ã¤ã§ãããªã®ã§ããªãã¸ã§ã¯ããç¶æ ãæã¤ããã«æ¸ãã¨ãã主張ãã©ããããã¨ãªã®ããå®è£ ã§è¡¨ãã¦ã¿ããã¨æãã¾ãã
ãã5.5 ã® yield 使ã£ã¦ã¾ãããã£ãã PHP ã«ã Python ã®ãããã§ããã®ã§ã yield ãªãã¦ãºã«ãã¨æã人ã¯ããããã¤ãã¬ã¼ã¿ã¼ã¯ã©ã¹ãå®è£ ãããããã¨æãã¾ããã¤ãã¬ã¼ã¿ã¼ã®å®è£ ãé¢åãªäººã¯ Ginq ã¨ãæ¤è¨ãã¦ãããããã§ãã
<?php /** * æ«å°¾ããæ¶åã§ããã·ã¼ã±ã³ã¹ */ class TailConsumableSequence { protected $sequence; function __construct($source) { $this->sequence = $source; } /** * Pythonã®popã¨åããããªãã® * æ«å°¾ãããããã¶ãããã®ã¼ã£ã¦æãåºã */ function consume($fluctuation=0) { $size = count($this->sequence); $targetPos = $size - 1 - $fluctuation; $consumedValue = $this->sequence[$targetPos]; for ($i = $targetPos; $i < $size - 1; ++$i) { $this->sequence[$i] = $this->sequence[$i + 1]; } array_pop($this->sequence); return $consumedValue; } /** * n以ä¸æ®ã£ã¦ããã */ function remainsEqualsOrMoreThan($n) { return count($this->sequence) >= $n; } /** * æ®ãæ°ãlimitãã©ã¡ããå°ããæ¹ */ function sizeOr($limit) { $size = count($this->sequence); return min($limit, $size); } } /** * æ°åã®å¾ãã®ã»ããããã«ããã«ãæ¾ã£ã¦ãã¦ãã¢ãä½ã * ãã«ããã«ã = ææ«å°¾ã¨ãããã limit ã¾ã§ã®ç¯å²ããã®ã¼ã£ãä½ç½®ã®ã㢠*/ function allRandomPeairsFrom($sequence, $limit=100) { $tcs = new TailConsumableSequence($sequence); while ($tcs->remainsEqualsOrMoreThan(2)) { $a = $tcs->consume(); // ææ«å°¾ããåã $b = $tcs->consume( mt_rand(0, $tcs->sizeOr($limit) - 1) ); // ææ«å°¾ããããç¨åº¦ããã®ã¼ã£ã¦åã yield [$a, $b]; } } function test($n, $r) { foreach (allRandomPeairsFrom(range(0, $n-1), $r) as list($a, $b)) { //printf("%d %d\n", $a, $b); assert($a - $b < $r * 2); } } test($argv[1], 10);
ããã§ãããã§ããããã追è¨3ã®ãã¿æ¸ãã¨åããããªç¹æ§ãªã®ã«ãããã¨èªããã³ã¼ãã§ãããã¾ãã
$ time php matching2.php 10000; time php matching2.php 20000 php matching2.php 10000 0.23s user 0.01s system 99% cpu 0.245 total php matching2.php 20000 0.40s user 0.02s system 98% cpu 0.421 total
追è¨2 㧠list_pop() ã«ãã¦ããå¦çããã¿æ¸ããããããã£ã¨äºæ³éãã®æ§è½ãåºãããã«ãªã£ãã php ã§é åã®åç §ã«å¯¾ãã¦æä½ãã¦ã¯ãããªããã¤ã¾ããé åãæä½ããã¢ã«ã´ãªãºã ãé¢æ°ã«ãã¦ã¯ãããªãã
ãããåæãé¢æ°ã«ãã¦ã¯ãããªããé åã®åç §ã«å¯¾ãã¦æä½ãã¦ã¯ãããªããçã§é åãããããæ¸ãæããã¨ãã¤ã³ã¿ã使ããªãããã«ãªãã¡ã¯ã¿ã§ããªããªã£ã¦ãããããããã©ãããã...
PHPã§ç¶æ ãæã¤ãããã¡ã¯ããªãã¸ã§ã¯ãã§å²ãã¨ããã§ããåç §æ¸¡ãã¨ãé åã®ã³ãã¼ã¨ãããæ¥ãããããããã解æ¾ããããã¿æ¸ãããã®ã¨åããããªåãã«ãªãã¾ãããªãã¸ã§ã¯ãã¯ãã¤ã³ã¿ãªã®ã§ã好ããªã ãé¢æ°ã«æ¸¡ãã¦ãããã¦ããã£ã¨ããã®ã¯ããªãã¸ã§ã¯ãã®ãã£ã¼ã«ãã¯ã¹ã³ã¼ããè¡ãæ¥ãããã¼ã«ã«å¤æ°ããããã£ã¨å®å®ããã¡ã¢ãªã«ç½®ãã¦ããã£ã¦ãã¨ã§ãã
ãã®ãç¶æ ã®å¤åã¯ããããæå³ããã¡ã½ãããæã¤ãªãã¸ã§ã¯ãã§ã®ã¿èµ·ããã¹ãã§ãããã¨ããã®ã¯ããä½æ³ã®è©±ã§ããã¨ããå´é¢ããããã©ãä»ã«ãããæ¸ããã»ããåç §æ¸¡ããããåé¡ãç°¡åã«ãªã£ã¦ãããã°ããªãã¦ãããã©ã¼ãã³ã¹ãå®å®ãããã¨ããæå³ã§ããããã§ãã
ã¾ãããã©ãã¦éåã«è¦ããæ¹æ³ã®ã»ããå®ã¯ç´ ç´ãã¨ããã®ãæè¿ã®PHPã®é¢ç½ãã¨ããã§ããOOPã®ãä½æ³ããããªãã®ã¨ãç©ççã«æªããã®ããé ãããã®ãåæ¯ä¾ãããªãæãã
PHPã®arrayã¯Pythonã§ããã¨ããã®ååä»ãã¿ãã«ã¨ãã¦ä½¿ãããããããã¡ã°ããã使ãæ¹ã ã¨æã£ã¦ãã¾ãã