Home > front end >  Returning max sequence from a list
Returning max sequence from a list

Time:10-13

I am trying to write a program with a console UI where a user can input a sequence and if they press 2 or 3 they can find the longest equal integer sequence and the longest "mountain" sequence respectively

everything goes smoothly when I press 1, I can enter the list, but when I press 2 (I haven't implemented 3 yet) nothing happens

Example Input : after pressing 1, 1 2 2 2 2 4, then press 2

Expected output: 4

Actual output: nothing, the program doesn't work

(I hope the title is on topic)

def lista_egale(lst1):
    l = 0
    prev_one = None
    lmax = -1
    for current in lst1:
        if prev_one == current:
            l  = 1
        elif l > lmax:
            lmax = l
        l = 0
        prev_one = current
        print(lmax)
def afiseaza_meniu():
    print("Aplicatie cu liste")
    print("1. Introduceti lista")
    print("2. Verificati daca lista are o secventa maxima cu numere egale si afisati-o")
    print("3. Verificati daca lista este de tip munte si afisati-o")
    print("4. exit")


def ui_citire_lista(lst1):
     input_string = input("Introduceti numerele ")
     lst1 = input_string.split()

def ui_afisare_munte():
    pass

def run():
    global lst1
    lst1 = []
    afiseaza_meniu()
    while True:
        cmd = input(">>>")
        if cmd == "4":
            return
        if cmd == "1":
            ui_citire_lista(lst1)
        elif cmd == "2":
            lista_egale(lst1)
        elif cmd == "3":
            ui_afisare_munte()
        else:
            print("comanda invalida")

def main():
    run()
    #test_egale()
    #test_munte()

if __name__ == "__main__":
    main()

CodePudding user response:

Your code doesn't work as your ui_citire_lista only assigns lst1 locally. You should rather return the value and catch it in run (where it is global)

change to ui_citire_lista:

def ui_citire_lista():
     input_string = input("Introduceti numerele ")
     return input_string.split()

change to run:

        if cmd == "1":
            lst1 = ui_citire_lista()

NB. there are other issues, but I'll let you discover them by yourself ;) don't hesitate to post again if you have trouble

CodePudding user response:

Here is a version of returning input, but you have to correct your lista_eagle function.

def lista_egale(lst1):
    l = 0
    prev_one = None
    lmax = -1
    for current in lst1:
        if prev_one == current:
            l  = 1
        elif l > lmax:
            lmax = l
        l = 0
        prev_one = current
        # print(lmax)
    return lmax
def afiseaza_meniu():
    print("Aplicatie cu liste")
    print("1. Introduceti lista")
    print("2. Verificati daca lista are o secventa maxima cu numere egale si afisati-o")
    print("3. Verificati daca lista este de tip munte si afisati-o")
    print("4. exit")


def ui_citire_lista(lst1):
     input_string = input("Introduceti numerele ")
     lst1 = input_string.split()

def ui_afisare_munte():
    pass

def run():
    global lst1
    lst1 = []
    afiseaza_meniu()
    result = None
    while True:
        cmd = input(">>>")
        if cmd in ['1','2','3','4']:
            lst1.append(cmd)
        else:
            print("comanda invalida")
        if cmd == "4":
            print('longest sequence is', result)
            return result
        elif cmd == "1":
            ui_citire_lista(lst1)
        elif cmd == "2":
            result = lista_egale(lst1)
        elif cmd == "3":
            ui_afisare_munte()
        
def main():
    run()
    #test_egale()
    #test_munte()

if __name__ == "__main__":
    main()

  • Related