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. :)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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