Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74329

Re: Help me write better Code

Newsgroups comp.lang.python
Date 2014-07-10 11:38 -0700
References <3a608dd2-d8bf-429e-af21-ce5ac8f18272@googlegroups.com> <mailman.11695.1404920643.18130.python-list@python.org>
Message-ID <ae431310-df72-41f5-9aee-43ca7df87707@googlegroups.com> (permalink)
Subject Re: Help me write better Code
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


On Wednesday, July 9, 2014 9:14:01 PM UTC+5:30, Mark Lawrence wrote:
> 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]

> -- 


Mark's version without the print.

Which says in a different way, Terry's point-2 :

> 2. Separate interface code that gets input and presents output from the
> function that processes the increasing sequence. The function should not
> care whether the ints come from a user, file, or calculation.


$ python3
Python 3.4.1 (default, Jun  9 2014, 10:28:44) 
[GCC 4.8.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from operator import itemgetter
>>> from itertools import groupby
>>> data = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] 
>>> [group  for k,g in groupby(enumerate(data), lambda t:t[0]-t[1])
...         for group in [list(map(itemgetter(1), g))]
...         if len(group) > 1
... 
... ]
[[67, 68], [91, 92, 93, 94]]
>>> 

You can replace the outermost '[]' with '()'
 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help me write better Code sssdevelop <sssdevelop@gmail.com> - 2014-07-09 07:27 -0700
  Re: Help me write better Code Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-09 16:44 +0100
    Re: Help me write better Code sssdevelop <sssdevelop@gmail.com> - 2014-07-10 07:39 -0700
      Re: Help me write better Code Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-10 15:51 +0100
    Re: Help me write better Code Rustom Mody <rustompmody@gmail.com> - 2014-07-10 11:38 -0700
  Re: Help me write better Code Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-09 12:16 -0600
    Re: Help me write better Code sssdevelop <sssdevelop@gmail.com> - 2014-07-10 07:38 -0700
  Re: Help me write better Code Terry Reedy <tjreedy@udel.edu> - 2014-07-09 14:46 -0400
    Re: Help me write better Code sssdevelop <sssdevelop@gmail.com> - 2014-07-10 07:38 -0700

csiph-web