2

I have started to practice coding problems (hackerearth.com) in PHP to increase my problem-solving skill.
As I saw, most of the coding problems are asked for taking input and then output the correct answer based on entered input.

Eg : Input-

  1. The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.
  2. The next line consists of N space separated integers , denoting the elements of the array A.

Till now, I know -

 fscanf(STDIN, "%d %d\n", $n, $k); //takes N and K

But I don't know how to take an array of size N.

Please help me how to take array of size N. Then It will help me to code further. Else I will just stuck on taking input.

EDIT:

Please help me any PHP pro coder.

EDIT 2:

The problem on which I am still stuck is given below -
Coding challenge -
Monk and Rotation Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.
enter image description here enter image description here

EDIT 3 -

Problem can be found here.
What I have tried till know to solve this problem-

fscanf(STDIN, "%s\n", $t);
fscanf(STDIN, "%s %s\n", $n, $k);
//taking 5 numbers seperated by space.
fscanf(STDIN, "%d %d %d %d %d\n", $item1,$item2,$item3,$item4,$item5);

$arr = [$item1,$item2,$item3,$item4,$item5];
for($i = 0; $i<$k; $i++){
    array_unshift($arr, array_pop($arr));
}
echo implode(' ', $arr);
diana
  • 39
  • 6
  • Please share more details. How **exactly** do you want to gather such an input? As a comma seperated list? In a do/while loop? – Nico Haase Jun 03 '21 at 13:00
  • I have already posted a link of my problem on which I am currently stuck because of unable to take input. By the way see my `EDIT 2` to get the whole problem. – diana Jun 03 '21 at 14:20
  • @NicoHaase Please see my problem in `EDIT 2`. – diana Jun 03 '21 at 14:29
  • What have you tried to resolve the problem? Probably, the input is a string of numbers, seperated by a space? – Nico Haase Jun 03 '21 at 18:40
  • @NicoHaase I have tried `fscanf(STDIN, "%d %d\n", $n, $k);` to take input for `N` and `K` where N is size of the array. But I don't know how to take an array of size N. As given in question, they gave N=5. So I used `fscanf(STDIN, "%d %d %d %d %d\n", $a1, $a2, $a3, $a4,$a5);` to take 5 numbers. But If the value of N changes to something other than 5 then `fscanf(STDIN, "%d %d %d %d %d\n", $a1, $a2, $a3, $a4,$a5);` is not working. This is the main problem I am stuck on it. – diana Jun 04 '21 at 02:25
  • The size of the array is dynamic. So elements inside the array can be up to `10 to the power 5`. I passed the first test case and its output is correct, but I failed in all other test cases because of array size is dynamic. Please let me know If you still didn't get ??? – diana Jun 04 '21 at 02:30
  • Why not read the value, and use `explode`? Much better than checking for a fixed "array" size – Nico Haase Jun 04 '21 at 07:56
  • I used `explode(' ', readline());` but it is returning empty array. Please open [link](https://www.hackerearth.com/practice/codemonk/) and then click on `Monk and Roation` to solve the problem by using your coding tricks... – diana Jun 04 '21 at 08:03
  • Please share all such details by editing your question. If `fscanf` works better, why not read the entire string in through that? – Nico Haase Jun 04 '21 at 08:10
  • atleast one time use the link which I send just above of this comment and try your code at there. you will get full detail there. – diana Jun 04 '21 at 08:23
  • 1
    No, thanks. You should list **your** approaches to resolve the problem if you need help – Nico Haase Jun 04 '21 at 08:27
  • @NicoHaase now please see my `EDIT 3`. – diana Jun 04 '21 at 08:52

2 Answers2

0

You could use readline to read in the space-separated integers, then just split at the spaces to get an array. Note that the array elements will be of type string.

// input string
$string = readline();

// turn into an array
$array = explode(" ", $string);
Tyrmos
  • 387
  • 4
  • 13
  • I have tried `$arr = explode(' ', readline());` but it is returning empty array in hackearth. Please go to [exmaple](https://www.hackerearth.com/practice/codemonk/) and click on 'Monk and Rotation' to get the problem I am working on it. – diana Jun 03 '21 at 12:52
  • Problem is very simple but I don't know how to take input in PHP. – diana Jun 03 '21 at 12:56
  • please see my `EDIT 2` to get what is my real problem on which I am working. – diana Jun 03 '21 at 14:30
0

In hackerearth for PHP you can do something like this -

function getMyInput($n)
{
    echo '<pre>';
    print_r($n);
}

$t = fgets(STDIN);

for ($t_itr = 0; $t_itr < $t; $t_itr++) {
    $n[] = fgets(STDIN);  
}
getMyInput($n);

Reference link

PHP standard input?

What does this code mean "ofstream fout(getenv("OUTPUT_PATH"));"

https://www.php.net/manual/en/function.fgets.php https://www.php.net/manual/en/function.intval.php

Explanation

Use fgets to read a line from the input using STDIN (I/O reading) then iterate from zero to the maximum provided INPUT and assign those values into a variable.
create a custom method and pass the I/O data (array variable) as an argument to make your own logic for problem-solving.

rocky
  • 631
  • 5
  • 14