MHB Is the list a circular shift of the other?

Click For Summary
SUMMARY

This discussion focuses on determining whether one list is a circular shift of another in Python. The initial approach using sorted lists is flawed, as it fails to account for the order of elements. A correct method involves checking if both lists have the same length and then using modular arithmetic to compare elements after cyclically shifting the first list. The proposed solution emphasizes the importance of element order in circular shifts.

PREREQUISITES
  • Understanding of Python programming
  • Familiarity with list data structures in Python
  • Knowledge of modular arithmetic
  • Basic algorithm design principles
NEXT STEPS
  • Implement a function to cyclically shift a list in Python
  • Explore Python's built-in functions for list manipulation
  • Learn about time complexity analysis for algorithms
  • Study the concept of circular queues and their applications
USEFUL FOR

Python developers, algorithm enthusiasts, and anyone interested in list manipulation and data structure optimization.

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. :)
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · 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