Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'algorithm': 0.04; 'from:addr:yahoo.co.uk': 0.04; 'element': 0.07; 'problem:': 0.07; '#print': 0.09; 'adopted': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'repeated': 0.09; 'tmp': 0.09; 'subject:Help': 0.11; 'language.': 0.14; '12]': 0.16; '55,': 0.16; '67,': 0.16; '[10]': 0.16; 'advance!': 0.16; 'blocks': 0.16; 'blocks:': 0.16; 'input:': 0.16; 'integers,': 0.16; 'integers.': 0.16; 'lambda': 0.16; 'prev': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'elements': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'practices,': 0.19; '>>>': 0.22; 'coding': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'skip:l 30': 0.24; 'subject:Code': 0.24; 'looks': 0.24; '---': 0.24; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'url:python': 0.33; 'skip:# 10': 0.33; 'but': 0.35; 'sequence': 0.36; 'next': 0.36; 'url:org': 0.36; 'skip:- 20': 0.37; 'thank': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'free': 0.61; 'url:3': 0.61; 'viruses': 0.61; 'protection': 0.63; 'our': 0.64; 'here': 0.66; 'url:0': 0.67; 'antivirus': 0.68; 'below:': 0.68; 'increasing': 0.74; '92,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Help me write better Code Date: Wed, 09 Jul 2014 16:44:01 +0100 References: <3a608dd2-d8bf-429e-af21-ce5ac8f18272@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-189-90.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <3a608dd2-d8bf-429e-af21-ce5ac8f18272@googlegroups.com> X-Antivirus: avast! (VPS 140709-0, 09/07/2014), Outbound message X-Antivirus-Status: Clean 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: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404920643 news.xs4all.nl 2845 [2001:888:2000:d::a6]:59635 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74260 On 09/07/2014 15:27, sssdevelop wrote: > Hello, > > I have working code - but looking for better/improved code. Better coding practices, better algorithm :) > > Problem: Given sequence of increasing integers, print blocks of consecutive integers. > > Example: > > Input: [10, 11, 12, 15] > Output: [10, 11, 12] > > Input: [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > Outout: [67, 68], [91, 92, 93, 94] > > My code looks as below: > ----------------------------- > #!/usr/bin/python > a = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > #a = [] > #a = [10] > #a = [10, 11, 12, 15] > print "Input: " > print a > > prev = 0 > blocks = [] > tmp = [] > last = 0 > for element in a: > if prev == 0: > prev = element > next > if element == prev + 1: > if tmp: > pass > else: > tmp.append(prev) > tmp.append(element) > else: > if tmp: > blocks.append(tmp) > tmp = [] > > prev = element > > if tmp: > blocks.append(tmp) > > if blocks: > #print "I have repeated elements and those are:" > for b in blocks: > print b > > ----------------------- > > thank you in advance! > Adopted from here https://docs.python.org/3.0/library/itertools.html data = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): group = list(map(operator.itemgetter(1), g)) if len(group) > 1: print(group) >>> [67, 68] [91, 92, 93, 94] >>> -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com