Problem D
Analyze List
Write a program that given a list $\ell $ of positive integers, prints out the following information:
-
The list $\ell $.
-
The sorted version of $\ell $.
-
A sorted list of the non-prime numbers in $\ell $. Note, that $1$ is included despite not being composite. The resulting list should only include each value once.
-
Minimum, maximum, and average values in $\ell $. The average value should be formatted to two decimal places.
The function is_prime() is given.
You need to print out an error message if the user enters invalid values into the list, and ask the user again for input until valid input is given.
Input
The input consists of one or more lines, each containing a list $\ell $.
If the list $\ell $ contains invalid values then the program should ask for another list until a valid list is given.
In the tests, each $\ell $ will be a list of length $1$ to $1\, 000$. The elements of the list will be integers or letters of the English alphabet.
Output
The output should contain at least $4$ lines. First if the user input an
invalid list the program should display.
“Incorrect input! Please try again.”
for every invalid input until a valid one is encountered.
Then the program should print the following four lines:
-
“Input list: {$\ell $}”.
-
“Sorted list: {$s$}” where $s$ is the list $\ell $ sorted.
-
“Composite list: {$c$}”, where $c$ is a list of non-prime numbers in $\ell $.
-
“Min: {$\ell _{min}$}, Max: {$\ell _{max}$}, Average: {$\mu $}”
Sample Input 1 | Sample Output 1 |
---|---|
2,5,7,2,8,10,34,23,9,4,5 |
Input list: [2, 5, 7, 2, 8, 10, 34, 23, 9, 4, 5] Sorted list: [2, 2, 4, 5, 5, 7, 8, 9, 10, 23, 34] Composite list: [4, 8, 9, 10, 34] Min: 2, Max: 34, Average: 9.91 |
Sample Input 2 | Sample Output 2 |
---|---|
4,7,2,9,11,13,22,5,33,7,13 |
Input list: [4, 7, 2, 9, 11, 13, 22, 5, 33, 7, 13] Sorted list: [2, 4, 5, 7, 7, 9, 11, 13, 13, 22, 33] Composite list: [4, 9, 22, 33] Min: 2, Max: 33, Average: 11.45 |
Sample Input 3 | Sample Output 3 |
---|---|
5,7,x 3, -4, 5, -6 2,8,8 |
Incorrect input! Please try again. Incorrect input! Please try again. Input list: [2, 8, 8] Sorted list: [2, 8, 8] Composite list: [8] Min: 2, Max: 8, Average: 6.00 |