30.2k views
5 votes
Write a program that accepts a list of integers and split them into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top.

1 Answer

3 votes

Answer:

The program is as follows:

n = int(input("List elements: "))

mylist = []; myList2 = []

for i in range(n):

num = int(input(": "))

mylist.append(num)

for i in mylist:

if i >= 0:

myList2.append(i)

for i in mylist:

if i < 0:

myList2.append(i)

print(myList2)

Step-by-step explanation:

This gets the number of list elements from the user

n = int(input("List elements: "))

This initializes two lists; one of input and the other for output

mylist = []; myList2 = []

This is repeated for every input

for i in range(n):

Get input from the user

num = int(input(": "))

Append input to list

mylist.append(num)

This iterates through the input lists

for i in mylist:

If list element is non-negative

if i >= 0:

Append element to the output list

myList2.append(i)

This iterates through the input lists, again

for i in mylist:

If list element is negative

if i < 0:

Append element to the output list

myList2.append(i)

Print output list

print(myList2)

User Nikko Khresna
by
7.4k points