how do you make a loop run in reverse?

Arnaud Delobelle arnodel at gmail.com
Wed Mar 27 17:18:34 EDT 2013


On 26 March 2013 23:59,  <rahulreddy24 at hotmail.com> wrote:
> So i have a set of for loops that create this :
>
> ***************************************
> ***   ***   ***   ***   ***   ***   ***
> ***   ***   ***   ***   ***   ***   ***
>    ***   ***   ***   ***   ***   ***   ***
>    ***   ***   ***   ***   ***   ***   ***
> ***************************************
>
>                  *
>                 ***
>                *****
>               *******
>              *********
>
> but i want to nest all the loops under one BIG loop that'll run in reverse to make this:
>
> ***************************************
> ***   ***   ***   ***   ***   ***   ***
> ***   ***   ***   ***   ***   ***   ***
>    ***   ***   ***   ***   ***   ***   ***
>    ***   ***   ***   ***   ***   ***   ***
> ***************************************
>
>                  *
>                 ***
>                *****
>               *******
>              *********
>               *******
>                *****
>                 ***
>                  *
> ***************************************
>    ***   ***   ***   ***   ***   ***   ***
>    ***   ***   ***   ***   ***   ***   ***
> ***   ***   ***   ***   ***   ***   ***
> ***   ***   ***   ***   ***   ***   ***
> ***************************************
>
> Is this possible?

Let's have a look at a simple example.  Imagine you have a function:

>>> def print_pretty_pattern():
...    for i in range(1, 6):
...       print '*'*i
...
>>> print_pretty_pattern()
*
**
***
****
*****

You can't reverse the pattern because it's not data so you need to
turn it into data:

>>> def generate_pretty_pattern():
...     for i in range(1, 6):
...         yield '*'*i

It's the same as above, but the 'print' statement has been replace
with a 'yield' statement, making the function into a generator
function, so that when you call it you get an iterable of all the
lines.  You can now make a function to print a pattern:

>>> def print_pattern(lines):
...     for line in lines:
...         print line

Or if you want to be concise:

>>> def print_pattern(lines):
...     print "\n".join(lines)

So you can print any pattern:

>>> print_pattern(generate_pretty_pattern())
*
**
***
****
*****

So now you can write another generator that makes the mirror pattern
of a given pattern:

>>> def mirror_pattern(pattern):
...     lines = []
...     for line in pattern:
...         yield line
...         lines.append(line)
...     if lines:
...         lines.pop() # don't repeat the last line
...         for line in reversed(lines):
...             yield line
...
>>> print_pattern(mirror_pattern(generate_pretty_pattern()))
*
**
***
****
*****
****
***
**
*

Here's another example:

>>> print_pattern(mirror_pattern(''.join(mirror_pattern("*".ljust(i).rjust(15))) for i in range(1,16,2)))
              *
            *   *
          *       *
        *           *
      *               *
    *                   *
  *                       *
*                           *
  *                       *
    *                   *
      *               *
        *           *
          *       *
            *   *
              *

--
Arnaud



More information about the Python-list mailing list