Computer Science, asked by dixitarchita25, 1 month ago

write a program to take a list from user then arrange list element in reverse order after doubling each element
input-[2,9,18,7,13,4]
output-[8,26,24,36,18,4]​

Answers

Answered by anindyaadhikari13
2

Answer:

The given program is written in Python.

1. Using user-defined logic.

n=int(input("How many elements for the list?? "))

print("Enter the elements...")

l=[]

for i in range(n):

   l.append(int(input(">> ")))

print("Given List:",l)

for i in range(n):

   l[i]=2*l[i]

l=l[::-1]

print("Modified List:",l)

Explanation:

  • Line 1: Ask the user for the number of elements.
  • Line 2: Display a message asking the user for giving input.
  • Line 3: Create a list.
  • Line 4-5: Adds the elements in the list.
  • Line 6: Displays the given list.
  • Line 7-8: Here, modification takes place. Each element in the list is replaced with twice of that element.
  • Line 9: List is reversed using slicing.
  • Line 10: Modified list is shown.

2. Using slicing.

n=int(input("How many elements for the list?? "))

print("Enter the elements...")

l=[]

for i in range(n):

   l.append(int(input(">> ")))

print("Given List:",l)

l=[2*i for i in l][::-1]

print("Modified List:",l)

Explanation:

  • This is a modified version of the previous approach. Each element is multiplied with 2 and the result is stored in the last and then the list is reversed. All these works are done in one line (Line 7).

Refer to the attachment for output.

•••♪

Attachments:
Similar questions