need help coming up with an efficient algorithm for the below.
I have several variables.
Boolean Variable S = Start Step Sequence - True/False
Integer Variable R = Total Number of Recipes - Constant of 50
Integer Variable X = Number of Recipe Steps - Can be 1 to (Variable R)
Integer Variable Y = Starting Recipe Step - Can be 1 to (Variable R)
Integer Variable Z = Ending Recipe Step - Can be 1 to (Variable R)
Integer Variable C = Current Recipe Step - Can be 1 to (Variable R)
Integer Variable T = Time Duration - Constant 60 seconds
Boolean Variable D = Steps Done - True/False
So I am working on a problem where the end user can Change either Variable X and Y.
At minimum i need an algorithm that can look at the Number of steps (Variable X) and then take the input of the starting Step (Variable Y) and then Calculate the Ending Step (Variable Z). If the steps cause Variable Z to go past 50 (Variable R) then is starts back at 1 and increments the value up accordingly.
Once Start Step Sequence (Variable S) is set to True then the algorithm will need to start at Variable Y and assign this to the Current Recipe Step (Variable C) then every Time Duration (Variable T) it needs to increment Variable C up by 1 to the next step. If the Ending Step (Variable Z) is such that it is less than the starting step (Variable Y) due to the to the number of steps causing the value to roll over then the same thing would need to happen with variable C and once at a value of 50 (Variable R) and more increments are required it would roll over to 1 and keep incrementing up until the number of steps is completed meaning Variable C is equal to Variable Z and the duration of the final step has expired. THen once the final step duration has completed flag Steps Done (Variable D) to True and set Variable S to flase.
I have made the algorithm in JavaScript as it can run inside your browser as a snippet which is useful for testing the output of the code in this answer.
Also, I have declared many variables with longer names (ie current_step = c
) as well as the one-letter names just for clarity. I know this makes the code much longer than necessary, but improves readability.
Here is the snippet:
let s = true; // Whether to start sequence
const r = 50; // Total number of steps
let x; // Number of recipe steps 1 -> r
let y; // Starting recipe step 1 -> r
let z; // Ending recipe step 1 -> r
let c; // Current recipe step 1 -> r
const t = 60; // Time duration in seconds
let d = false; // Whether steps are done
async function algorithm(options) {
return new Promise(resolve_entire_algorithm => {
const total_steps = options.r;
const number_of_steps = options.x;
const starting_step = options.y;
let ending_step = starting_step + number_of_steps;
while(ending_step > /*Math.min(starting_step,number_of_steps,*/total_steps/*)*/) ending_step -= 50;
if(!options.s) {
return {s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:options.c,t:options.t,d:options.d};
};
let current_step = starting_step;
let time_duration = options.t;
let completed_all_steps = options.d;
async function complete_step() {
return new Promise(resolve => {
if(completed_all_steps) {
resolve(true);
return;
};
if(current_step > number_of_steps && ending_step < starting_step && ending_step < number_of_steps) c = 1;
if(current_step > total_steps) current_step -= total_steps;
if(current_step == ending_step) {
completed_all_steps = true;
};
console.log(current_step, ending_step, completed_all_steps);
setTimeout(() => {
resolve(complete_step);
current_step++;
}, time_duration*10);
});
};
async function listen(func) {
const result = await func();
if(result === true) {
current_step--;
resolve_entire_algorithm({
s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:current_step,t:options.t,d:completed_all_steps
});
return;
} else if(typeof func == "function") {
console.log("Not completed (even if current_step==ending_step, you still have to wait until the current_step has expired. Takes (T) seconds per step, including the last step.)");
listen(func);
};
};
listen(complete_step);
});
};
const initial_settings = {s,r,x:30,y:30,z,c,t,d};
algorithm(initial_settings)
.then(output_settings => {
console.log("\n\n...\nAlgorithm finished.\n\n\nOriginal:\n",initial_settings, "\n\nResult:\n",output_settings);
});
time_duration*10
to time_duration*1000