0

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.

1
  • 1
    why not use max? Is this homework? Commented Apr 29, 2020 at 19:06

6 Answers 6

1

You can fin the longest and the shortest strings in the list the following way:

a_list = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']

# we give the both variable the value of the first element in the list 
shortest = a_list[0]  
longest = a_list[0]  

for i in a_list[1:]:
    if len(shortest) >= len(i):  # check if current string in shorter 
        shortest = i # if yes change variable value

    if len(longest) <= len(i):  # check if current string in longer
        longest = i # if yes change variable value

# print results:
print(shortest)
print(longest)
Sign up to request clarification or add additional context in comments.

Comments

1

You're assigning to k - the iteration variable, not p the maximum.

Also the indentaion needs to be fixed slightly:

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:
        p = k
r=b_list.index(p)
print("longest string : " ,a_list[r])

Output:

longest string :  bcdefg

P.S: Your shortest string code suffers from the same issues. It just happens to work with that particular input

Comments

0
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
    a=len(i)
    b_list.append(a)

max_len_element  =a_list[0]
max = b_list[0]

for i in range(len(b_list)):
    if b_list[i] > max:
        max = b_list[i]
        max_len_element = a_list[i]
        print(max, max_len_element)

max_len_element is your max length element

Comments

0

This is a very convoluted way to do it. @rdas already told you about p=k instead of k=p. You need to only loop once through your list:

a_list = ['abc','bcd','bcdefg','abba','cddc','opq']
shortest, *remaining = a_list  # 'abc' and ['bcd','bcdefg','abba','cddc','opq']
ls = len(shortest)

for word in remaining:
    if len(word) < ls:
        shortest = word
        ls = len(word)
print(shortest)

Comments

0

You could just define first element as max and compare with rest and replace the max if the new max is found.

a_list=['abc','bcd','bcdefg','abba','cddc','opq']

max = a_list[0]

for i in a_list[1:]:
    if len(i)>len(max):
        max = i
print(max)

Comments

0

I think one single for loop is enough:

a = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']

longest_str_length = 0
longest_str = None

for i in a:
    curr_length = len(i)
    if curr_length > longest_str_length:
        longest_str_length = curr_length
        longest_str = i

print("Longest string is '{}'".format(longest_str))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.