write the program in python to shift the negative numbers to left and the positive numbers to right so that the resultant list will be as follows:
original list [ -2, 1, -3, -15, 16, -17, 5, -3, -6]
output should be [ 1, 16, 5, -6, -3, -17, -15, -3, -2]
Answers
Answered by
15
Answer:
list1=[-2,1,-3,-15,16,-17,5,-3,-6]
list2=[0]*9
a=len(list1)
a=a-1
j=0
for i in range(9):
if list1[i]>0:
list2[j]=list1[i]
j+=1
else:
list2[a]=list1[i]
a-=1
print(list2)
Explanation:
Attachments:
Answered by
4
Answer:
l=[-2, 1, -3, -15, 16, -17, 5, -3, -6 ]
n=len(l)
print("Original list:",l)
for i in range(n-1):
for j in range(n-i-1):
if l [ j ] <0:
l [ j ], l [ j + 1] = l [ j + 1], l [ j ]
print("After shifting list is:", l)
Output:
Original list: [ -2, 1, -3, -15, 16, -17, 5, -3, -6 ]
After shifting list is: [ 1,16,5,-6,-3,-17,-15,-3,-2]
Similar questions
Physics,
2 months ago
English,
2 months ago
Political Science,
2 months ago
Environmental Sciences,
4 months ago
Computer Science,
10 months ago