-3

How to find length of the longest run of monotonically increasing numbers in a given list ?

For example, if L = [10, 4, 6, 8, 3, 4, 5, 7, 7, 2] then function implementation should return the value 5 because the longest run of monotonically increasing integers in L is [3, 4, 5, 7, 7].

Please suggest me the solution.

gtlambert
  • 11,711
  • 2
  • 30
  • 48
Giri lekkala
  • 57
  • 2
  • 7

5 Answers5

3

Keep a count which tracks monotonically increasing number and a 'maxCont' to track the highest count

def mCount(L):
    count=1

    maxCount=0
    for i in range(len(L)-1):
        if L[i+1] >= L[i]:
            count +=1
        else:

            count =1
        if maxCount<count:
            maxCount=count
    return maxCount

Demo:

print mCount([10, 4, 6, 8, 3, 4, 5, 7, 7, 2])

Output:

5
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
1
def longest_run(L):
    """
    Assumes L is a list of integers containing at least 2 elements.
    Finds the longest run of numbers in L, where the longest run can
    either be monotonically increasing or monotonically decreasing. 
    In case of a tie for the longest run, choose the longest run 
    that occurs first.
    Does not modify the list.
    Returns the sum of the longest run. 
    """
    firstone = mIncreasing(L)
    secondone = mDecreasing(L)
    result = 0
    if len(firstone) > len(secondone) :
        for i in firstone:
            result += i
    elif len(firstone) == len(secondone) :
        for i in L:
            if sum(firstone) == sum(secondone):
                for r in firstone:
                    result += r 
                break
            if i in firstone and not i in secondone:
                for j in firstone:
                    result += j
                break
            elif i in secondone and not i in firstone:
                for k in secondone:
                    result += k
                break

    elif len(firstone) < len(secondone):
        for i in secondone:
            result += i
    return result

#finde the longest run of monotonically increasing numbers in L

def mIncreasing(L):
    current_set = L[:]
    temp_set = [current_set[0]]
    m_increasing = []

    for i in range(len(current_set)-1):
        if current_set[i] <= current_set[i+1]:
            temp_set.append(current_set[i+1])
            if len(temp_set) > len(m_increasing):
                m_increasing = temp_set[:]
        elif current_set[i] > current_set[i+1]:
            temp_set = [current_set[i+1]]
    return m_increasing


#finde the longest run of monotonically decreasing numbers in L
def mDecreasing(L):
    current_set = L[:]
    temp_set = [current_set[0]]
    m_decreasing = []
    for i in range(len(current_set)-1):
        if current_set[i] >= current_set[i+1]:
            temp_set.append(current_set[i+1])
            if len(temp_set) > len(m_decreasing):
                m_decreasing = temp_set[:]
        elif current_set[i] < current_set[i+1]:
            temp_set = [current_set[i+1]]
    return m_decreasing
Hayder Ctee
  • 145
  • 9
0
def longestRun(L):
    answer = 0
    start = 0
    end = 0
    while start<len(L) and end<len(L):
        if L[end] >= L[start]:
            end += 1
        else:
            if end - start + 1 > answer:
                answer = end - start
                start = end
    return answer
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

def longestRun(L):

def longestRun(L):

count=1

maxCount=1
for i in range(len(L)-1):
    if L[i+1] >= L[i]:
        count +=1
    else:

        count =1
    if maxCount<count:
        maxCount=count
return maxCount
0
def newIndexIsLongerLength(oldStartIndex, oldEndIndex, newStartIndex, newEndIndex):
    return (newEndIndex-newStartIndex) > (oldEndIndex-oldStartIndex)

def getMonotonicallyI(L):
    startIndex = 0
    endIndex = 0

    for tmpStartIndex in range(len(L)-1):
        tmpEndIndex = tmpStartIndex

        while tmpEndIndex < len(L)-1:
            if L[tmpEndIndex] > L[tmpEndIndex+1]:
                break
            tmpEndIndex += 1

        if newIndexIsLongerLength(startIndex, endIndex, tmpStartIndex, tmpEndIndex):
            startIndex, endIndex = tmpStartIndex, tmpEndIndex

    return startIndex, endIndex


def getMonotonicallyD(L):
    startIndex = len(L)-1
    endIndex = len(L)-1

    for tmpStartIndex in range(len(L)-1, 0, -1):
        tmpEndIndex = tmpStartIndex

        while tmpEndIndex > 0:
            if L[tmpEndIndex] > L[tmpEndIndex-1]:
                break
            tmpEndIndex -= 1

        if newIndexIsLongerLength(endIndex, startIndex, tmpEndIndex, tmpStartIndex):
            startIndex, endIndex = tmpStartIndex, tmpEndIndex

    #print(endIndex, startIndex)
    return endIndex, startIndex


def longest_run(L):
    """
    Assumes L is a list of integers containing at least 2 elements.
    Finds the longest run of numbers in L, where the longest run can
    either be monotonically increasing or monotonically decreasing. 
    In case of a tie for the longest run, choose the longest run 
    that occurs first.
    Does not modify the list.
    Returns the sum of the longest run. 
    """

    # Starting Values Of Index


    startIndexI, endIndexI = getMonotonicallyI(L)
    startIndexD, endIndexD = getMonotonicallyD(L)

    if newIndexIsLongerLength(startIndexI, endIndexI, startIndexD, endIndexD):
        return sum(L[startIndexD:endIndexD+1])
    return sum(L[startIndexI:endIndexI+1])