In: Computer Science
Given a list of items, write a program that generates a list of lists of the following form:
[a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]]
Hint: Slicing is your friend.
please write a python program
n=int(input("Enter the list size : "))
l=[]
for c in range(n):
l.append(input())
l2=[]
for i in range(1,n+1):
l2.append(l[-i:])
print(l2)
The above program will take the input automatically the characters from a to z and return the result in the form of the output specified.Here as we are using the concept of slicing that if i =1 then we make it l[-1: ] which will give nals t element and l[-2:] which gives last two elements and so on upto the length.The above program is designed based on the question.chr(c) will convert the integer to the character as per the ASCII values.
SCREENSHOT OF THE CODE ALONG WITH THE OUTPUT SCREENSHOT:
BELOW I AM PROVIDING ANOTHER CODE WHERE YOU CAN TAKE THE INPUT AS THE SUER DEFINED WHICH WE CAN TAKE THE STRING INPUT.
CODE:
n=int(input("Enter the list size : "))
l=[]
for c in range(n):
l.append(input())
l2=[]
for i in range(1,n+1):
l2.append(l[-i:])
print(l2)
The above program will ask the suer to enter the size of the list and then it aks the program to enter the values (For example we take 26 as the input and we enter 26 values and then we get the input and then we get the output in the desired format.
SCREENSHOT OF THE CODE AND OUTPUT :