29.0k views
0 votes
12.17 lab: filter and sort a list write a program that gets a list of integers from input, and outputs negative integers in descending order (highest to lowest). ex: if the input is: 10 -7 4 -39 -6 12 -2 the output is: -2 -6 -7 -39 for coding simplicity, follow every output value by a space. do not end with newline.

2 Answers

5 votes

Final answer:

To filter and sort a list of integers, create a new list to store the negative numbers, loop through the input list and add negative numbers to the new list, sort the new list in descending order, and print the sorted list.

Step-by-step explanation:

To filter and sort a list of integers, you can follow these steps:

  1. Read the input list of integers.
  2. Create a new list to store the negative integers.
  3. Loop through each number in the input list:
  • If the number is negative, add it to the new list.
Sort the new list of negative integers in descending order.Print the sorted list of negative integers followed by a space after each value.

In the given example, the input is '10 -7 4 -39 -6 12 -2'. Following the steps above, the program would output: '-2 -6 -7 -39'.

4 votes

Here's a simple Python program that takes a list of integers as input and outputs the negative integers in descending order.

# Get a list of integers from input

input_list = list(map(int, input().split()))

# Filter and sort negative integers in descending order

negative_numbers = sorted(filter(lambda x: x < 0, input_list), reverse=True)

# Output the result with each value followed by a space

output_str = ' '.join(map(str, negative_numbers))

print(output_str, end=' ')

You can run this program and input a list of integers, and it will output the negative integers in descending order, each followed by a space. For example:

Input: 10 -7 4 -39 -6 12 -2

Output: -2 -6 -7 -39

User Dyodji
by
8.5k points