Created
November 30, 2018 17:09
-
-
Save ammardev/1e685d8a2e90f84b238d5369762c97a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Problem 2: | |
# Function: | |
function mergeSortedSequences(array $list1, array $list2) : array { | |
$l1pointer = 0; | |
$l2pointer = 0; | |
$result = []; | |
while ($l1pointer < count($list1) && $l2pointer < count($list2)) { | |
if ($list1[$l1pointer] < $list2[$l2pointer]) { | |
array_push($result, $list1[$l1pointer]); | |
$l1pointer++; | |
} else { | |
array_push($result, $list2[$l2pointer]); | |
$l2pointer++; | |
} | |
} | |
if($l1pointer == count($list1)) | |
$result = array_merge($result, array_slice($list2, $l2pointer)); | |
elseif($l2pointer == count($list2)) | |
$result = array_merge($result, array_slice($list1, $l1pointer)); | |
return $result; | |
} | |
# Function Usage: | |
echo 'Insert the first list: '; | |
$list1 = json_decode(fgets(fopen("php://stdin","r"))); | |
echo 'Insert the second list: '; | |
$list2 = json_decode(fgets(fopen("php://stdin","r"))); | |
echo json_encode( mergeSortedSequences($list1, $list2) ), PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment