简体   繁体   English

确定是否可以通过从数组中删除不超过一个元素来获得严格递增的序列

[英]determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array

def almostIncreasingSequence(sequence):
    if sorted(sequence)==sequence:
        return True
    else:
        seq1=sequence[:]
        flag=0
        for i in range(len(seq1)):
            sequence.pop(i)
            num=check_sorted(sequence)
            if num==1:
                flag=1
                break
            else:
                sequence=seq1[:]
        if flag==1:
            return True
        else:
            return False



def check_sorted(sequence):
    check=0
    for i in range(len(sequence)-1):
        if sequence[i]<sequence[i+1]:
            check=1
        else:
            check=0
    #if sorted(sequence)==sequence:
        #return 1
    #else:
        #return 0
    return check

Can u please tell wt wrong I m dng here?你能说错我在这里吗?

Input:
sequence: [1, 2, 1, 2]
Output:
true
Expected Output:
false
def strictly_increasing_sequence(sequence):
    N = len(sequence)
    for i in range(N - 1):
        if (sequence[i] >= sequence[i+1]):
            return False
    return True

def check_sequence(sequence):
    if strictly_increasing_sequence(sequence):
        print("The sequence was strictly increasing from the beginning")
        return True
    else :
        N = len(sequence)
        for i in range(N):
            subsequence = sequence[:i]+ (sequence[i+1:] if i < N - 1 else [])
            if strictly_increasing_sequence(subsequence):
                print("By pop item {} it works".format(i))
                return True
    return False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获得数组元素的严格递增序列 - obtain a strictly increasing sequence of the elements of array 得到一个严格递增的序列 python - Obtain a strictly increasing sequence python 如何检查是否有可能通过删除python中的元素之一来获得递增列表? - How to check whether it is possible to obtain an increasing list by deleting one of its element in python? 创建一个严格递增的序列减去 1 个元素 - Creating a strictly increasing sequence minus 1 element ValueError:具有多个元素的数组的真值不明确。 使用a.any()或a.all()从列表中删除元素 - ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() FOR removing an element from a list 确定字符串中是否有多个单词 - Determine whether there’s more than one word in a string 确定输入列表是否严格增加 - Determine if the input list is strictly increasing 如何获得最短的随机序列,以便它编码从一个元素到另一个元素的所有可能转换? - How do I obtain the shortest random sequence such that it encodes all possible transitions from one element to another? 具有多个元素的数组的真值是不明确的。 按索引从列表中删除时使用a.any()或a.all() - The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() When removing from list by index 检查字符串是否包含Python中数组中的多个元素 - Check if string contains more than one element from array in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM