Computer Science, asked by Anamikaas04, 26 days ago

Write a user defined function in python 'SHIFT(list)' that would accept a list as an argument. The function should shift the negative numbers of the list to the right and the positive number to the left without using another list.​

Answers

Answered by anindyaadhikari13
5

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

def SHIFT(list):

   return [i for i in list if i>=0]+[j for j in list if j<0]

n=int(input('How many elements are required for the list? '))

print('Enter the elements...')

arr=list()

for i in range(n):

   arr.append(int(input('Enter: ')))

print('Given list:',arr)

arr=SHIFT(arr)

print('Modified list:',arr)

The problem is solved using list comprehension. First we will extract the positive numbers and then the negative numbers and concatenate both the list and return it.

\textsf{\large{\underline{Sample I/O}:}}

How many elements are required for the list? 5

Enter the elements...

Enter: 1

Enter: 2

Enter: 3

Enter: -4

Enter: -5

Given list: [1, 2, 3, -4, -5]

Modified list: [1, 2, 3, -4, -5]

Attachments:
Similar questions