how do you make a loop run in reverse?

Discussion in 'Python' started by rahulreddy24@hotmail.com, Mar 26, 2013.

  1. Guest

    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?
     
    , Mar 26, 2013
    #1
    1. Advertising

  2. Dave Angel Guest

    On 03/26/2013 07:59 PM, 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
     
    Dave Angel, Mar 27, 2013
    #2
    1. Advertising

  3. Mark Lawrence, Mar 27, 2013
    #3
  4. Xavier Ho Guest

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

    xav


    On 27 March 2013 10:59, <> 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
    >
     
    Xavier Ho, Mar 27, 2013
    #4
  5. > On 27 March 2013 10:59, <> 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 <> 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
     
    Oscar Benjamin, Mar 27, 2013
    #5
  6. On 26 March 2013 23:59, <> 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
     
    Arnaud Delobelle, Mar 27, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    2
    Views:
    370
    Gordon Beaton
    Sep 8, 2004
  2. dogbite
    Replies:
    4
    Views:
    729
    osmium
    Oct 10, 2003
  3. Alex Snast
    Replies:
    16
    Views:
    2,287
    Alex Snast
    Sep 21, 2008
  4. Fa Sidd
    Replies:
    12
    Views:
    232
    ara howard
    Jan 26, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    451
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page