Async Filter Positives
JavaScript
Medium
3 views
Problem Description
Given n integers, keep only positive numbers using async predicate. Print new length and the filtered list.
Input Format
Line1: n. Line2: n integers.
Output Format
Line1: m. Line2: m integers (or empty).
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/).map(Number);let i=0;const n=a[i++];const arr=a.slice(i,i+n);const ok=async x=>{await Promise.resolve();return x>0;};(async()=>{let out=[];for(const x of arr){if(await ok(x))out.push(x);}let res=String(out.length);if(out.length)res+='\
'+out.join(' ');process.stdout.write(res);})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!