Skip to content

Instantly share code, notes, and snippets.

@Golpha
Last active May 20, 2016 12:23
Show Gist options
  • Save Golpha/fed3e05d1738ed3dfce3 to your computer and use it in GitHub Desktop.
Save Golpha/fed3e05d1738ed3dfce3 to your computer and use it in GitHub Desktop.

Revisions

  1. @nhlm nhlm revised this gist Sep 8, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions test.php
    Original 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 = array();
    $content = [];
    $iterator = new PyramidIterator(range(0,9));

    foreach ( $iterator as $row ) {
    @@ -61,7 +63,7 @@ public function rewind()

    echo PHP_EOL.PHP_EOL;

    $content = array();
    $content = [];
    $iterator = new PyramidIterator(range('A', 'Z'));

    foreach ( $iterator as $row ) {
  2. @nhlm nhlm created this gist Sep 8, 2014.
    37 changes: 37 additions & 0 deletions result.txt
    Original 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
    71 changes: 71 additions & 0 deletions test.php
    Original 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);