Q. Write a program as per following specifications:
(i)
'''return the length of the longest
String in the list of a strings str_list.
Precondition: the list will contain
At least one element .'''
(ii)
'''L is a list of numbers. return a new list where each element is the corresponding element of list L summed with number num.'''
You can understand by Watching video :-
Answer :-
(i)
lst = eval(input("Enter a list of string = ")) if lst == [] : print("List should contain at least one element ") else : long = "" for i in range(len(lst)) : if len(long ) < len(lst[i]) : long = lst[i] print("Longest string in list = ",long)
(ii)
lst = eval(input("Enter a list = ")) num = int(input("Enter a number = ")) for i in range(len(lst)): lst[i] = lst[i] + num print("New list = ",lst)
Output :-
(i)
Enter a list of string = ["Path","Walla","Portal","Express"]
Longest string in list = Express
>>>
Enter a list of string = []
List should contain at least one element
>>>
(ii)
Enter a list = [1,2,3,4,5,6,7,8,9]
Enter a number = 5
New list = [6, 7, 8, 9, 10, 11, 12, 13, 14]
>>>
Enter a list = [78,54,2,9,4,6,3,84,21,74]
Enter a number = 9
New list = [87, 63, 11, 18, 13, 15, 12, 93, 30, 83]
>>>
Thank you !
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )