how do you make a loop run in reverse?

R

rahulreddy24

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?
 
D

Dave Angel

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?

tes
 
X

Xavier Ho

There is a built-in function that reverses an iterable. Have a look at the
documentation.

xav
 
O

Oscar Benjamin

So i have a set of for loops that create this :
[snip]
but i want to nest all the loops under one BIG loop that'll run in reverse
to make this: [snip]
Is this possible?

There is a built-in function that reverses an iterable. Have a look at the
documentation.

I assume you mean the reversed function. It does not in general
reverse an iterable. From the docs:
"""
reversed(seq)

Return a reverse iterator. seq must be an object which has a
__reversed__() method or supports the sequence protocol (the __len__()
method and the __getitem__() method with integer arguments starting at
0).

New in version 2.4.

Changed in version 2.6: Added the possibility to write a custom
__reversed__() method.
"""

So it reverses a sequence (having the __len__ and __getitem__ methods)
or an object that advertises reversibility (having a __reversed__
method). It does not reverse anything else including generators,
iterators and most non-sequence iterables.

To the OP: To reverse a "set of for loops" as requested is not
possible using the reversed function. It is, however, possible to
reverse a list of strings. So if you have a function that returns the
list of strings you show as output then you can easily reverse that
list with reversed(mylist) or mylist[::-1].


Oscar
 
A

Arnaud Delobelle

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:
.... for i in range(1, 6):
.... print '*'*i
....*
**
***
****
*****

You can't reverse the pattern because it's not data so you need to
turn it into data:
.... 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:
.... for line in lines:
.... print line

Or if you want to be concise:
.... print "\n".join(lines)

So you can print any pattern:
*
**
***
****
*****

So now you can write another generator that makes the mirror pattern
of a given 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
....*
**
***
****
*****
****
***
**
*

Here's another example:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top