Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how do you make a loop run in reverse?

Reply
Thread Tools

how do you make a loop run in reverse?

 
 
rahulreddy24@hotmail.com
Guest
Posts: n/a
 
      03-26-2013
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?
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      03-27-2013
On 03/26/2013 07:59 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
>


tes

--
DaveA
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      03-27-2013
On 26/03/2013 23:59, (E-Mail Removed) wrote:
> So i have a set of for loops that create this :
>


snipped the art

>
> but i want to nest all the loops under one BIG loop that'll run in reverse to make this:
>


snipped the art

>
> Is this possible?
>


Yes read http://docs.python.org/3/library/stdtypes.html#range and
http://docs.python.org/3/library/fun....html#reversed

--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

 
Reply With Quote
 
Xavier Ho
Guest
Posts: n/a
 
      03-27-2013
There is a built-in function that reverses an iterable. Have a look at the
documentation.

xav


On 27 March 2013 10:59, <(E-Mail Removed)> 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?
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      03-27-2013
> On 27 March 2013 10:59, <(E-Mail Removed)> wrote:
>>
>> 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?


On 27 March 2013 00:19, Xavier Ho <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      03-27-2013
On 26 March 2013 23:59, <(E-Mail Removed)> 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_patte rn()))

*
**
***
****
*****
****
***
**
*

Here's another example:

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

*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*

--
Arnaud
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
make pls don't work , i help you make web ,help you do business onthe internet xiaobb03@gmail.com Digital Photography 0 03-13-2008 11:28 AM
Scripts run using load in "for" loop run out of order Fa Sidd Ruby 12 01-26-2008 12:06 AM
why the following code cannot run out of the while loop?...thank you mike Java 2 09-08-2004 06:38 AM



Advertisments