Fw: Undeliverable Message

Bart Kastermans bkasterm at gmail.com
Wed Jan 30 17:01:25 EST 2008


On Jan 25, 5:05 am, Matthew_WAR... at bnpparibas.com wrote:
> Hallo pyPeople,
>
> I wrote a little snippet of code that takes a list representing some
> 'digits', and according to a list of symbols, increments the digits through
> the symbol list.
>
> so for example,
>
> digits=["a","a","a"]
> symbols=["a","b","c"]
>
> increment(digits,symbols) repeatedly would return digits as
>
> aab
> aac
> aba
> abb
> abc
> aca
>
> etc..
>
> Heres the code
>
> def increment(digits,symbols):
>         overflow=True
>         digitpos=-1
>         while overflow and -digitpos<=len(digits):
>                 digitsymbolindex=symbols.index(digits[digitpos])
>                 if digitsymbolindex==len(symbols)-1:
>                         overflow=True
>                         digits[digitpos]=symbols[0]
>                         digitpos=digitpos-1
>                 else:
>                         digits[digitpos]=symbols[digitsymbolindex+1]
>                         overflow=False
>         return digits
>
> Now, this works. All good. It's nice and simple.  I'm just wondering how
> anyone else might approach it?

I (not an expert at all) have only minor comments and one question:
comments:
why keep setting overflow to True, if you do not touch it will not
change.
digitpos -= 1 is easier to read in  my mind
question:
Why first extract the indices and then compare (in your if statement),
and
why do you not just compare the symbols?

Best,
Bart



More information about the Python-list mailing list