SET/GET One Variable
PHP
Medium
4 views
Problem Description
You will get q commands: SET x, GET. Keep a single variable (starts EMPTY). GET prints value or EMPTY.
Input Format
First line q. Next q lines commands.
Output Format
Outputs for each GET.
Sample Test Case
Input:
6
GET
SET 10
GET
SET -3
GET
GET
Official Solution
<?php
$inputLines=preg_split('/\\R/', trim(stream_get_contents(STDIN)));
if(!$inputLines || trim($inputLines[0])==='') exit;
$q=intval($inputLines[0]);
$val=null; $output=[];
for($i=1;$i<=$q;$i++){
$t=trim($inputLines[$i] ?? '');
if($t==='') continue;
$tokens=preg_split('/\\s+/', $t, 2);
if($tokens[0]==='SET') $val=$tokens[1] ?? '';
else $output[] = ($val===null?'EMPTY':$val);
}
echo implode(PHP_EOL,$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!