首页 > 解决方案 > Calculating Distance Between Two Items in a List

问题描述

I am trying to Calculate the Distance or Length between two items in a list

Question: Find the length of the longest subsequence in which the value 6 does not occur. (This number can be zero, if only 6 were in the list.)

For example, in the trial 66423612345654 the longest subsequence which the value 6 does not occur is 12345. and such the distance or length we can say is 5

Note: i have tried using trackers and for loops but i guess i am having trouble with the break positioning maybe

Throws=[6,6,2,5,3,6,6,3,6,6,3,6,6,6]
Max=0
Min=0

for i in range (0,len(Throws)):
    Tracker1 = Throws[i]
    if Tracker1==6:
        for k in range (i+1,len(Throws)):
            Tracker2 = Throws[k]
            if Tracker2 ==6 and Throws[k-1]!=6:
                Min = k-i
                if Min>Max:
                    Max=Min
                    break
print(Max)

The Max value for the given code should be 3 ,Thanks in advance

标签: pythonpython-3.x

解决方案


Easy to understand:

THROWS = [6, 6, 2, 5, 3, 6, 6, 3, 6, 6, 3, 6, 6, 6]
CONTROL_NUM = 6

max = 0
count = 0

for current in THROWS:
    if current != CONTROL_NUM:
        count += 1
        if count > max:
            max = count
    else:
        count = 0


print(max)

推荐阅读