233k views
5 votes
Write a Python function that takes as input a list, A, and returns two lists L and G. L constains all numbers in A, which are not A[0] and are less than or equal to A[0]. G contains all numbers in A, which are not A[0], and are greater than A[0].

User Jitka
by
4.5k points

1 Answer

3 votes

Answer:

In Python:

def split(A):

L=[]; G=[]

for i in range(1,len(A)):

if (A[i] != A[0] and A[i] < A[0]):

L.append(A[i])

if (A[i] != A[0] and A[i] > A[0]):

G.append(A[i])

return L, G

Step-by-step explanation:

This defines the function

def split(A):

This initializes the L and G lists

L=[]; G=[]

This iterates through the original list A

for i in range(1,len(A)):

This populates list L using the stated condition

if (A[i] != A[0] and A[i] < A[0]):

L.append(A[i])

This populates list G using the stated condition

if (A[i] != A[0] and A[i] > A[0]):

G.append(A[i])

This returns the two lists L and G

return L, G

User Warrick
by
4.5k points