69.7k views
2 votes
Write a program that will filter a list of non-negative integers such that all duplicate values are removed. Integer values will come from standard input (the keyboard) and will range in value from 0 up to 2,000,000,000. Input will be terminated by the special value, -1. Once the input is read in you should output (to the screen) the list of numbers as a sorted list (smallest to largest) with one value listed per line where all duplicates have been removed. The primary difficulty with this program is that there are an enormous number of input values and an expected large number of duplicate numbers.

1 Answer

6 votes

Answer:

The program in python is as follows:

original = []

inp = int(input(": "))

while inp>=0:

if inp<=2000000000:

original.append(inp)

inp = int(input(": "))

newlist = []

for i in original:

if i not in newlist:

newlist.append(i)

newlist.sort()

for i in newlist:

print(i)

Step-by-step explanation:

This initializes the original list

original = []

This gets input for the list

inp = int(input(": "))

The following loop is repeated while input is not negative

while inp>=0:

This ensures that only inputs from 0 and 2000000000 enters the list

if inp<=2000000000:

original.append(inp)

Prompt the user for another input

inp = int(input(": "))

This initializes a newlist

newlist = []

Iterates through the original list

for i in original:

Check for duplicate

if i not in newlist:

Each number is entered to the new list, without duplicate

newlist.append(i)

This sorts the new list in ascending order

newlist.sort()

This prints the list item one per line

for i in newlist:

print(i)

User Narsereg
by
6.4k points