Skip to content
Advertisement

How to read input in Google Kickstart (PHP)

How can I read following stdin for Google Kickstart oder Jam code etc.:

4
1 9
91 99
451 460
501 1000

So the 4 in the first line has to be in a var (e.g. $a); Then in every line after that: the first param goes into an Array of $b[] e.g. And the same with the second param. In $c[] e.g. So the result should be:

lang: PHP

$a = "4";
$b = ["1", "91", "451", "501"];
$c = ["9", "99", "460", "1000"];

It doesn’t matter if it’s an integer or a string.

Thank you

Advertisement

Answer

Do you think so, like this?

<?php
$str="4
1 9
91 99
451 460
501 1000";

$t = explode("n", $str);
print_r($t);

foreach($t as $r) {
    $n[] = explode(" ",$r);
}
$a = array_shift($n)[0]; 
$b = [];
foreach($n as $v) {
    $b[] = $v[0];
    $c[] = $v[1];
}

print_r($a);
print_r($b);
print_r($c);

output

4
Array
(
    [0] => 1
    [1] => 91
    [2] => 451
    [3] => 501
)
Array
(
    [0] => 9
    [1] => 99
    [2] => 460
    [3] => 1000
)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement