Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'example:': 0.03; 'data:': 0.09; 'function,': 0.09; 'function:': 0.09; 'lines.': 0.09; 'lines:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'iterable': 0.16; 'loops': 0.16; 'nest': 0.16; 'possible?': 0.16; 'subject:make': 0.16; 'subject:run': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'example.': 0.24; 'replace': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'mirror': 0.31; 'run': 0.32; 'another': 0.32; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'subject:?': 0.36; 'turn': 0.37; 'skip:p 20': 0.39; 'above,': 0.60; 'march': 0.61; 'simple': 0.61; 'making': 0.63; '****': 0.68; 'reverse': 0.68; 'statement,': 0.68; 'repeat': 0.74; 'subject:you': 0.87; 'imagine': 0.93; '***': 0.95; '2013': 0.98; 'to:addr:hotmail.com': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=ZwBBFBPMphXHRPOdI2jhwun/D3vE3xHEQxhaXkr+U2w=; b=WFYm2O6FNvwThjVwF/szLeXOKo03lqq2e99rS2EkUB3R6gS8keH7vyJxcqUZFjjTJ+ prUmznD6N29rpQyAMtRSVVBrsYYaFAnEPCZG9alyYLyy/gsUqCp6b89iHjoJteZXSlUz qfO7D8+54EuPORo7AkzDYndYLM320hxKobP/1Lr6jI/dUaKBsAT4JkagjCDqbxDenilM SRA5sqvHdCV4Pie/ODppZ++OK7hUyGIo6DXpeUX+sJQ2qw7kdp3pyGjOV23/3bt6U4XQ wvCarG/jHc3X4PiEPpu7KTQPnX4iznygbCn6KYUorDWUhgB1tW67679UoVBkT79Fxg0F ilJA== MIME-Version: 1.0 X-Received: by 10.112.3.198 with SMTP id e6mr1201354lbe.118.1364419114439; Wed, 27 Mar 2013 14:18:34 -0700 (PDT) In-Reply-To: References: Date: Wed, 27 Mar 2013 21:18:34 +0000 Subject: Re: how do you make a loop run in reverse? From: Arnaud Delobelle To: rahulreddy24@hotmail.com Content-Type: text/plain; charset=UTF-8 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 131 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364419117 news.xs4all.nl 6843 [2001:888:2000:d::a6]:57374 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42043 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