Javascript - Better Alternate to Recursive call - javascript

I have this following problem.
I need to calculate a number x based on time t, x would be represented as M(t).
We have the following
M(0) = 1
M(1) = 1
M(2) = 2
M(2t) = M(t) + M(t + 1) + t (for t > 1)
M(2t + 1) = M(t - 1) + M(t) + 1 (for t >= 1)
With that being said the first thing i had in mind to implement this is by using recursion
function CalculateForTime(t) {
if (t == 0 || t == 1) {
return 1;
}
else if (t == 2) {
return 2;
}
else if (t % 2 == 0) {
t = t / 2;
return CalculateForTime(t) + CalculateForTime(t + 1) + t;
}
else {
t = (t - 1) / 2;
return CalculateForTime(t - 1) + CalculateForTime(t) + 1;
}
}
This works however it breaks when running on a large number t for example 1^20
I tried looking into tail call recursion or substituting the recursion approach to an iteration approach but couldn't really figure it out.
If tail recursion or iteration is the way to go then please I need help on converting this. If not then I'm open to different methods on making this more optimized.
Thank you,
Omar.

You could use a hash table, because for an array it would generate holes with no value.
function calculateForTime(t) {
var k = t;
if (k in lookup) {
return lookup[k];
}
if (t == 0 || t == 1) {
return lookup[k] = 1;
}
if (t == 2) {
return lookup[k] = 2;
}
if (t % 2 == 0) {
t = t / 2;
return lookup[k] = calculateForTime(t) + calculateForTime(t + 1) + t;
}
t = (t - 1) / 2;
return lookup[k] = calculateForTime(t - 1) + calculateForTime(t) + 1;
}
var lookup = {};
console.log(calculateForTime(1e10));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could store the values in an array,then theres no need to recalc...
var times=[1,1,2];
function CalculateForTime(t) {
t = Math.floor(t / 2);
return times[t]||(times[t]=CalculateForTime(t) + CalculateForTime(t + 1) + t);
}
console.log(
CalculateForTime(100),
CalculateForTime(1000),
CalculateForTime(10000),
);
console.log(times.slice(0,100));

You could use memoization to avoid recalculating the same values again and again. See https://addyosmani.com/blog/faster-javascript-memoization/
This is the same as what others suggested, except that it separates the algorithm from the caching of values.
function memoize(func){
var cache = {};
return function( arg ){
if(arg in cache) {
return cache[arg];
} else {
return cache[arg] = func( arg );
}
}
}
// Overwrite with a function that remember previous results
CalculateForTime = memoize(CalculateForTime);
Pardon any typos, answered from phone

Related

how to avoid using recursive function

I found an question and would like to try if I can write a better function without using recursive function and while loop.
But I found that I have no idea how to write it better. Is there anyone who can give me some hints or inspire me.
function recursivefunction(i, val) {
if (!val) val= 0;
if (i < 2) throw new Error('wrong input');
if (i === 2) return 1 / i + val;
return recursivefunction(i - 1, val+ 1 / (i * (i -1)));
}
Write a program doing the same calculation without
recursion.
function recursivefunction(i, val) {
if (!val) val= 0;
if (i < 2) throw new Error('wrong input');
if (i === 2) return 1 / i + val;
return recursivefunction(i - 1, val+ 1 / (i * (i -1)));
}
function nonRecursiveFunction(i, val) {
if (!val) val = 0;
if (i < 2) throw new Error('wrong input');
while(i > 2) {
val = val + 1 / (i * (i -1));
i--;
}
return 1 / i + val;
}
const recursive = recursivefunction(4, 2);
const nonrecursive = nonRecursiveFunction(4, 2);
console.log(`Recusrive: ${recursive}, nonrecursive: ${nonrecursive}`);
To be honest, I'd replace the return statement with val + 0.5, because we know that i is exactly 2 and we can use a constant value instead of dividing here.

Float to String Conversion - JS

So I am currently working on that function
const countSixes = n => {
if (n === 0) return 0;
else if (n === 1) return 1;
else n = (countSixes(n-1) + countSixes(n-2)) / 2;
return n;
}
And so my question is how to convert the final floating-point value into a string?
Every time after calling the function and trying to convert the float number it returns NaN
What I've Tried
"" + value
String(value)
value.toString()
value.toFixed(2)
Hope to get the answer
Thank you!
The first option works for me
<script>
const countSixes = n => {
if (n === 0) return 0;
else if (n === 1) return 1;
else n = (countSixes(n-1) + countSixes(n-2)) / 2;
return n;
}
alert(countSixes(12) + "")
</script>
The problem is really interesting. Its return NaN because when you return n as String, as the function is called recursively so it cannot perform arithmetic operations in next level.
It will never end for certain numbers like 55
function countSixes(n,firstTime=true){
if (n === 0) return 0;
else if (n === 1) return 1;
else n = (countSixes(n-1,false) + countSixes(n-2,false)) / 2;
if(firstTime) return n.toFixed(10); // return string
else return parseFloat(n.toFixed(10)); // return float
}
You could convert the final value to a string with the wanted decimals.
const countSixes = n => {
if (n === 0) return 0;
if (n === 1) return 1;
return (countSixes(n - 1) + countSixes(n - 2)) / 2;
}
console.log(countSixes(30).toFixed(15));

Min heap in javascript

Hey I tried to implement a min heap in javascript, but i had a question regarding the algorithm for remove min. I'm using an array to represent the heap internally. When I'm percolating downwards, what should be the stopping condition? In my code I have it at 2 * k <= this.size so it would travel down to potentially the very last element, but it doesn't feel "correct", is there a better stopping condition? Thanks in advance!
this.removeMin = function () {
//replace root with last element and percolate downwards
var min = this._heap[1],
k,
left,
right;
this._heap[1] = this._heap.pop();
this.size--;
k = 1;
while ( ( 2 * k ) <= this.size) {
left = 2 * k;
right = 2 * k + 1;
if (this._heap[k] > this._heap[left] && this._heap[k] > this._heap[right]) {
if (this._heap[left] <= this._heap[right]) {
swap(this._heap, k, left);
k = left;
} else {
swap(this._heap, k, right);
k = right;
}
} else if (this._heap[k] > this._heap[left]) {
swap(this._heap, k, left);
k = left;
} else {
swap(this._heap, k, right);
k = right;
}
}
return min;
};
This is a much easier implementation, I hope this can help.
class MinHeap {
constructor(array) {
this.heap = this.buildHeap(array);
}
// O(n) time | O(1) space
buildHeap(array) {
const firstParentIdx = Math.floor((array.length - 2) / 2);
for (let currentIdx = firstParentIdx; currentIdx >= 0; currentIdx--) {
this.siftDown(currentIdx, array.length - 1, array);
}
return array;
}
// O(log(n)) time | O(1) space
siftDown(currentIdx, endIdx, heap) {
let childOneIdx = currentIdx * 2 + 1;
while (childOneIdx <= endIdx) {
const childTwoIdx = currentIdx * 2 + 2 <= endIdx ? currentIdx * 2 + 2 : -1;
let idxToSwap;
if (childTwoIdx !== -1 && heap[childTwoIdx] < heap[childOneIdx]) {
idxToSwap = childTwoIdx;
} else {
idxToSwap = childOneIdx;
}
if (heap[idxToSwap] < heap[currentIdx]) {
this.swap(currentIdx, idxToSwap, heap);
currentIdx = idxToSwap;
childOneIdx = currentIdx * 2 + 1;
} else {
return;
}
}
}
// O(log(n)) time | O(1) space
siftUp(currentIdx, heap) {
let parentIdx = Math.floor((currentIdx - 1) / 2);
while (currentIdx > 0 && heap[currentIdx] < heap[parentIdx]) {
this.swap(currentIdx, parentIdx, heap);
currentIdx = parentIdx;
parentIdx = Math.floor((currentIdx - 1) / 2);
}
}
// O(1) time | O(1) space
peek() {
return this.heap[0];
}
// O(log(n)) time | O(1) space
remove() {
this.swap(0, this.heap.length - 1, this.heap);
const valueToRemove = this.heap.pop();
this.siftDown(0, this.heap.length - 1, this.heap);
return valueToRemove;
}
// O(log(n)) time | O(1) space
insert(value) {
this.heap.push(value);
this.siftUp(this.heap.length - 1, this.heap);
}
swap(i, j, heap) {
[heap[i], heap[j]] = [heap[j], heap[i]];
}
}
I think you miss one if condition. When the k element is both less than the right and the left, the downwards must stop.
It must be:
if (this._heap[k] > this._heap[left] && this._heap[k] > this._heap[right]) {
if (this._heap[left] <= this._heap[right]) {
swap(this._heap, k, left);
k = left;
} else {
swap(this._heap, k, right);
k = right;
}
} else if (this._heap[k] > this._heap[left]) {
swap(this._heap, k, left);
k = left;
} else if(this._heap[k] < this._heap[right]) {
swap(this._heap, k, right);
k = right;
}else{
break;
}
Why you are writing compare code again and again. Sharing below code for reference which will optimize your code, you can replace it with your while loop.
.....
while (2 * k <= this.size) {
let j = 2 * key;
if (j < this.size && less(j, j + 1)) j++; // find smallest child
if (!less(k, j)) break; // check parent is lesser than smallest child or not
exch(k, j); //if parent is bigger then exchange
k = j; //keep checking untill reaches to the end(this.size)
}
.....
function less(i, j){
if(this._heap[j] < this._heap[i] < 0) return true;
else return false;
}
function exch(i, j){
let temp = this._heap[i];
this._heap[i] = this._heap[j];
this._heap[j] = temp;
}
Hope this works.

