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

<?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: 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

<?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";
?>