language analysis to enforce code standards

Aahz aahz at pythoncraft.com
Fri Jul 10 11:23:29 EDT 2009


In article <mailman.2948.1247229186.8015.python-list at python.org>,
Jean-Michel Pichavant  <jeanmichel at sequans.com> wrote:
>
>You could also verify there are at least N different characters used in 
>the sentence:
>
>N = 5 # must contains at least 5 different characters
>record = []
>for c in s:
>    if c not in record:
>        record += [c]
>if len(record) >= N:
>    print "at least %s different characters" % N

Much simpler and *way* more efficient with a set:

if len(set(s)) < N:
    print "Must have at least %s different characters" % N
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha



More information about the Python-list mailing list