Rotating arbitrary sets of elements within a list

G

Guest

I'm trying to come up with a good algorithm to do the following:

Given a list 'A' to be operated on, and a list 'I' of indices into 'A',
rotate the i'th elements of 'A' left or right by one position.

Here's are some examples:

A = [a, b, c, d, e, f]
I = [0, 3, 4]

rotate(A, I, 'left') --> [b, c, d, e, f, a]
rotate(A, I, 'right') --> [b, a, c, f, d, e]

I = [1, 3, 4]

rotate(A, I, 'left') --> [b, a, d, e, c, f]
rotate(A, I, 'right') --> [a, c, b, f, d, e]

Any ideas?
 
W

wes weston

I'm trying to come up with a good algorithm to do the following:

Given a list 'A' to be operated on, and a list 'I' of indices into 'A',
rotate the i'th elements of 'A' left or right by one position.

Here's are some examples:

A = [a, b, c, d, e, f]
I = [0, 3, 4]

rotate(A, I, 'left') --> [b, c, d, e, f, a]
rotate(A, I, 'right') --> [b, a, c, f, d, e]

I = [1, 3, 4]

rotate(A, I, 'left') --> [b, a, d, e, c, f]
rotate(A, I, 'right') --> [a, c, b, f, d, e]

Any ideas?

phil,
Could you do a circlular buffer where the front
starts at 'a' (0) and rotate left increments front.
The i'th element is gotten as mod 6 (front+i) where
6 would be the length of the list.
wes
 
G

Guest

I'm trying to come up with a good algorithm to do the following:

Given a list 'A' to be operated on, and a list 'I' of indices into 'A',
rotate the i'th elements of 'A' left or right by one position.



Ok, here's what I've got. It seems to work right, but can it be
improved upon?

def list_rotate(A, I, direction=1):
A1 = [A for i in I]
A2 = [A for i in range(len(A)) if i not in I]
if direction:
I1 = [(i-1)%len(A) for i in I] # rotate left
else:
I1 = [(i+1)%len(A) for i in I] # rotate right
I2 = [i for i in range(len(A)) if i not in I1]
for i in range(len(I1)): A[I1] = A1
for i in range(len(I2)): A[I2] = A2
 
W

wes weston

I'm trying to come up with a good algorithm to do the following:

Given a list 'A' to be operated on, and a list 'I' of indices into 'A',
rotate the i'th elements of 'A' left or right by one position.

Here's are some examples:

A = [a, b, c, d, e, f]
I = [0, 3, 4]

rotate(A, I, 'left') --> [b, c, d, e, f, a]
rotate(A, I, 'right') --> [b, a, c, f, d, e]

I = [1, 3, 4]

rotate(A, I, 'left') --> [b, a, d, e, c, f]
rotate(A, I, 'right') --> [a, c, b, f, d, e]

Any ideas?


class CBuf:
def __init__(self,list=[]):
self.List = list
self.Front = 0
def ActualIndexGet(self,index):
return (self.Front + index) % len(self.List)
def ValueGet(self,index):
return self.List[self.ActualIndexGet(index)]
def Rotate(self,dir):
if dir == 'L':
self.Front += 1
else:
self.Front -= 1
self.Front = self.Front % len(self.List)
def Show(self):
i = self.Front
while 1:
print self.List," ",
i = (i+1) % len(self.List)
if i == self.Front:
break
print


b = CBuf(['a','b','c','d','e','f'])
b.Show()
b.Rotate('L')
b.Show()
b.Rotate('L')
b.Show()

a b c d e f
b c d e f a
c d e f a b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top