120k views
4 votes
Define and implement a method that takes a string array as a parameter and returns the length of the shortest and the longest strings in the array

User Anne Droid
by
7.7k points

1 Answer

3 votes

Answer:

#HERE IS CODE IN PYTHON

#function to find length of shortest and longest string in the array

def fun(list2):

#find length of shortest string

mn=len(min(list2))

#find length of longest string

mx=len(max(list2))

#return both the value

return mn,mx

#array of strings

list2 = ['Ford', 'Volvo', 'BMW', 'MARUTI','TATA']

# call the function

mn,mx=fun(list2)

#print the result

print("shortest length is:",mn)

print("longest length is:",mx)

Step-by-step explanation:

Create an array of strings.Call the function fun() with array as parameter. Here min() function will find the minimum string among all the strings of array and then len() function will find its length and assign to "mn". Similarly max() will find the largest string and then len() will find its length and assign to "mx". Function fun() will return "mn" & "mx".Then print the length of shortest and longest string.

Output:

shortest length is: 3

longest length is: 5

User Rudramuni TP
by
8.2k points