Created
July 5, 2016 22:40
-
-
Save bhu1st/4501aa84df92632caa9f54e142007b9e 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 | |
// This is the text editor interface. | |
// Anything you type or change here will be seen by the other person in real time. | |
// You are given tasks which can be identified as A, B, ... | |
// Execution of eack task takes tA, tB, ... time | |
// If you execute task A, you have to wait dA time before executing task A again | |
// If you are given a sequence of tasks and asked to execute sequentially, how long would it take to execute them? | |
// | |
// A B C A A D B | |
// | |
// tA = 5 | |
// tB = 1 | |
// tC = 1 | |
// tD = 2 | |
// | |
// dA = 5 | |
// dB = 2 | |
// dC = 2 | |
// dD = 3 | |
// | |
// 5(tA) + 1(tB) + 1(tC) + 3(dA:5 - 2) + 5(tA) + 5(dA) + 5(tA) + 2(tD) + 1(tB) = 28 | |
// A + 1 + 1 + waiting 3 + A | |
//array signature for each task : taskname, executionTime, waitTime | |
$tasksInfo = array( | |
array('A', 5, 5), | |
array('B',1,2), | |
array('C', 1,2), | |
array('D', 2, 3) | |
); | |
$inputTaskSequence = array ('A', 'B','C', 'A', 'A', 'D', 'B'); | |
// task#1: AA | |
// task#2: BB | |
// task#3: CCCCC | |
// . | |
// 1000s of distinct tasks | |
function array_search_custom ($task, $taskInfo) { //search multidimentional array for values | |
foreach ($taskInfo as $key => $t) { | |
if ($t[0] === $task) return $key; | |
} | |
return false; | |
} | |
function taskExecutionTime($input, $taskCount, $tasksInfo){ | |
$time = 0; | |
$taskStack = array (); | |
// 5(tA) + 1(tB) + 1(tC) + 3(dA:5 - 2) + 5(tA) + 5(dA) + 5(tA) + 2(tD) + 1(tB) = 28 | |
// A + 1 + 1 + waiting 3 + A | |
for($i = 0; $i < $taskCount; $i++) { | |
$currentTask = $input[$i]; | |
echo "\nhere: ".$currentTask; | |
//// If you execute task A, you have to wait dA time before executing task A again | |
if (!empty($taskStack) && in_array($currentTask, $taskStack)) { | |
//this task was already started and is in taskStack, now need to wait before executing this again | |
//need to calculate: time elapsed since the task | |
//check if this is less than or equal to wait time | |
// simply: waitTime for this task - time elapsed since this tasks was pushed to stack | |
$elapsedTime =0; | |
$waitTime = 0; | |
echo "\n---------reverse---------"; | |
for($elapsedIndex = $i -1 ; $elapsedIndex >= 0; $elapsedIndex--) { | |
echo "\nrepeated task: " . $repeatedTask = $currentTask; | |
echo "\nprevious task: " . $previousTask = $taskStack[$elapsedIndex]; | |
echo "\n"; | |
if ($repeatedTask != $previousTask) { | |
$k = array_search_custom ($previousTask, $tasksInfo); //find info about previous task | |
print_r($tasksInfo[$k]); | |
$elapsedTime += $tasksInfo[$k][1]; //save its execution time to elapsed time | |
}else { | |
$elapsedTime += $tasksInfo[$k][2]; | |
break; | |
} | |
} | |
echo "\n---------reverse---------"; | |
// A B C A A D B | |
// 5(tA) + 1(tB) + 1(tC) + 3(dA:5 - 2) + 5(tA) + 5(dA) + 5(tA) + 2(tD) + 1(tB) = 28 | |
// 5 + 1 + 1 + waiting 3 + 5 + waiting 5 + 5 + 2 + 1 = 28 | |
$k = array_search_custom ($currentTask, $tasksInfo); | |
echo "\nElapsed time: " . $elapsedTime . PHP_EOL; | |
if ($tasksInfo[$k][2] > $elapsedTime) { | |
$waitTime = $tasksInfo[$k][2] - $elapsedTime; //waitTime - timeElapsed | |
} else { | |
$waitTime += $elapsedTime; //$tasksInfo[$k][1]; | |
} | |
$time += $waitTime; | |
}else { | |
//find task by Value | |
$key = array_search_custom ($currentTask, $tasksInfo); | |
echo "\nkey: " . $key . PHP_EOL; | |
$time += $tasksInfo[$key][1]; | |
print_r($tasksInfo[$key]); | |
echo "Execution time: " . $tasksInfo[$key][1] . PHP_EOL; | |
} | |
array_push($taskStack, $currentTask); | |
//print_r($taskStack); | |
echo "Total time " . $time; | |
echo PHP_EOL; | |
} | |
return $time; | |
} | |
echo taskExecutionTime($inputTaskSequence, count($inputTaskSequence), $tasksInfo); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment