Filter Even Numbers
Python
Medium
2 views
Problem Description
Read n integers. Write function only_even(arr) that returns list of even numbers in same order. Output them space-separated, if none output EMPTY.
Input Format
First line n. Second line n integers.
Output Format
Even numbers or EMPTY.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
def only_even(arr):
out=[]
for x in arr:
if x%2==0:
out.append(x)
return out
res=only_even(a)
sys.stdout.write('EMPTY' if not res else ' '.join(map(str,res)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!