courvillejustin
4/8/2015 - 9:40 PM

JS Bin Interview Cake - problem mathcing parenthesis] // source http://jsbin.com/gesafo

JS Bin

Interview Cake - problem mathcing parenthesis]

// source http://jsbin.com/gesafo

(function(){
  /**
   * pMap - take an input string and creates a dictionary mapping the opening and 
   * closing parenthesis in the string. 
   */
  var pMap = function(inputStr){
    var pDict = {};
    var pStack = [];
    inputStr.split('').forEach(function(c,pos){
      if(c === '('){
        pStack.unshift(pos);
      } 
      else if(c === ')'){
        pDict[pStack.shift().toString()] = '' + pos;
      }
    });
    return pDict;
  }
  var testString = "((Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.))";
  console.log(pMap(testString));
})();
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Interview Cake - problem mathcing parenthesis]">
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>

<script id="jsbin-javascript">
(function(){
  /**
   * pMap - take an input string and creates a dictionary mapping the opening and 
   * closing parenthesis in the string. 
   */
  var pMap = function(inputStr){
    var pDict = {};
    var pStack = [];
    inputStr.split('').forEach(function(c,pos){
      if(c === '('){
        pStack.unshift(pos);
      } 
      else if(c === ')'){
        pDict[pStack.shift().toString()] = '' + pos;
      }
    });
    return pDict;
  }
  var testString = "((Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.))";
  console.log(pMap(testString));
})();
</script>



<script id="jsbin-source-javascript" type="text/javascript">(function(){
  /**
   * pMap - take an input string and creates a dictionary mapping the opening and 
   * closing parenthesis in the string. 
   */
  var pMap = function(inputStr){
    var pDict = {};
    var pStack = [];
    inputStr.split('').forEach(function(c,pos){
      if(c === '('){
        pStack.unshift(pos);
      } 
      else if(c === ')'){
        pDict[pStack.shift().toString()] = '' + pos;
      }
    });
    return pDict;
  }
  var testString = "((Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing.))";
  console.log(pMap(testString));
})();</script></body>
</html>