Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Recursions are nasty in JavaScript, because the maximum recursion depth is pretty low on some platforms: http://stackoverflow.com/a/7828803/2879325https://stackoverflow.com/a/7828803/2879325

Recursions are nasty in JavaScript, because the maximum recursion depth is pretty low on some platforms: http://stackoverflow.com/a/7828803/2879325

Recursions are nasty in JavaScript, because the maximum recursion depth is pretty low on some platforms: https://stackoverflow.com/a/7828803/2879325

Ensure that the correct overload of `toString` is called. Should the caller try on to pass anything else but a function, it's an error.
Source Link
Ext3h
  • 2.9k
  • 13
  • 17
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(functorFunction.prototype.toString.call(functor), e, expected);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(functorFunction.prototype.toString.call(functor), result, expected);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(functor.toString(), e, expected);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(functor.toString(), result, expected);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(Function.prototype.toString.call(functor), e, expected);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(Function.prototype.toString.call(functor), result, expected);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(functor.toSourcetoString(), e, expextedexpected);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(functor.toSourcetoString(), result, expextedexpected);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(functor.toSource(), e, expexted);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(functor.toSource(), result, expexted);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
function test(functor, expected) {
  var result;
  try {
    result = functor();
  } catch(e) {
    console.error(functor.toString(), e, expected);
  }
  // Object comparison hack
  if (JSON.stringify(result) !== JSON.stringify(expected)) {
    console.error(functor.toString(), result, expected);
  }
}

// O(N^2)
test(function (){return heightSlow([4, -1, 4, 1, 1])}, 3);
test(function (){return heightSlow([-1, 0, 4, 0, 3])}, 4);

// Theta(N)
test(function (){return heightFast([4, -1, 4, 1, 1])}, 3);
test(function (){return heightFast([-1, 0, 4, 0, 3])}, 4);
added 339 characters in body
Source Link
Ext3h
  • 2.9k
  • 13
  • 17
Loading
added 138 characters in body
Source Link
Ext3h
  • 2.9k
  • 13
  • 17
Loading
Source Link
Ext3h
  • 2.9k
  • 13
  • 17
Loading