Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Let's share your Javascript/RMMV algorithm challenges here

DoubleX

Adventurer
Xy$
1.11
This post aims to incite you to share some algorithm challenges, so we can learn from those challenges, seek for answers for difficult problems we're facing, and hopefully apply their answers in case we encounter similar issues.
Let's start by sharing mine:

Challenge 1: Finding Array Elements

Intended difficulty level
- 2(Little Javascript coding proficiency)

Situation - Consider the below piece of code:
JavaScript:
/* Returns null if array's empty
    Returns the element in array with the smallest property num that's just
    larger than num if sign equals to 1
    Returns the element in array with the largest property num that's just
    smaller than num if sign equals to -1
    If there's more than 1 such elements, return any of them
    If there's no such elements, return the element in array with the smallest
    property num if sign equals to 1
    If there's no such elements, return the element in array with the largest
    property num if sign equals to -1
    No argument will be mutated */
function find_min_max_num_prop(array, num, sign) {
     // Writes your codes here only

     //
}; // find_min_max_num_prop
It's given that:
- array is an Array
- num is a Number
- sign is always either 1 or -1
- Every element in array has a property num, which is a Number

Goal -
Returns null if array's empty
Returns the element in array with the smallest property num that's just larger than num if sign equals to 1.
Returns the element in array with the largest property num that's just smaller than num if sign equals to -1.
If there's more than 1 such elements, return any of them.
If there's no such elements, return the element in array with the smallest property num if sign equals to 1.
If there's no such elements, return the element in array with the largest property num if sign equals to -1.
No argument will be mutated.

Condition - Your codes must always be 100% correct in this situation.

Constraint - You can only add new code in find_min_max_num_prop(although you can add anything you want).

Challenge 2: Eliminating Repeated Executions

Intended difficulty level - 3(Some Javascript coding proficiency)

Situation - Consider the below piece of code:
JavaScript:
CustomObjManager = {
     throw new Error("This is a static class");
}

//-------------------------------------------------------------------//
// Eliminates repeated do_something execution for all custom objects //
//-------------------------------------------------------------------//
// obj: The current custom object executing do_something
// objs: An array of all custom objects
// active_objs: An array of all active custom objects
CustomObjManager.do_something_once = function(obj, objs, active_objs) {
     if this.do_something_cond() {
     // Write your codes here only
     //
     }
} // CustomObjManager.do_something_once

// objs: An array of all custom objects
// active_objs: An array of all active custom objects
CustomObj.prototype.do_something = function(objs, active_objs) {
     this.do_something_part_1();
     CustomObjManager.do_something_once(this, objs, active_objs)
     this.do_something_part_2();
} // CustomObj.prototype.do_something
It's given that:
- All custom objects are prototypes of CustomObj.
- objs and active_objs are arrays of all custom objects and all active custom objects respectively.
- Everything's run by 1 single thread only(i.e.: no multithreading, concurrent computing, parallel programming, etc, so there's no need to worry about race conditions).
- When 1 special event starts, each active custom object will call do_something, but any other custom object(i.e., non active custom object) will only call do_something via do_something_once, which can only be called via do_something.
- do_something_cond will always return the same result during any such special event.

Goal - Ensures that no active custom object will call do_something more than once during any such special event, and each non active custom object will also call do_something exactly once during that special event if and only if do_something_cond returns true.

Condition - Your codes must always be 100% correct in this situation.

Constraint - You can only add new codes in parts specified by do_something_once(although you can add anything whenever you want).

I'll continue to post more and more. Let's share yours, so we can all benefit from each other here.
 
Last edited:

eivl

Local Hero
Xy$
0.00
Can you please make something clear for me.
Does Share your javascript algorithm challenges here mean : Here is a problem and how can i make a JS solution on it?

Let me give you an example.

Returns the element in array with the smallest property num that's just
larger than num if sign equals to 1
what do you want to have returned? because you write smallest property num, this does not make sense, do you mean return the smallest number from an array of numbers?
 

DoubleX

Adventurer
Xy$
1.11
Can you please make something clear for me.
Does Share your javascript algorithm challenges here mean : Here is a problem and how can i make a JS solution on it?

Let me give you an example.



what do you want to have returned? because you write smallest property num, this does not make sense, do you mean return the smallest number from an array of numbers?
The elements of array aren't themselves Number, but each of those element has a property num, which is a Number.
So the property num of the ith element of array is array.num, which is a Number.
 

DoubleX

Adventurer
Xy$
1.11
does this help with understand content
Do you mean the default RMMV codebase? If so, then the first 2 challenges doesn't, but later there can be some challenges that needs understanding to the default RMMV codebase.

Edit:
My Solution to challenge 1
JavaScript:
/* Returns null if array's empty
Returns the element in array with the smallest property num that's just
larger than num if sign equals to 1
Returns the element in array with the largest property num that's just
smaller than num if sign equals to -1
If there's more than 1 such elements, return any of them
If there's no such elements, return the element in array with the smallest
property num if sign equals to 1
If there's no such elements, return the element in array with the largest
property num if sign equals to -1
No argument will be mutated */
function find_min_max_num_prop(array, num, sign) {
// Writes your codes here only
if (array.length == 0) {
return null;
};
var temp_array = [];
for (index = 0; index < array.length; index++) {
temp_array.push(array[index]);
}
temp_array.sort(function(a, b) { return (a.num - b.num) * sign });
for (index = 0; index < array.length; index++) {
if (temp_array[index].num * sign > num * sign) {
return temp_array[index]
}
}
return temp_array[0]
//
} // find_min_max_num_prop
It achieves the goal, meets the condition and conform to the constraint.
Proof:
It doesn't mutate any passed argument as temp_array is the only mutated variable, which isn't linked to any passed ones.
It's always 100% correct because -
It returns null if array's empty.
temp_array's always sorted in ascending and descending order if sign's equal to 1 and -1 respectively.
If the ith element's large/smaller than num for sign being 1/-1, so do all the (i + j)th elements.
So it's sure that that ith element's the smallest/largest one that's just larger/smaller than num for sign begin 1/-1.
If no such element's found, the 1st element, which must be the smallest/largest element for sign being 1/-1, will be returned.

