[Tutor] First real script

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 6 23:33:20 CET 2006


> Do you know a better way to do this?
>
>            if i < A_Len-1:
>
>                a = A_List[i-1]
>                b = A_List[i]
>                c = A_List[i+1]
>
>            else:
>
>                a = A_List[i-1]
>                b = A_List[i]
>                c = A_List[0]

Hi Carlos,

Here's one way to handle it.  If you're familiar with modulo "clock" 
arithmetic, you might want to apply it above.

Here's an example:

########################################
>>> for i in [1, 2, 3, 11, 12, 13, 14]:
...   print i, i % 12
...
1 1
2 2
3 3
11 11
12 0
13 1
14 2
########################################

The idea is that the second columns results are the result of doing:

     i % 12

and that number "rolls" around twelve.  Your index can "roll" around 
A_Len.


There are other alternative approaches to handle the boundary case.  We 
can talk about them if you'd like.


Good luck!


More information about the Tutor mailing list