POW – True, False & Random

This Problem of the Week is another logic problem. It is a variation of the famous puzzle off of which the True and False POW was based. The answer to this question will not be revealed until Sunday at 12:00 AM EST can be found here.

Problem of the Week

You are a contestant on a game show. You have the opportunity to ask three questions to three androids: A, B, and C. Each question that you ask may only be directed to one android. One of these androids always responds truthfully, one of them always responds falsely, and the third always responds randomly; thus their names are True, False and Random. Although they understand every human language and can respond without hesitation, they will only respond in their own language. The following are two words in their language: da and su. One of these words means yes while the other means no.  In order to make it easier for you, they will only answer yes or no questions. Since you don’t know which word is which or even which android is which, what three questions could you ask in order to determine the identity of the three androids?

POW – Unnamed Function #1

This Problem of the Week involves determining the identity of a well-known mathematical function by examining the algorithm used on the input number to produce the output.  The function will only accept integers that are greater than or equal to zero. The following is the mathematical definition of the function:

Function Definition Condition
unnamed(n) = 1 if n = 0
n × unnamed(n – 1) if n > 0

If you are a JavaScript/JScript person, here is the equivalent JavaScript code for this unnamed function:

function unnamed(n) {
  if(n == 0)
    return 1;
  if(n > 0)
    return n * unnamed(n - 1);
}

With the above definitions for this unnamed function, what mathematical function would you say this function is equivalent to?

The answer to this Problem of the Week was posted on February 13, 2012 at 12:00 AM (EST) and can be found here.

POW – Working Together

Both Don and Juan and are given a task on which they must work together to complete in a timely fashion. It takes Don 75 minutes to do the job by himself. It takes Juan 60 minutes to do the job by himself.

  1. At their speed, how long would it take both Don and Juan to work together to get the task done? What formula could you use if you had to substitute each person’s time for a letter?
  2. If Guadalupe, who takes 68 minutes, also helps, how long would it take all three to work together to get the task done? What formula could you use if you had to substitute each person’s time for a letter?

The answer for this Problem of the Week can be found here.

JavaScript – String.prototype.expandTabs() Revisited

New & Improved Definition

Last year I wrote a version of this function which was to mimic the Python expandtabs function. The funny thing is I didn’t remember that I wrote the function in the first place. Therefore while working on a project, I wrote the following function which is actually shorter and faster than the original:

String.prototype.expandTabs = function(tabSize) {
  var spaces = (new Array((tabSize = tabSize || 8 ) + 1)).join(" ");
  return this.replace(/([^\r\n\t]*)\t/g, function(a, b) {
    return b + spaces.slice(b.length % tabSize);
  });
};

Minimum Tab Size

If for some reason the above function, although it mimics my previous version (except it doesn’t limit the tab size to 32), is lacking in your mind because it doesn’t provide a way of specifying the minimum tabSize, you can alternatively use the following function definition:

String.prototype.expandTabs = function(tabSize, minTabSize) {
  tabSize = tabSize || 8;
  minTabSize = minTabSize || 1;
  var spaces = (new Array(Math.ceil((minTabSize - 1) / tabSize) * tabSize + tabSize + 1)).join(" ");
  return this.replace(/([^\r\n\t]*)\t/g, function(a, b) {
    return b + spaces.slice(0, Math.ceil((b.length + minTabSize) / tabSize) * tabSize - b.length);
  });
};

Although the above definition is more complete, I feel like there is a better calculation for the amount of spaces that I am simply overlooking. All-in-all, I am happy with both of these newer versions of String.prototype.expandTabs(). I guess having a bad memory has its advantages at times. ;)

JavaScript – Set Default Function Parameter Values

It has been a while since I wrote a JavaScript utility function so a few minutes ago, I had the idea to create a function which will allow you to set the default value of a parameter for any given function. After writing the following function, I thought about the fact that the next version of jPaq already has this partially defined:

Function.prototype.defaultTo = function() {
  var fn = this, defaults = arguments;
  return function() {
    var args = arguments,
      len = arguments.length,
      newArgs = [].slice.call(defaults, 0),
      overrideAll = args[len - 1] === undefined;
    for(var i = 0; i < len; i++) {
      if(overrideAll || args[i] !== undefined) {
        newArgs[i] = args[i];
      }
    }
    return fn.apply(this, newArgs);
  };
};

The reason this function is different is because it will allow you to override defaults with any value—even undefined. Let’s see a few examples that prove that this works the way I claim it does:

// A function that takes one value and simply spits it back out.
// If nothing is passed in, false will be returned.
var inOut = function(value) {
  return value;
}.defaultTo(false);

// Test it out:
alert(inOut());  // returns false
alert(inOut(Math.PI));  // returns the value of PI
alert(inOut("Hello world!!!"));  // returns "Hello world!!!"
alert(inOut(null));  // returns null
alert(inOut(undefined));  // returns undefined

If you try out the above code after using the definition for the defaultTo(), you will find that every value that was passed in was also returned. In addition, when nothing was passed in, false was returned as specified by the default. The one question that may linger is, how is it that you can pass in undefined and the default is still overridden? The answer is that whenever undefined is passed in as the last argument, all defaults are overridden with the specified parameters. Now that you no the ins and outs of this function, you can start setting defaults for all of your functions without a problem.