如何找到序列中最长的相邻整数值

时间:2019-11-17 21:54:17

标签: python python-3.x

对于这个问题,我想获得序列中相邻整数的最长间隔。我觉得我已经接近答案了,但不确定如何解决。有人可以帮我吗?

输入:[1, 2, 5, 5, 5, 6, 6, 6, 8, 8]

预期输出:12(555)66688

我的输出:12(555)6(66)8

我的代码:

L = [1, 2, 5, 5, 5, 6, 6, 6, 8, 8]
print(L)
inRun = False
last = None
max_value = 0
count = 0
for i in range(len(L) - 1):
    if inRun:
        if L[i] != L[i - 1]:
            if count > max_value:
                max_value = count
                last = i
            print(')', end='')
            inRun = False
            count = 0
        else:
            count += 1
    else:
        if L[i] == L[i + 1]:
            count += 1
            print('(', end='')
            inRun = True
    print(L[i], end='')
if inRun:
    print(')'

1 个答案:

答案 0 :(得分:1)

编辑

使用过的集合的简化代码。另外,我在序列中添加了打印元素的第一个和最后一个索引。

from collections import Counter

L = [1, 2, 5, 5, 5, 6, 6, 6, 8, 8, 8]
print(L)

cnt = Counter(L)
value, max_value = cnt.most_common(1)[0]

firstIndex = L.index(value)

before_seq = ''.join(str(n) for n in L[:firstIndex])
seq = ''.join(str(n) for n in L[firstIndex : firstIndex + max_value])
after_seq = ''.join(str(n) for n in L[firstIndex + max_value:])

print("{}({}){}".format(before_seq, seq, after_seq))

print("first index: {}\nlast index: {}".format(firstIndex, firstIndex + max_value - 1))

上一个答案

我为您准备了两种解决方案,因为我不知道您所说的“最长运行时间”到底是什么意思,而且我也不知道您的代码中哪里有问题。在第一个代码中,您有一个输出:12(555)(666)88。在第二个中,您有:12(555)66688。希望我能帮到我:)

第一:输出12(555)66688

L = [1, 2, 5, 5, 5, 6, 6, 6, 8, 8]
print(L)

max_value = 0
count = 1

for i in range(len(L)):
    for j in range(i + 1, len(L)):
        if L[i] == L[j]:
            count = count + 1
        else:
            if max_value < count:
                max_value = count

            count = 1
            break

    if max_value < count:
        max_value = count
        count = 1

index = 0
count = 1

for i in range(len(L)):
    for j in range(index + 1, len(L)):
        if L[index] == L[j]:
            count = count + 1
        else:
            break

    if count == max_value:
        print('(', end='')
        for k in range(index, index + max_value):
            print(L[k], end='')
        print(')', end='')
        index = index + max_value
        count = 1
        max_value = max_value + 1
    else:
        for k in range(index, index + count):
            print(L[k], end='')
        index = index + count
        count = 1

    if index >= len(L):
        break

第二个输出12(555)(666)88

L = [1, 2, 5, 5, 5, 6, 6, 6, 8, 8]
print(L)

max_value = 0
count = 1

for i in range(len(L)):
    for j in range(i + 1, len(L)):
        if L[i] == L[j]:
            count = count + 1
        else:
            if max_value < count:
                max_value = count

            count = 1
            break

    if max_value < count:
        max_value = count
        count = 1

index = 0
count = 1

for i in range(len(L)):
    for j in range(index + 1, len(L)):
        if L[index] == L[j]:
            count = count + 1
        else:
            break

    if count == max_value:
        print('(', end='')
        for k in range(index, index + max_value):
            print(L[k], end='')
        print(')', end='')
        index = index + max_value
        count = 1
    else:
        for k in range(index, index + count):
            print(L[k], end='')
        index = index + count
        count = 1

    if index >= len(L):
        break