PHP: Staircase Algorithm

Case Study

Consider a staircase of size :

#
##
###
####

Observe that its base and height are both equal to , and the image is drawn using #symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Input Format

A single integer, , denoting the size of the staircase.

Output Format

Print a staircase of size using # symbols and spaces.

Note: The last line must have spaces in it.

Sample Input

6 

Sample Output

#
##
###
####
#####
######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of .

Solution

Link to github

<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);


for ($i=0; $i < $n; $i++) {
for ($j=0; $j < $n; $j++) {
if ($j>=$n-1-$i) {
echo '#';
}else{
echo " ";
}
}
echo "\n";
}

?>

PHP: Diagonal Difference Algorithm

Case Study

Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, . The next lines denote the matrix’s rows, with each line containing space-separated integers describing the columns.

Constraints

Output Format

Print the absolute difference between the two sums of the matrix’s diagonals as a single integer.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Sample Output

15

Explanation

The primary diagonal is:

11
5
-12

Sum across the primary diagonal: 11 + 5 – 12 = 4

The secondary diagonal is:

4
5
10

Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 – 19| = 15

Note: |x| is absolute value function

Solution

Link to github

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$a = array();
for($a_i = 0; $a_i < $n; $a_i++) {
 $a_temp = fgets($handle);
 $a[] = explode(" ",$a_temp);
 array_walk($a[$a_i],'intval');
}

$left=0;
$right=0;
$count= count($a);
for ($i=0; $i < $count; $i++) {
$left += $a[$i][$i];
$right += $a[$count-1-$i][$i];
}
echo abs($left - $right);

?>

PHP: Big Sum Algorithm

Case Study

You are given an array of integers of size . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Input Format

The first line of the input consists of an integer . The next line contains space-separated integers contained in the array.

Output Format

Print a single value equal to the sum of the elements in the array.

Constraints

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output

5000000015

Note:

The range of the 32-bit integer is .

When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

Solution

Link to github

<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');
echo array_sum($arr);
?>

PHP: Compare the Triplets Algorithms

Case Study:

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale  from 1  to 100  for three categories: problem clarity, originality, and difficulty.

We define the rating for Alice’s challenge to be the triplet A = (a0,a1,a2) , and the rating for Bob’s challenge to be the triplet B = (b0,b1,b2).

Your task is to find their comparison points by comparing with a0, with b0,a1 with b1, and a2 with b2.

  • If ai > bi, then Alice is awarded point.
  • If  ai < bi, then Bob is awarded point.
  • If ai = bi, then neither person receives a point.

Comparison points is the total points a person earned.

Given A and B, can you compare the two challenges and print their respective comparison points?

Input Format

The first line contains 3 space-separated integers, a0,a1 , and a2, describing the respective values in triplet A.
The second line contains space-separated integers, b0, b1, and b2, describing the respective values in triplet B.

Constraints

 1 <= ai <= 100
1 <= bi <= 100

Output Format

Print two space-separated integers denoting the respective comparison points earned by Alice and Bob.

Sample Input

5 6 7
3 6 10

Sample Output

1 1 

Explanation

In this example:

Now, let’s compare each individual score:

  • a0 > b0, so Alice receives point.
  • a1 = b1, so nobody receives a point.
  • a2 < b2, so Bob receives point.

Alice’s comparison score is 1, and Bob’s comparison score is 1. Thus, we print 1 1 (Alice’s comparison score followed by Bob’s comparison score) on a single line.

Solution

Link to github

<?php

$handle = fopen ("php://stdin", "r");
function solve($a0, $a1, $a2, $b0, $b1, $b2){
    // Complete this function
    $aScore = 0;
    $bScore = 0;

    $aScore += ($a0 > $b0? 1:0) + ($a1 > $b1? 1:0) + ($a2 > $b2? 1:0);
    $bScore += ($b0 > $a0? 1:0) + ($b1 > $a1? 1:0) + ($b2 > $a2? 1:0);
    
    $score = [];
    array_push($score,$aScore,$bScore);
    return $score;   
}

fscanf($handle, "%d %d %d", $a0, $a1, $a2);
fscanf($handle, "%d %d %d", $b0, $b1, $b2);
$result = solve($a0, $a1, $a2, $b0, $b1, $b2);
echo implode(" ", $result)."\n";
?>

Feel free to add your comments.

HR Challenge – Day 0 (PHP)

Hi there!!!

This year my focus is more on coding and fitness. I have decided to take challenge from hackerrank. To begin with I have decided to take the 30 days of coding in php.

Day 0: Hello World

Problem:

Objective
In this challenge, we review some basic concepts that will get you started with this series. You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank. Check out the Tutorial tab for learning materials and an instructional video!

Task
To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World. on a single line, and finally print the value of your variable on a second line.

You’ve got this!

Note: The instructions are Java-based, but we support submissions in many popular languages. You can switch languages using the drop-down menu above your editor, and the variable may be written differently depending on the best-practice conventions of your submission language.

Input Format

A single line of text denoting (the variable whose contents must be printed).

Output Format

Print Hello, World. on the first line, and the contents of on the second line.

Sample Input

Welcome to 30 Days of Code!

Sample Output

Hello, World. 
Welcome to 30 Days of Code!

Explanation

On the first line, we print the string literal Hello, World.. On the second line, we print the contents of the variable which, for this sample case, happens to be Welcome to 30 Days of Code!. If you do not print the variable’s contents to stdout, you will not pass the hidden test case.

Solution:


?php
$_fp = fopen("php://stdin", "r");

$inputString = fgets($_fp); // get a line of input from stdin and save it to our variable

// Your first line of output goes here
print("Hello, World.\n");

// Write the second line of output
print $inputString;
$stdout = fopen('php://stdout', 'w');

fclose($_fp);
?