Problem Description Given a list of integers, find and display all even numbers from the…
- User
Problem Description
Given a list of integers, find and display all even numbers from the end of the list. Input Format
Each line of input begins with an integer N indicating the number of integer n that follow which comprises a list.
Output Format
All even numbers from the end of the list, each separated by a single space. Separate output for each test case with the newline character. Otherwise, display the words None to display
Constraints
2 ≤ N ≤ 100
-1000 ≤ n ≤ 1000
SAMPLE INPUT 5 10 25 12 4 1 3 16 28 100
SAMPLE OUTPUT 4 12 10 100 28 16
Assistant
We need to interpret the problem correctly.
Problem: "Given a list of integers, find and display all even numbers from the end of the list."
But reading the description: "Each line of input begins with an integer N indicating the number of integer n that follow which comprises a list."
So input format: Each line has an integer N followed by N integers. We need to output for each line: all even numbers from the end of the list.
Specifically: "find and display all even numbers from the end of the list." Possibly we need to output the even numbers in reverse order? Or as they appear from the end? Sample: Input: "5 10 25 12 4 1" -> N=5, numbers: 10, 25, 12, 4, 1. Even numbers: 10, 12, 4. "From the end of the list": So starting from the end, we go backward and output even numbers? The sample output: "4 12 10". That's the even numbers from the list, but in reverse order (i.e., starting from the last element, go forward? Actually, list: [10,25,12,4,1]. Even numbers: 10 at index 0, 12 at index 2, 4 at index 3. From the end: the last element is 1 (odd), then 4 (even) -> output 4, then moving backwards: next is 12 -> output 12, then next is 25 (odd), then 10 (even) -> output 10. So output order is reverse of original order for even numbers. So indeed, we need to output the even numbers in reverse order of occurrence.
Second sample line: "3 16 28 100" -> N=3, numbers: 16, 28, 100. Even numbers: all. From the end: last=100, then 28, then 16 -> output "100 28 16". So matches.
And if there are no even numbers? Output "None" (the problem says: "Otherwise, display the words None to display").
Note: The output format says: "All even numbers from the end of the list, each separated by a single space. Separate output for each test case with the newline character. Otherwise, display the words None to display" So if no even numbers, output "None" (presumably exactly that).
Constraints: N between 2 and 100, inclusive. n between -1000 and 1000.
We need to process possibly multiple lines of input. The sample input shows two lines. So input is read until EOF.
Implementation: For each line, read integers. The first integer is N. Then next N integers are the list. We can ignore N because we can count. But we should ensure we only read N numbers.
Edge cases: The list may contain negative even numbers. Evenness defined by n%2==0 (including zero and negatives). So need to include them.
The output: For each test case, print the even numbers from the end, space-separated. If none, print "None". And each test case output on its own line.
Now, we need to produce code to solve this. Likely language unspecified? But the problem is for typical coding contest. We'll provide solution in Python.
Implementation steps:
- Read all lines from stdin (sys.stdin.read().strip().splitlines()) or iterate sys.stdin.
- For each line, if line is empty skip.
- Split line into list of strings, convert to ints.
- If the list is empty, skip.
- The first integer is N. But there could be extra numbers? Probably not. We can verify: if len(nums) != N+1? Actually, line contains N followed by N numbers, so length should be N+1. But we can trust N and then take next N numbers; if there are extra (maybe due to spaces?), but better to use N to slice: numbers = ints[1:1+N]. But we need to ensure we only take N numbers. In case line contains more? Probably not. Could also ignore N and just process all numbers after first? But the problem says N indicates number of integers that follow, so line may have exactly N+1 numbers.
But careful: The input may be multiple test cases separated by newlines, but also possibly spaces across lines? Usually each test case is on its own line.
Implementation:
import sys
def solve(): output_lines = [] for line in sys.stdin: line = line.strip() if not line: continue parts = line.split() if not parts: continue # Convert to ints ints = list(map(int, parts)) N = ints[0] # Ensure we have at least N numbers numbers = ints[1:1+N] # slice will handle if N larger than available? But constraints guarantee. evens_from_end = [] # Traverse from end for num in reversed(numbers): if num % 2 == 0: evens_from_end.append(str(num)) if evens_from_end: output_lines.append(" ".join(evens_from_end)) else: output_lines.append("None") sys.stdout.write("\n".join(output_lines))
if name == "main": solve()
That should work.
But the sample output: "4 12 10 \n100 28 16" Actually there is a trailing space? The sample output shows lines with spaces but no trailing space. In our join, no trailing.
Check: For first line: evens_from_end = ["4","12","10"] -> join "4 12 10". Good.
Edge: If numbers count is less than N? Not possible per spec.
Now test with sample input: "5 10 25 12 4 1\n3 16 28 100". Our code yields "4 12 10\n100 28 16". matches.
Now negative even numbers: Example: N=2, numbers: -2, -3 -> evens from end: traverse reversed: -3 (odd) skip, -2 (even) -> output "-2". Works.
If no evens: output "None".
Complexities: O(N) per test case.
Thus final answer: Provide explanation and code.
But the request: "Problem Description ..." The user likely wants code solution. They might be using an online judge. So we can output the Python code.
We should also note that we might need to handle cases where there are extra spaces or lines. Use sys.stdin.read to get all tokens? Another approach is to read all tokens and then parse using N. But since each test case is on a line, line-based is simpler and matches input format.
Potential pitfalls: Input may not be line-separated? The description says "Each line of input begins with an integer N...". So line-based.
Thus final. ```python
import sys
def solve() -> None: """ Reads multiple test cases from standard input. For each test case, prints all even numbers from the end of the list, separated by spaces. If there are no even numbers, prints "None". Each test case output is on its own line. """ output_lines = []
for line in sys.stdin: line = line.strip() if not line: # skip empty lines continue parts = line.split() # Convert all tokens to integers nums = list(map(int, parts)) # The first integer is N, the number of elements that follow N = nums[0] # The next N integers are the list lst = nums[1:1 + N] even_rev = [] # Traverse from the end for x in reversed(lst): if x % 2 == 0: even_rev.append(str(x)) if even_rev: output_lines.append(" ".join(even_rev)) else: output_lines.append("None") sys.stdout.write("\n".join(output_lines))if name == "main": solve()