Write a function in python named SwapHalfList(Array) ,which accepts a list Array of numbers and sawps the element of 1st Half of the list with the 2nd Half of the list ONLY if the sum of 1 st Half is greater than 2nd Half of the list

 

"""

"""

Write a function in python named SwapHalfList(Array) ,which accepts a list Array of numbers and sawps the element of 1st Half of the list with the 2nd Half of the list ONLY if the sum of 1 st Half is greater than 2nd Half of the list 


"""


Testcases1:

Input:

[1,3,4,3]


Output:

[4,3,1,3]


Testcases2:

Input:

[1,90,8,6,8]

Output:

[6,8,8,1,90]


# First we will take two empty list i.e F1 and F2


Steps to Follow:

1.First take two empty list so that you can take seperately half half list.
2.Then take any list or you can input the from the user.
3.Then define a function and then check given list contain even number it means([2,1,33,2] this is called even number list.
4.If list is even then do the sum of the first half and second seperately ,then check if first half of the sum is greater than second ,then run ahead.
5.Now half list  numbers append in the F1 and  second half list append in F2 .Then add two list F2 and F1.
If your list contain is odd then follow below steps:
1.If your list is odd then middle value of the list take sepaerately and then similarily above statement follow ,
2.At the last add F2+F1 and middle index value of the list.


print("You are going to swap the first half of the list to second half. ") 

F1=[ ]

F2=[ ]

array=eval(input("Enter numbers in the list:") 

U=array[len(array)//2]




# Now we will define a function and we will check if the length of list is even then we will operation of swaping and in the else part we will do only something differnt operation


def list(array):

    

if len(array)%2==0:

sum=0

tsum=0

for i in range(len(array)//2):

sum+=array[i]

for j in range(len(array)//2,len(array)):

tsum+=array[j]

if sum>tsum:

for k in  range(len(array)//2):

F1.append(array[k])

for l in  range(len(array)//2,len(array)):

F2.append(array[l])

else:

pass

print(F2+F1)

else:

                pass

sum=0

tsum=0

for i in range(len(array)//2+1):

sum+=array[i]

for j in range(len(array)//2+1,len(array)):

tsum+=array[j]

if sum>tsum:

for k in  range(0,len(array)//2):

F1.append(array[k])

for l in  range(len(array)//2+1,len(array)):

F2.append(array[l])

        

        

        

print(F2+[U]+F1)



list(array)

print("Your list successfully swaped.") 




























No comments:

Powered by Blogger.