Home > database >  download the int input to array until is not smaller than previous one except first
download the int input to array until is not smaller than previous one except first

Time:03-16

download the int input to array until is not smaller than previous one except first, I'm trying this way.

So this would look like this:

4, 1, 2, 1 and now break

I have no clue what to do, I've tryed many ways like double for loop but no one of them work

def make_list(size: int):
    return [int(input("enter element of the array:\n")) for _ in range(0, size)]


if __name__ == '__main__':
    size = 5

CodePudding user response:

so not sure if I understood the question (not very good at english), but I tried a little something that seems to give the output you wanted. Not very clean at the moment, so might update later.

def make_list(size: int):
    if(size > 0):
        tab = []
        firstNb = get_input()
        tab.append(firstNb)
        if(size > 1):
            lastNb = get_input()
            tab.append(lastNb)
            for _ in range(0, size - 1):
                nb = get_input()
                tab.append(nb)
                if(nb < lastNb):
                    return tab
                lastNb = nb
        return tab
    else:
        return []

def get_input():
    return int(input("enter element of the array:\n"))

if __name__ == '__main__':
    size = 5
    result = make_list(size)
    print(result)

CodePudding user response:

I solved that, maybe not in pretty way ( somethign does not work with list comprehension)

def create_array(size: int) -> [int]:
    x = [(int(input("enter element of the array:\n")))]
    # return ["break" if x[i] < x[i - 1] else int(input("enter element of the array:\n")) for i in range(0, size)]  somethign wrong there
    for i in range(0, size):
        x.append(int(input("enter element of the array:\n")))
        if x[i   1 ] < x[i - 1]:
            return x
    return x
    
    if __name__ == '__main__':
        size = 5
        print(create_array(size))
  • Related