Problem C
Remove Odds
Write two functions for removing odd numbers from integer lists:
-
extract_evens(int_list):
Returns a new list with only even integers from int_list, without modifying int_list. -
remove_odds(int_list):
Removes odd integers from int_list, thus modifying int_list.
Hint: Functions have the ability to modify mutable objects in the calling program. Lists are mutable objects.
The main file, which handles input and output, is already provided. - Please only submit your function definitions, without any code outside the functions!
Input
Each of the functions should accept one list $l$ as a parameter. You may assume the elements of the list $l$ will be integers.
In the samples below, the input consists of one line containing a sequence $x = (x_1, \dots , x_n)$ of $n$ numbers separated by a space. The autograder will handle reading the input, splitting it, converting to integers and passing the resulting list as an argument to the functions.
In the tests, $x$ will always satisfy $1 \le n \le 1\, 000$ and $1 \le x_i < 100$ for all $i$ with $1 \le i \le n$.
Output
The function extract_evens() should return a new list $e$ containing all the even numbers appearing in $l$, in the same order as they appear in $l$, and no other numbers. After the function is called, the original list $l$ should remain unchanged.
The function remove_odds() should return nothing (just None), but should modify the given list $l$, so that after the function is called, the list will have changed to a modified list $l'$ containing all the even numbers appearing in $l$, in the same order as they appear in $l$, and no other numbers.
In each test case, the autograder will call both of the functions with the given list as a parameter, in sequence, and will verify the results as well as the effects on the given list, and print the following four lines to the output, as seen in the samples below:
-
“Original list before calling functions: {$l$}”
-
“Resulting list after extracting evens: {$e$}”
-
“Original list after extracting evens and before removing odds: {$l$}”
-
“Original list after removing odds: {$l'$}”
Sample Input 1 | Sample Output 1 |
---|---|
1 1 2 3 4 5 |
Original list before calling functions: [1, 1, 2, 3, 4, 5] Resulting list after extracting evens: [2, 4] Original list after extracting evens and before removing odds: [1, 1, 2, 3, 4, 5] Original list after removing odds: [2, 4] |
Sample Input 2 | Sample Output 2 |
---|---|
10 7 7 7 7 1 10 9 9 |
Original list before calling functions: [10, 7, 7, 7, 7, 1, 10, 9, 9] Resulting list after extracting evens: [10, 10] Original list after extracting evens and before removing odds: [10, 7, 7, 7, 7, 1, 10, 9, 9] Original list after removing odds: [10, 10] |
Sample Input 3 | Sample Output 3 |
---|---|
1 1 2 3 4 5 6 7 8 9 10 |
Original list before calling functions: [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Resulting list after extracting evens: [2, 4, 6, 8, 10] Original list after extracting evens and before removing odds: [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Original list after removing odds: [2, 4, 6, 8, 10] |
Sample Input 4 | Sample Output 4 |
---|---|
9 1 3 3 52 7 |
Original list before calling functions: [9, 1, 3, 3, 52, 7] Resulting list after extracting evens: [52] Original list after extracting evens and before removing odds: [9, 1, 3, 3, 52, 7] Original list after removing odds: [52] |