I tried to print "longest string" in my list. I don't want to use max() or sort(). I just tried to print it without Method. I've succeeded in getting the shortest string, but I'm having trouble printing out the longest string. Their output is the same.
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
p=b_list[0]
for k in b_list:
if k<=p:
k=p
r=b_list.index(p)
print("Shortest string : " ,a_list[r])
break
This is the shortest string output and, of course, there is an item with a length of 3 other than 'abc' but I first entered break so that the list index only prints the smallest value.
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
p=b_list[0]
for k in b_list[1:]:
if k>p:
k=p
r=b_list.index(p)
print("longest string : " ,a_list[r])
break
This is the result of printing the one with the longest string.What's the problem? Once again, I want to solve it without using the min(),max(),sort() method.
max? Is this homework?