Challenge 3: Tracing Object Properties
Intended difficulty level - 4(Decent javascript coding proficiency)

Situation - Consider the plugin function, plugin call and configuration parts of DoubleX RMMV Object Properties:
JavaScript:
// Traces all object properties meeting some conditions linked to the //
// queried object //
// Designed as a bug diagnosis tool used by Javascript coders with debug //
// experience //
JavaScript:
//============================================================================//
// ** Plugin Call Info //
// A path in the object property trace will stop if it'd be cyclic //
//----------------------------------------------------------------------------//
// * Object manipulations //
// 1. trace_obj_prop(cond, label) //
// - Traces all object properties satisfying function cond linked to //
// this object //
// - Labels all traced object properties with function label //
// - cond and label are functions written in //
// Object Property Tracing Condition Function and //
// Object Property Tracing Label Function respectively //
// 2. _obj_prop_log[cond] //
// - Returns the log of all traced object properties satisfying //
// function cond linked to this object //
//============================================================================//
JavaScript:
//------------------------------------------------------------------------//
// Object Property Tracing Condition Function //
// - Setups cond used by trace_obj_prop(cond, label) //
//------------------------------------------------------------------------//
// cond must be a function taking the object property as the only argument
// The below examples are added to help you setup your own cond functions

// Checks if the currently traced object's indeed an object
cond_obj: function(obj) {
return typeof obj === "object";
}, // substitute cond with "cond_obj" to use this function

// Checks if the currently traced object's an array
cond_array: function(obj) {
return Array.isArray(obj);
}, // substitute cond with "cond_array" to use this function

// Add your own cond functions here


//------------------------------------------------------------------------//
// Object Property Tracing Label Function //
// - Setups label used by trace_obj_prop(cond, label) //
//------------------------------------------------------------------------//
// label must be a function taking the object property as the only argument
// All label functions must return a string
// The below examples are added to help you setup your own label functions

// Always returns the entire object
label_obj: function(obj) {
return obj;
}, // substitute label with "label_obj" to use this function

// Always returns the type(including Array) of each traced object property
label_array: function(obj) {
if (Array.isArray(obj)) {
return "array";
}
return typeof obj;
}, // substitute label with "label_array" to use this function

// Add your own label functions here
The following parts are already implemented:
JavaScript:
// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.trace_obj_prop = function(cond, label) { // New
// Finish this function's contents
//
}; // Object.prototype.trace_obj_prop

// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.log_obj_prop = function(cond, label) { // New
// Finish this function's contents
//
}; // Object.prototype.log_obj_prop

//----------------------------------------------------------------------------//
// Label and use all nonempty subtrees to form the object property tree //
//----------------------------------------------------------------------------//
// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.traverse_obj_prop_tree = function(cond, label) { // New
// Finish this function's contents
//
}; // Object.prototype.traverse_obj_prop_tree

// prop: The current object property to be traced
Object.prototype.is_obj_prop = function(prop) { // New
// Finish this function's contents
//
}; // Object.prototype.is_obj_prop

Goal - Finish the rest of the plugin implementations to make this plugin works as intended.

Condition - Your implementations must always be 100% correct for this plugin alone except for bugs coming from the default RMMV codebase itself.

Constraint - This plugin must be able to be completely standalone.

My Hint -
JavaScript:
// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.trace_obj_prop = function(cond, label) { // New
if (this._obj_prop_trace === undefined) {
this._obj_prop_log = {};
this._obj_prop_trace = {};
}
// Adds new codes here
//
this._obj_prop_log[cond] = "";
this._obj_prop_trace[cond] = {};
this.log_obj_prop(cond, label);
}; // Object.prototype.trace_obj_prop

// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.log_obj_prop = function(cond, label) { // New
// Checks if the currently traced object property has object properties
var has_obj_prop = false;
for (prop in this) {
if (this.hasOwnProperty(prop) && this.is_obj_prop(prop)) {
has_obj_prop = true;
break;
}
}
//
if (has_obj_prop) {
this._obj_prop_log[cond] = "{";
this.traverse_obj_prop_tree(cond, label);
this._obj_prop_log[cond] += "}";
}
}; // Object.prototype.log_obj_prop

//----------------------------------------------------------------------------//
// Label and use all nonempty subtrees to form the object property tree //
//----------------------------------------------------------------------------//
// cond: The function checking whether an object property will be traced
// label: The function returning the label of an traced object property
Object.prototype.traverse_obj_prop_tree = function(cond, label) { // New
var ot = DoubleX_RMMV.Obj_Prop;
for (prop in this) {
if (this.hasOwnProperty(prop) && this.is_obj_prop(prop)) {
// Adds new codes here
//
}
}
}; // Object.prototype.traverse_obj_prop_tree
 
Top