Function returns undefined despite the variable having a value

Here is the code. You can test it for yourself.
Please explain :)
var factorial = 1;
function factorialize(num) {
factorial *= num;
if (num == 1) {
var result = factorial;
return result;
}
factorialize(num-1);
}
factorialize(5);
It needs no global variable and no local variable, too.
function factorialize(num) {
if (num === 1) {
return 1;
}
return num * factorialize(num - 1);
}
console.log(factorialize(5));
// or a very short version:
function f(n) { return +!~-n || n * f(n - 1); }
console.log(f(10));
You don't need some variables if you use recursion. That's one of the most interesting things about recursion.
Take a look at this much shorter recursive solution:
function factorial(n)
{
return (n === 1) ? 1 : n * factorial(n - 1);
}
for (var i = 1; i <= 7; i++)
document.getElementById("myDiv").innerHTML += (i + "! = " + factorial(i) + "<br/>");
<div id="myDiv">
</div>

Damerau-Levenshtein distance Implementation

I'm trying to create a damerau-levenshtein distance function in JS.
I've found a description off the algorithm on WIkipedia, but they is no implementation off it. It says:
To devise a proper algorithm to calculate unrestricted
Damerau–Levenshtein distance note that there always exists an optimal
sequence of edit operations, where once-transposed letters are never
modified afterwards. Thus, we need to consider only two symmetric ways
of modifying a substring more than once: (1) transpose letters and
insert an arbitrary number of characters between them, or (2) delete a
sequence of characters and transpose letters that become adjacent
after deletion. The straightforward implementation of this idea gives
an algorithm of cubic complexity: O\left (M \cdot N \cdot \max(M, N)
\right ), where M and N are string lengths. Using the ideas of
Lowrance and Wagner,[7] this naive algorithm can be improved to be
O\left (M \cdot N \right) in the worst case. It is interesting that
the bitap algorithm can be modified to process transposition. See the
information retrieval section of[1] for an example of such an
adaptation.
https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
The section [1] points to http://acl.ldc.upenn.edu/P/P00/P00-1037.pdf which is even more complicated to me.
If I understood this correctly, it's not that easy to create an implementation off it.
Here's the levenshtein implementation I currently use :
levenshtein=function (s1, s2) {
// discuss at: http://phpjs.org/functions/levenshtein/
// original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// bugfixed by: Onno Marsman
// revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// reimplemented by: Alexander M Beedie
// example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
// returns 1: 3
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len;
}
if (s2_len === 0) {
return s1_len;
}
// BEGIN STATIC
var split = false;
try {
split = !('0')[0];
} catch (e) {
// Earlier IE may not support access by string index
split = true;
}
// END STATIC
if (split) {
s1 = s1.split('');
s2 = s2.split('');
}
var v0 = new Array(s1_len + 1);
var v1 = new Array(s1_len + 1);
var s1_idx = 0,
s2_idx = 0,
cost = 0;
for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) {
v0[s1_idx] = s1_idx;
}
var char_s1 = '',
char_s2 = '';
for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) {
v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
char_s1 = s1[s1_idx];
cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx + 1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) {
m_min = b;
}
if (c < m_min) {
m_min = c;
}
v1[s1_idx + 1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp;
}
return v0[s1_len];
}
What are your ideas for building such an algorithm and, if you think it would be too complicated, what could I do to make no difference between 'l' (L lowercase) and 'I' (i uppercase) for example.
The gist #doukremt gave: https://gist.github.com/doukremt/9473228
gives the following in Javascript.
You can change the weights of operations in the weighter object.
var levenshteinWeighted= function(seq1,seq2)
{
var len1=seq1.length;
var len2=seq2.length;
var i, j;
var dist;
var ic, dc, rc;
var last, old, column;
var weighter={
insert:function(c) { return 1.; },
delete:function(c) { return 0.5; },
replace:function(c, d) { return 0.3; }
};
/* don't swap the sequences, or this is gonna be painful */
if (len1 == 0 || len2 == 0) {
dist = 0;
while (len1)
dist += weighter.delete(seq1[--len1]);
while (len2)
dist += weighter.insert(seq2[--len2]);
return dist;
}
column = []; // malloc((len2 + 1) * sizeof(double));
//if (!column) return -1;
column[0] = 0;
for (j = 1; j <= len2; ++j)
column[j] = column[j - 1] + weighter.insert(seq2[j - 1]);
for (i = 1; i <= len1; ++i) {
last = column[0]; /* m[i-1][0] */
column[0] += weighter.delete(seq1[i - 1]); /* m[i][0] */
for (j = 1; j <= len2; ++j) {
old = column[j];
if (seq1[i - 1] == seq2[j - 1]) {
column[j] = last; /* m[i-1][j-1] */
} else {
ic = column[j - 1] + weighter.insert(seq2[j - 1]); /* m[i][j-1] */
dc = column[j] + weighter.delete(seq1[i - 1]); /* m[i-1][j] */
rc = last + weighter.replace(seq1[i - 1], seq2[j - 1]); /* m[i-1][j-1] */
column[j] = ic < dc ? ic : (dc < rc ? dc : rc);
}
last = old;
}
}
dist = column[len2];
return dist;
}
Stolen from here, with formatting and some examples on how to use it:
function DamerauLevenshtein(prices, damerau) {
//'prices' customisation of the edit costs by passing an object with optional 'insert', 'remove', 'substitute', and
//'transpose' keys, corresponding to either a constant number, or a function that returns the cost. The default cost
//for each operation is 1. The price functions take relevant character(s) as arguments, should return numbers, and
//have the following form:
//
//insert: function (inserted) { return NUMBER; }
//
//remove: function (removed) { return NUMBER; }
//
//substitute: function (from, to) { return NUMBER; }
//
//transpose: function (backward, forward) { return NUMBER; }
//
//The damerau flag allows us to turn off transposition and only do plain Levenshtein distance.
if (damerau !== false) {
damerau = true;
}
if (!prices) {
prices = {};
}
let insert, remove, substitute, transpose;
switch (typeof prices.insert) {
case 'function':
insert = prices.insert;
break;
case 'number':
insert = function (c) {
return prices.insert;
};
break;
default:
insert = function (c) {
return 1;
};
break;
}
switch (typeof prices.remove) {
case 'function':
remove = prices.remove;
break;
case 'number':
remove = function (c) {
return prices.remove;
};
break;
default:
remove = function (c) {
return 1;
};
break;
}
switch (typeof prices.substitute) {
case 'function':
substitute = prices.substitute;
break;
case 'number':
substitute = function (from, to) {
return prices.substitute;
};
break;
default:
substitute = function (from, to) {
return 1;
};
break;
}
switch (typeof prices.transpose) {
case 'function':
transpose = prices.transpose;
break;
case 'number':
transpose = function (backward, forward) {
return prices.transpose;
};
break;
default:
transpose = function (backward, forward) {
return 1;
};
break;
}
function distance(down, across) {
//http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
let ds = [];
if (down === across) {
return 0;
} else {
down = down.split('');
down.unshift(null);
across = across.split('');
across.unshift(null);
down.forEach(function (d, i) {
if (!ds[i]) {
ds[i] = [];
}
across.forEach(function (a, j) {
if (i === 0 && j === 0) {
ds[i][j] = 0;
} else if (i === 0) {
//Empty down (i == 0) -> across[1..j] by inserting
ds[i][j] = ds[i][j - 1] + insert(a);
} else if (j === 0) {
//Down -> empty across (j == 0) by deleting
ds[i][j] = ds[i - 1][j] + remove(d);
} else {
//Find the least costly operation that turns the prefix down[1..i] into the prefix across[1..j] using
//already calculated costs for getting to shorter matches.
ds[i][j] = Math.min(
//Cost of editing down[1..i-1] to across[1..j] plus cost of deleting
//down[i] to get to down[1..i-1].
ds[i - 1][j] + remove(d),
//Cost of editing down[1..i] to across[1..j-1] plus cost of inserting across[j] to get to across[1..j].
ds[i][j - 1] + insert(a),
//Cost of editing down[1..i-1] to across[1..j-1] plus cost of substituting down[i] (d) with across[j]
//(a) to get to across[1..j].
ds[i - 1][j - 1] + (d === a ? 0 : substitute(d, a))
);
//Can we match the last two letters of down with across by transposing them? Cost of getting from
//down[i-2] to across[j-2] plus cost of moving down[i-1] forward and down[i] backward to match
//across[j-1..j].
if (damerau && i > 1 && j > 1 && down[i - 1] === a && d === across[j - 1]) {
ds[i][j] = Math.min(ds[i][j], ds[i - 2][j - 2] + (d === a ? 0 : transpose(d, down[i - 1])));
}
}
});
});
return ds[down.length - 1][across.length - 1];
}
}
return distance;
}
//Returns a distance function to call.
let dl = DamerauLevenshtein();
console.log(dl('12345', '23451'));
console.log(dl('this is a test', 'this is not a test'));
console.log(dl('testing testing 123', 'test'));

Categories