[Tutor] transposing [n, [e],[s[te]],[d]] sequences

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 16 Feb 2002 15:41:19 -0800 (PST)


On Sat, 16 Feb 2002, kevin parks wrote:

> # Here is the type of thing i can write pretty swiftly now:
> 
> def Xpose(seq,n, oct=12, sort_flag=0):
>     ''' take a sequence and tranpose it modulo some number
>     '''
>     mylist=[]
>     for i in seq:
>         x = (i+n)%oct
>         mylist.append(x) # to append or extend?
>     if sort_flag:
>         mylist.sort() # to sort or not sort
>     return mylist

Very nice.  Yes, this looks great!


> # Problem: how do i make something like this work for nested
> sequences? since sometimes we will have lists of lists or (god help
> us) list of *gasp* tuples...


What would be an example of how we'd want Xpose() to work on nested
structures?  Should this new Xpose() try to preserve the structure of the
list, like this:

###
### imaginary session #1
###
>>> x = [0, [2, 4], [5, [7], 9], 11]
>>> c = Xpose(x, 4, 12, 0)
>>> c
[4, [6, 8], [9, [11], 1], 3]
###


Or should this new Xpose() disregard the nested structure altogether, and
work on a flatten version of the list?

###
### imaginary session #2
###
>>> x = [0, [2, 4], [5, [7], 9], 11]
>>> c = Xpose(x, 4, 12, 0)
>>> c
[4, 6, 8, 9, 11, 1, 3]
###

Or perhaps something else?  Just want to make sure we're on the same
wavelength before working on this problem... *grin*


Talk to you later!