Last active
June 12, 2019 15:59
-
-
Save martin-mok/53eaf1a98d084091f13a43d9d414f2b4 to your computer and use it in GitHub Desktop.
PHP code tested for solving compare-the-triplets in https://www.hackerrank.com/challenges/compare-the-triplets/problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
The problem is here: | |
https://www.hackerrank.com/challenges/compare-the-triplets/problem | |
Reference: Code for creating a simple php calculator for debugging | |
https://stackoverflow.com/questions/21445245/simple-php-calculator | |
*/ | |
$result = ""; | |
function compareTriplets($a, $b) { | |
$r=array(0,0); | |
for ($i = 0; $i < count($a); ++$i) { | |
if($a[$i]>$b[$i]){ | |
$r[0]=$r[0]+1; | |
} elseif($a[$i]<$b[$i]){ | |
$r[1]=$r[1]+1; | |
} | |
} | |
return $r; | |
} | |
if(isset($_POST['submit'])) | |
{ | |
$a = array_map('intval', preg_split('/ /', rtrim($_POST['n1']), -1, PREG_SPLIT_NO_EMPTY)); | |
$b = array_map('intval', preg_split('/ /', rtrim($_POST['n2']), -1, PREG_SPLIT_NO_EMPTY)); | |
$result = implode(" ", compareTriplets($a, $b)); | |
} | |
?> | |
<form method="post"> | |
<table align="center"> | |
<tr> | |
<td><strong><?php echo $result; ?><strong></td> | |
</tr> | |
<tr> | |
<td>Enter 1st Number</td> | |
<td><input type="text" name="n1"></td> | |
</tr> | |
<tr> | |
<td>Enter 2nd Number</td> | |
<td><input type="text" name="n2"></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td><input type="submit" name="submit" value=" = "></td> | |
</tr> | |
</table> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment