Wednesday, 25 May 2022

how to find the index backwards

How can I count the minimum steps to get to a specific index counting forward and backward?

The list is:

content = [1, 2, 3, 4]

If I start from index 0 and want to know how many steps to get to the number 4 iterating forwards I'll get 3, because:

#0 #1 #2 #3
[1, 2, 3, 4, 5]

But I also want to know it backward, like:

#0       #2 #1
[1, 2, 3, 4, 5]

In the example above the minimum amount of steps is 2

Another example

content = ["A", "B", "C", "D", "E"]
start_index = 4 # "E"
to_find = "D"

 #1   #2   #3   #4   #0
["A", "B", "C", "D", "E"]
# Moving forward I'll start from "A" again until reache "D"

                #1   #0
["A", "B", "C", "D", "E"] # Moving backwards...

In the example above the minimum amount of steps is 1



from how to find the index backwards

No comments:

Post a Comment