For this question, I want to get the longest run of adjacent integers in a sequence. I feel like I'm close to the answer, but not sure how to fix it. Can anyone please help me out?
Input: [1, 2, 5, 5, 5, 6, 6, 6, 8, 8]
Expected Output: 12(555)66688
My output: 12(555)6(66)8
My code:
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(')'
EDIT
Simplified code with used collections. Also, I added printing first and the last index of elements in the sequence.
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))
Previous answer
I prepared two solutions for you because I don't know what exactly you meant saying "longest run" and also I have no idea where is a problem in your code. In the first code, you have an output: 12(555)(666)88. In the second you have: 12(555)66688. I hope I helped:)
First: Output 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
Second Output 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.