Adding a line
min_rr_sequence.append(count) as done in code below corrects the flaw in the provided code:
import numpy as np
resp_rates = [1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1,1,1,1,1]
rr_min_current = np.amin(resp_rates)
print(rr_min_current)
count = 0
first = 1
min_rr_sequence = []
print(resp_rates)
for resp_rate in resp_rates:
if resp_rate == rr_min_current and first == 1:
count=count+1
first = 0
elif resp_rate == rr_min_current and first == 0:
count=count+1
elif resp_rate != rr_min_current and first == 0:
min_rr_sequence.append(count)
first = 1
count = 0
min_rr_sequence.append(count)
The problem was that the provided code didn't run .append(count) in case the longest counted sequence occurs at the very end of the resp_rates list.
As pointed out by Thierry Lathuille in his comment to the question the flaw in the code is that "the last elif in which you append the count of the sequence won't be reached."
Worth to notice fact is, that the by Thierry Lathuille provided solution using itertools.groupby(), min() and len() is almost 10 times faster as other of the provided (status: Nov. 18, 2018, 17:30 MEZ) approaches to this task.
Check it out for yourself on your system. Here the code:
import time
# resp_rates = [1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1,1,1,1,1]
# resp_rates = [1,1,1,1,1,1,1,1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1,1,1,1,1]
# resp_rates = [1,1,1,1,1,1,1,1,1,1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1,1,1,1,1]
# resp_rates = [1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1]
# resp_rates = [5,1,1,2,89,56,4,1,1,1,1,10,5,67,1,1,1,76,5,7,6,6,6,1,1,1,1,1,1,1]
# resp_rates = [1,3,1,1,1,2,1,1,4,1,1,1,1]
resp_rates = [7,7,7,7,7,7,7,8,8,8,8,8,8,8,8]
# -----------------------------------------------------------------------------
print("# Original code with one line added to make it print correct result:")
import numpy as np
largeList = 20000000*[5]
resp_rates = resp_rates + largeList
timeNOW = time.time()
rr_min_current = np.amin(resp_rates)
# print(rr_min_current)
count = 0
first = 1
min_rr_sequence = []
# print(resp_rates)
for resp_rate in resp_rates:
if resp_rate == rr_min_current and first == 1:
count=count+1
first = 0
elif resp_rate == rr_min_current and first == 0:
count=count+1
elif resp_rate != rr_min_current and first == 0:
min_rr_sequence.append(count)
first = 1
count = 0
min_rr_sequence.append(count)
#longest_min_rr_sequence.append(np.amax(min_rr_sequence))
# print(min_rr_sequence)
longest_min_rr_sequence = np.amax(min_rr_sequence)
print("Result of timing", time.time() - timeNOW )
print(longest_min_rr_sequence)
# -----------------------------------------------------------------------------
print("# Provided solution using itertools.groupby(), min() and len(): ")
timeNOW = time.time()
from itertools import groupby
out = -min((value, -len(list(group))) for value, group in groupby(resp_rates))[1]
print("Result of timing", time.time() - timeNOW )
print(out)
# 7
# -----------------------------------------------------------------------------
print("# Provided solution using itertools.groupby() and operator.itemgetter(): ")
timeNOW = time.time()
import itertools, operator
r = max((list(y) for (x,y) in itertools.groupby((enumerate(resp_rates)),operator.itemgetter(1)) if x == min(resp_rates)), key=len)
print("Result of timing", time.time() - timeNOW )
print(len(r))
print("# Accepted answer: ")
timeNOW = time.time()
lowest = resp_rates[0]
count = 0
max_count = 0
for i in resp_rates:
if i < lowest:
lowest = i
count = 1
elif i == lowest:
count = count + 1
else:
if count != 0:
max_count = count
count = 0
if count != 0:
max_count = count
print("Result of timing", time.time() - timeNOW )
print(max_count)
print("# Accepted answer (EDITED): ")
timeNOW = time.time()
lowest = resp_rates[0]
count = 0
max_count = 0
for i in resp_rates:
if i < lowest:
lowest = i
count = 1
max_count = count
elif i == lowest:
count = count + 1
if count > max_count:
max_count = count
else:
count = 0
print("Result of timing", time.time() - timeNOW )
print(max_count)
Here the printout on my system:
python3.6 -u "longestSequence-ofMinimalNumber_Cg.py"
# Original code with one line added to make it print correct result:
Result of timing 10.655358791351318
20000000
# Provided solution using itertools.groupby(), min() and len():
Result of timing 0.3956716060638428
20000000
# Provided solution using itertools.groupby() and operator.itemgetter():
Result of timing 4.829622983932495
20000000
# Accepted answer:
Result of timing 3.7705492973327637
20000000
# Accepted answer (EDITED):
Result of timing 5.475024223327637
20000000
elifin which you append the count of the sequence won't be reached.