Is the list a circular shift of the other?

In summary, the conversation discusses how to check if one list is a circular shift of another list using Python. The speaker suggests using a cyclical shift approach and the other speaker suggests checking for the same number of elements and comparing all elements through modular arithmetic.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
mathmari said:
Hey! :eek:

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)
 
  • #3
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. :)
 

FAQ: Is the list a circular shift of the other?

What is a circular shift in a list?

A circular shift in a list is when the elements of the list are shifted one position to the left or right, and the last element is moved to the front or the first element is moved to the end. This creates a circular pattern in the list.

How can you tell if one list is a circular shift of the other?

To determine if one list is a circular shift of the other, you can compare the elements of the lists in a circular manner. If the elements of one list can be rearranged to match the elements of the other list, then it is a circular shift.

What is the difference between a circular shift and a regular shift in a list?

A regular shift in a list simply moves the elements one position to the left or right, without any changes to the order of the elements. A circular shift, on the other hand, takes the last element and moves it to the front or the first element and moves it to the end, creating a circular pattern in the list.

Can a list be a circular shift of itself?

Yes, a list can be a circular shift of itself if it is shifted by the number of elements in the list. This essentially brings the list back to its original order, creating a circular shift.

How can circular shifts be useful in data analysis?

Circular shifts can be useful in data analysis for detecting patterns and finding relationships between data points. By comparing circular shifts of different lists, we can identify similarities and differences in the data, which can provide insights into the underlying patterns and relationships.

Similar threads

Replies
2
Views
834
Replies
4
Views
1K
Replies
43
Views
4K
Replies
29
Views
4K
Replies
3
Views
722
Replies
1
Views
1K
Back
Top