Skip to content

Instantly share code, notes, and snippets.

@Daniyal-Javani
Created January 2, 2016 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniyal-Javani/6011b45012411c416120 to your computer and use it in GitHub Desktop.
Save Daniyal-Javani/6011b45012411c416120 to your computer and use it in GitHub Desktop.
hackerrank.com
<?php
//https://www.hackerrank.com/challenges/cavity-map
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$grid = array();
for($grid_i = 0; $grid_i < $n; $grid_i++)
fscanf($handle,"%s",$grid[]);
for ($i = 1; $i < $n -1 ; ++$i){
for ($j = 1 ; $j < $n -1 ; ++$j){
if ($grid[$i][$j] > $grid[$i - 1][$j] &&
$grid[$i][$j] > $grid[$i + 1][$j] &&
$grid[$i][$j] > $grid[$i][$j - 1] &&
$grid[$i][$j] > $grid[$i][$j + 1]) {
$grid[$i][$j]='X';
}
}
}
foreach ($grid as $value) {
echo $value."\n";
}
<?php
//https://www.hackerrank.com/challenges/game-of-thrones
$_fp = fopen("php://stdin", "r");
fscanf($_fp,"%s",$pass);
$pass_array = str_split($pass);
$number_of_values = array_count_values($pass_array);
//print_r($number_of_values);
if(strlen($pass) % 2 == 1){
$can_be_odd = True;
} else {
$can_be_odd = False;
}
foreach ($number_of_values as $value) {
if($value % 2 == 1){
if($can_be_odd){
$can_be_odd = False; //No longer can be odd!
} else {
echo 'NO';
die();
}
}
}
echo "YES";
<?php
//https://www.hackerrank.com/challenges/time-conversion
$handle = fopen ("php://stdin","r");
fscanf($handle,"%s",$time);
$hh = substr($time, 0, 2);
if ($time[8] == 'P') { // For 12:00:00PM
if ($hh != 12){
$hh += 12;
}
echo $hh.substr($time, 2, 6);
} else {
if ($hh != 12){ //For 12:00:00AM
echo substr($time, 0, 8);
}
else {
$hh = '00';
echo $hh.substr($time, 2, 6);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment