Showing posts with label FreeCodeCamp. Show all posts
Showing posts with label FreeCodeCamp. Show all posts

Monday, June 5, 2017

Permutation in JavaScript

// Permutations

// Option 1, fix prefix, add a letter to the end of current space.


Source code:

function get_suffix(surffix, charIdx) {
  var arr = surffix.split("");
  arr.splice(charIdx, 1);
  var newSurffix = arr.join("");
  return newSurffix;
}

function permSuffix (str) {
var myArr = [];
var permNext = function f_permNext(prefix, surffix, initStr) {
 //call example:  permNext("","abc","abc");
 var newPrefix = "",
   newSurffix = "";
 var strLen = initStr.length;
 var i = 0;
 console.log("start func: " + prefix, i, surffix);
 if (prefix.length === strLen) {
   myArr.push(prefix);
   console.log("** Combined: " + prefix);
   return;
 }
 for (i = 0; i < surffix.length; i++) {
   newPrefix = prefix + surffix[i];
   newSurffix = get_suffix(surffix, i);
   console.log(newPrefix, i, newSurffix);
    f_permNext(newPrefix, newSurffix, initStr);
 }
};

permNext("", str, str);
return myArr.length;
}

permSuffix("123");

> 6

// options 2,  Introduce and insert a letter to the space of prefix.



Source code:

function introChar(targetStr, chr) {
var arr = [];
var strArr = [];
for (var i=0; i<=targetStr.length; i++) {
strArr = targetStr.split("");
strArr.splice(i, 0, chr);
arr.push(strArr.join(""));
}
return arr;
}

introChar("abc", "1");

function permInsert (str) {
var myArr = [];
var permIntro = function f_intro (arr, pos, permStr) {
console.log(pos, arr, permStr);
var len = permStr.length;
if (pos >= len) {
myArr = myArr.concat(arr);
console.log(arr);
return;
}
if (arr.length === 0) {
arr = introChar("", permStr[0]);
pos++;
}
chr = permStr[pos];
pos++;
arr.forEach( function(val) {
var plusCharArr = introChar(val, chr);
console.log(plusCharArr);
f_intro(plusCharArr, pos, permStr);
});
};
permIntro([], 0, str);
return myArr.length;
}

permInsert("abcde");

> 120

// Reference,  arr is reference type, the paramenter is a link,
// update in a function, update outside arr elements value too.

var yiArr = [7,8,9];

function cut1 (pArr) {
lArr = pArr.slice();
lArr.splice(1, 1);
return lArr;
}

cut1(yiArr);

yiArr;

Monday, May 29, 2017

Pig Latin with RegExp in JavaScript

For Pig Latin exam in Intermediate Algorithm Scripting of https://www.freecodecamp.com/challenges/pig-latin

Here is the normal solution,

function translatePigLatin(str) {
var csntArr = ["b", "c", "d", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "x", "z", "w", "y"];
var strArr = str.split("");
var newStr = "";
var i=0;
var leadCsntArr = Array();

if (csntArr.indexOf(strArr[i]) >= 0) {
while (csntArr.indexOf(strArr[i]) >= 0) {
leadCsntArr.push(strArr[i]);
++i;
}
newStr = str.substr(i) + leadCsntArr.join("") + "ay" ;
} else {
newStr = str + "way" ;
}

  return newStr;
}

translatePigLatin("consonant");

Here is the RegExp solution, it is concise and simple.  ^_^

function translatePigLatin(str) {
  var newStr = "";

  resArr = str.match(/[bcdfghijklmnpqrstvxzwy]*/);
  
  if (resArr[0] === "") {
    newStr = str + "way" ;
  } else {
    newStr = str.substr(resArr[0].length) + resArr[0] + "ay" ;
  }
  return newStr;
}

translatePigLatin("consonant");