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()
CodePudding user response:
you run
print(lista_egale(lst1))
The return value of your function
lista_egale(lst1)
is None
, which gets printed. Probably you want to run simply:
lista_egale(lst1)
CodePudding user response:
This worked for me except for the second assertion which (to me) doesn't make sense at all. Why would you want [1] to be returned and not anything else? Anyway, it returns [4] in that case.
def lista_egale(lst1):
l = 0
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
return ([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()
EDIT:
Your mistakes were:
l
was initialized as 1 even though it should've been initialized to 0 (because if the longest "streak" is 1-long, nothing would reach thel>lmax
.- In the
lista_eagle
function you didn't return anything and instead printed the result. - You were printing
number * lmax
which is a number instead of the streak itself which you can get by concatenating the array to itiself lmax times (as shown here atreturn [number] * lmax
).
CodePudding user response:
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'
CodePudding user response:
In lista_egale()
's print staement do this:
print((str(number) " ")*lmax)
This will print 2 2 2 rather than 222