interesting exercise

castironpi at gmail.com castironpi at gmail.com
Tue May 8 01:57:23 EDT 2007


On May 8, 12:24 am, a... at mac.com (Alex Martelli) wrote:
> <castiro... at gmail.com> wrote:
>
>    ...
>
> > def p(a,b):
> >       if list( a ) != sorted( list( a ) ): raise ValueError, "String not
> > ordered."
> >       if not b: return ['']
> >       return [i+j for i in list(a) for j in p(a,b-1)]
>
> No need for 2/3 of the list(...) calls.  sorted(a) and sorted(list(a))
> will ALWAYS be the same sequence; "for i in a" and "for i in list(a)"
> will always iterate on the same sequence [as long as you're not changing
> a inside the iteration, which, in this case, you aren't].
>
> Alex

Ah quite true.  list( a ) != sorted( a ).  ...for i in a for...




More information about the Python-list mailing list