I wrote a program that outputs the longest sequence of equal numbers when I press 2 in the console, but for some reason it prints the numbers tied to each other (i.e if I input 1 2 2 2 4 5 it outputs 222 instead of 2 2 2) and for some reason it also prints None
The assertion tests that I made also fail for some reason
Input: press 1, input 1 2 2 2 2 4 5 6 1 4
Expected output: press 2, then it should show 2 2 2 2 2
Actual output: 22222 None
def lista_egale(lst1):
l = 1
prev_one = None
number = 0
lmax = -1
for current in lst1:
if prev_one == current:
l += 1
elif l > lmax:
lmax = l
number = prev_one
l = 1
prev_one = current
print(number * lmax)
def test_egale():
assert lista_egale([1, 2, 2, 2, 4, 5, 6]) == 2 2 2
assert lista_egale([4, 1, 2, 4, 1, 2, 3, 2]) == 1
assert lista_egale([1, 1, 1, 1, 1, 2, 2, 2, 2]) == 1 1 1 1 1
def show_menu():
print("lists")
print("1. Enter the list")
print("2. Check if the list has a max sequence of equal numbers and print it")
print("4. exit")
def ui_read_list():
input_list = input("Numbers go here ")
return input_list.split()
def run():
global lst1
lst1 = []
show_menu()
while True:
cmd = input(">>>")
if cmd == "4":
return
if cmd == "1":
lst1 = ui_read_list()
elif cmd == "2":
print(lista_egale(lst1))
else:
print("invalid command")
def main():
run()
test_egale()
#test_munte()
main()
As others have pointed out, it sounds like you're expecting lista_egale
to return a value rather than printing it. I think you also want to use the count
function of lists to find how many times each item is in the list like this:
def lista_egale(lst1):
lmax = -1
number = 0
for current in lst1:
if lst1.count(current) > lmax:
lmax = lst1.count(current)
number = current
return ' '.join([str(number)] * lmax)
Then to make your asserts work, the comparisons should also be strings like this:
def test_egale():
assert lista_egale([1, 2, 2, 2, 4, 5, 6]) == '2 2 2'
assert lista_egale([4, 1, 2, 4, 1, 2, 3, 2]) == '1'
assert lista_egale([1, 1, 1, 1, 1, 2, 2, 2, 2]) == '1 1 1 1 1'