1. Min-Max Sum | Case Study | Solution |
2. Between Two Sets- Find factor between sets | Case Study | Solution |
3. Record breaking | Case Study | Solution |
Node: TypeError – path must be absolute
Node Errors:
TypeError: path must be absolute or specify root to res.sendFile
Solution:
The error states that you need to specify an absolute path instead of relative path.
There are two approach to solve this:
Approach 1:
dirname+index file
– assuming that index.html is in the same directory as the script
res.sendFile(__dirname + '/index.html');
Approach 2:
– specify the root which is used as the base path for the first argument to res.sendFile()
res.sendFile(‘index.html’,{root: __dirname});
Considering the best approach is specifying the root path. When passing the user-generated file path which could possibly contain malformed or malicious parts such as ../../../etc/mms.
Setting the root path will prevent path from being access outside the base path.
In the extreme case if you trust the path then path.resolve will be an option
var path = require('path'); // All other routes should redirect to the index.html app.route('/*') .get(function(req, res) { res.sendFile(path.resolve(app.get('appPath') + '/index.html')); });
Learn about Nodejs:
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: 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
<?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
<?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
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
<?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.