Consider the following list:
>>> circle = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> list(enumerate(circle))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h')]
If circle is conceptualized as a circular list i.e. circle[0] is connected to circle[7], given a start index and an end index where start != end, I want to construct two lists that represent linear traversal orders in clockwise and counter-clockwise directions.
Depending on the values of start and end, here's what I have come up with:
Case 1: start < end
>>> circle = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> start, end = 1, 6
>>> clockwise = circle[start:end+1]
>>> clockwise
['b', 'c', 'd', 'e', 'f', 'g']
>>> counter_clockwise = circle[start::-1] + circle[:end-1:-1]
>>> counter_clockwise
['b', 'a', 'h', 'g']
Case 2: start > end
>>> circle = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> start, end = 6, 1
>>> clockwise = circle[start:] + circle[:end+1]
>>> clockwise
['g', 'h', 'a', 'b']
>>> counter_clockwise = circle[start:end-1:-1]
>>> counter_clockwise
['g', 'f', 'e', 'd', 'c', 'b']
Is there a pythonic/more-efficient/easier way of constructing these two lists?