MHB Is the list a circular shift of the other?

Click For 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. :)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
998
  • · Replies 4 ·
Replies
4
Views
2K
Replies
15
Views
3K
  • · Replies 43 ·
2
Replies
43
Views
4K
Replies
9
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K