Monday, September 19, 2016

Permutation of String in JavaScript

A simple algorithm to find the permutation of a string in JavaScript.
/**
 * Find the permutations of the given string.
 * @param s The input string
 *
 * Algorithm:
 * 1. If the length of the string is 1, return the string as permutation of 'a' is 'a'
 * 2. Else for the given string find the last character char and the substring by stripping the last char
 *    2.1 Recurse with the substring
 * 3. For each permutation from step 2 add the char to each position of the substring and return
 */
function permute(s) {
    var len = s.length, char, i = 0, j = 0, p = [], elem, pArr = [], plen = 0;
    
    if (len == 1) return s;
    
    char = s.charAt(len-1);
    pArr = permute(s.substring(0, len-1));
    plen = pArr.length;

    for (i = 0; i < plen; i++) {
        for (j = 0; j < len; j++) {
            elem = pArr[i].split("");
            elem.splice(j, 0, char);
            p.push(elem.join(""));
        }
    }
    return [...new Set(p)];
}
permute("abc"); // [ 'cba', 'bca', 'bac', 'cab', 'acb', 'abc' ]
Download from github.

Sunday, September 18, 2016

Make a Copy of an Array in JavaScript

If you want to make a copy of an array in JavaScript, you can use Array#slice method. This way we will get a copy of the array. If we are simply assigning the array to another variable, it is not making a copy. So performing splice() operation on the array will affect the value stored in both variables.
var a = [1,2,3],
    b = a;
a.splice(1,1); // => a is [1,3] and b is also [1,3]
var a = [1,2,3],
    b = a.slice(0);
a.splice(1,1); // => a is [1,3] and b is [1,2,3]

Thursday, September 15, 2016

jQuery & AngularJS Tech Talk Slides

These are presentation slides of jQuery and AngularJS - A Brief Introduction for a tech talk conducted at TCS, Trivandrum DC.


Saturday, September 3, 2016

Override Properties File Using Classpath

Say you have a config.properties file containing various configuration details. This file is placed under the resources folder and loaded using classpath. Then you can override the file during runtime by specifying a different file to the classpath command line argument. Let's say that the main class is Main.java inside com.example.test package and the test.jar is our program. Use the following command to override the config.properties file.
java -classpath .;test.jar;config.properties; com.example.test.Main

Thursday, November 26, 2015

Remove Array Duplicates in ES6

We can use Set and spread operator to remove duplicates from an array.
[...new Set([1,2,3,1,2,3])];  // [1,2,3]
Array.from(new Set([1,2,3,1,2,3]));  // [1,2,3]

Thursday, May 14, 2015

Orientation Lock Cordova Plugin for Android

OrientationPlugin is a cordova plugin that allows to lock the app into portrait, landscape modes and is available from github.

Wednesday, February 4, 2015

jQuery Mobile Fixed Footer Jumps Up On Input Focus

In Android jQuery mobile's fixed footer goes up from bottom on input focus. The footer goes up when the virtual keyboard shows. The bug can be fixed by hiding the footer on input focus and displaying footer on focusout.
$(document).on('focus', 'input, textarea', function()  {
    $.mobile.activePage.find("div[data-role='footer']").hide();
});
$(document).on('focusout', 'input, textarea', function()  {
    $.mobile.activePage.find("div[data-role='footer']").show();
});