Closure Counter
PHP
Hard
5 views
Problem Description
Create a counter() closure that returns 1,2,3,... each time it is called. Input q, print q lines of counts.
Input Format
One integer q.
Output Format
q lines counts.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$q=intval($inputText);
$counter=function(){
static $c=0;
$c++;
return $c;
};
$output=[];
for($i=0;$i<$q;$i++) $output[] = strval($counter());
echo implode(PHP_EOL,$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!