Computer Science, asked by natsudragonaire777, 4 months ago

Write a program to copy from 2 equal sized lists into one list. Suppose there are three

lists named A, B, C. Even position of A will get the elements from B and odd position of

A will get element from C in python.​

Answers

Answered by anindyaadhikari13
3

Answer:

The given program is written in Python 3.

def my_func():

n=int(input("How many elements? "))

if n<=0:

print("Invalid input.")

else:

A,B,C=[],[],[]

print("Enter the elements in list B.")

for i in range(n):

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

print("Enter the elements in list C.")

for i in range(n):

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

print("List B:",B)

print("List C:",C)

x=0

for i in range(2*n):

if i%2==0:

A.append(C[x])

else:

A.append(B[x])

x+=1

print("List A:",A)

my_func()

Logic:

Logic is very simple.

Let us assume that number of elements = 5 i.e.,

→ n = 5

→ So, there will be 5 elements in both list B and list C.

Let us put some values in both lists.

>> B = [1,2,3,4,5]

>> C = [6,7,8,9,10]

Now, list A would become,

>> A = [6,1,7,2,8,3,9,4,10,5]

Elements in even position are - [1,2,3,4,5]

Elements in odd position are - [6,7,8,9,10]

In lists, the index value starts with 0.

So, if the index value is even, position is odd and if the index value is odd, position is even.

We will check if the index value is even or not. If true, we will append elements from list C or else, we will append elements from list B.

At last, the new list will be shown.

Refer to the attachment.

•••♪

Attachments:
Similar questions