MHB Is the list a circular shift of the other?

AI Thread Summary
The discussion centers on creating a Python program to determine if one list is a circular shift of another. The initial approach of comparing sorted versions of the lists is identified as flawed, as it fails with examples where the lists contain the same elements but are not circular shifts of each other. A suggested solution involves cyclically shifting one list and comparing it to the other, ensuring both lists have the same length before proceeding. Additionally, checking if the first element of one list exists in the other is recommended as a preliminary step. The use of modular arithmetic to navigate through the lists is also proposed for matching elements effectively.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

I want to write a progarmm in python that reads two lists A, B and checks if the one of the lists is a circular shift of the other list. The result is either True or False.

I thought to do something like that:

Code:
if sorted(A) == sorted(B):
  C = True
else:
  C = False

But it cannot be correct because if we consider the lists A=[1, 2, 3, 4, 5] and B=[2, 3, 4, 1, 5], the sorted lists are the same, but B is not a circular shift of A. Right? (Wondering)

Could you give me a hint? (Wondering)
 
Technology news on Phys.org
mathmari said:
Hey! :o

I want to write a progarmm in python that reads two lists A, B and checks if the one of the lists is a circular shift of the other list. The result is either True or False.

I thought to do something like that:

Code:
if sorted(A) == sorted(B):
  C = True
else:
  C = False

But it cannot be correct because if we consider the lists A=[1, 2, 3, 4, 5] and B=[2, 3, 4, 1, 5], the sorted lists are the same, but B is not a circular shift of A. Right? (Wondering)

Could you give me a hint? (Wondering)

Hey mathmari! (Smile)

How about shifting list A cyclically, and comparing it to B?
And repeat until we've done all possible cyclical shifts of list A? (Wondering)
 
I would probably go about it in a bit clunkier fashion.

I would first determine if both lists had the same number of elements. If not, then the test fails.

If no fail, then I would see if the first element in list A is in list B...if not, then the test fails.

If no fail, then I would cycle through the two lists using modular arithmetic to determine the correct subscripts for the second list, and if all elements match, then success. :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
952
Replies
4
Views
2K
Replies
43
Views
4K
Replies
29
Views
4K
Replies
1
Views
1K
Replies
4
Views
1K
Back
Top