-
-
Save Golpha/fed3e05d1738ed3dfce3 to your computer and use it in GitHub Desktop.
Revisions
-
nhlm revised this gist
Sep 8, 2014 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ <?php version_compare(PHP_VERSION, '5.4.0', '>=') or die('Update your PHP Version, your version is no longer supported by php.net'); class PyramidIterator implements Iterator { private $range; @@ -50,7 +52,7 @@ public function rewind() header('Content-Type: text/plain; charset=utf-8'); $content = []; $iterator = new PyramidIterator(range(0,9)); foreach ( $iterator as $row ) { @@ -61,7 +63,7 @@ public function rewind() echo PHP_EOL.PHP_EOL; $content = []; $iterator = new PyramidIterator(range('A', 'Z')); foreach ( $iterator as $row ) { -
nhlm created this gist
Sep 8, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ 0 101 21012 3210123 432101234 54321012345 6543210123456 765432101234567 87654321012345678 9876543210123456789 A BAB CBABC DCBABCD EDCBABCDE FEDCBABCDEF GFEDCBABCDEFG HGFEDCBABCDEFGH IHGFEDCBABCDEFGHI JIHGFEDCBABCDEFGHIJ KJIHGFEDCBABCDEFGHIJK LKJIHGFEDCBABCDEFGHIJKL MLKJIHGFEDCBABCDEFGHIJKLM NMLKJIHGFEDCBABCDEFGHIJKLMN ONMLKJIHGFEDCBABCDEFGHIJKLMNO PONMLKJIHGFEDCBABCDEFGHIJKLMNOP QPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQ RQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQR SRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRS TSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRST UTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTU VUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUV WVUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUVW XWVUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUVWX YXWVUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUVWXY ZYXWVUTSRQPONMLKJIHGFEDCBABCDEFGHIJKLMNOPQRSTUVWXYZ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ <?php class PyramidIterator implements Iterator { private $range; private $row; public function __construct(array $range) { $this->range = $range; $this->rewind(); } public function getIndentLength() { return count($this->range) - 1 - $this->row; } public function key() { return $this->row; } public function current() { $left = array_reverse(array_slice($this->range, 0 , $this->row + 1)); $right = $this->key() ? array_slice($this->range, 1, $this->row) : []; return join($left).join($right); } public function next() { $this->row++; } public function valid() { return count($this->range) > $this->row; } public function rewind() { $this->row = 0; } } header('Content-Type: text/plain; charset=utf-8'); $content = array(); $iterator = new PyramidIterator(range(0,9)); foreach ( $iterator as $row ) { $content[] = str_repeat(' ', $iterator->getIndentLength()).$row; } echo join(PHP_EOL, $content); echo PHP_EOL.PHP_EOL; $content = array(); $iterator = new PyramidIterator(range('A', 'Z')); foreach ( $iterator as $row ) { $content[] = str_repeat(' ', $iterator->getIndentLength()).$row; } echo join(PHP_EOL, $content);