Skip to content

Instantly share code, notes, and snippets.

@void666
Last active October 17, 2017 21:40
Show Gist options
  • Select an option

  • Save void666/1b2b4de6c49d35da8ebcccde5eaac583 to your computer and use it in GitHub Desktop.

Select an option

Save void666/1b2b4de6c49d35da8ebcccde5eaac583 to your computer and use it in GitHub Desktop.
Reform Strings.
download and save file `reformStrings.js`.
Install Node. https://nodejs.org/en/
run `node reformStrings.js`
[ [ 'I want to have @food_name, @food_name, @food_name and @food_name now',
'I want to have @food_name now' ],
[ 'I want to have @food_name now',
'I want to have @food_name now' ],
[ 'I want to have @food_name, @food_name and @food_name now',
'I want to have @food_name now' ],
[ 'I want to have @food_name , @food_name, @food_name now',
'I want to have @food_name now' ],
[ 'I want to have @food_name , @food_name , @food_name , @food_name now',
'I want to have @food_name now' ],
[ 'Is @food_name and @food_name better than @food_name, @food_name and @food_name now',
'Is @food_name better than @food_name now' ],
[ 'This is some random text', 'This is some random text' ],
[ 'I want to have @food_name , @food_name and , @food_name @food_name now',
'I want to have @food_name now' ],
[ 'I want to @food_name ,and , , @food_name, and ,@food_name, @food_name',
'I want to @food_name' ],
[ '@food_name and @food_name, @food_name, \t\t @food_name and @food_name is',
'@food_name is' ],
[ '@food_name', '@food_name' ],
[ '@food_name is', '@food_name is' ],
[ '@food_name and @food_name and and @food_name',
'@food_name and and @food_name' ] ]
/**
*
* The algorithm used is to first sanitize the string and separate the words in an array.
* Post which each word is checked for matching instance of `@food_name`. If a match is found another pointer is assigned the present value
* and another loop runs from the present position till matching conjunctions of `@food_name` is found.
* On a mismatch, the currentWord is added to the string with `@food_name` as the prefix.
* Post which the current looping counter is assigned the present value of the inner counter.
* By this mechanism, the problem is solved in O(N) time complexity where N is the number of words in the string,
* as every single word is exactly visited once.
*
* Takes a string and returns after optimising the `@food_name` sub-string instances.
* @param str
* @return {string}
*/
var replaceMultipleFoodName = function (str) {
const FOOD_NAME = '@food_name';
const AND = 'and';
var finalString = '';
var curr = 0;
str = _sanitizeString(str);
while (curr < str.length) {
var currWord = str[curr];
if (currWord === FOOD_NAME) {
var next = curr;
var match = true;
while (next < str.length && match) {
var nextWord = str[next];
if (nextWord === FOOD_NAME) {
if (next === str.length - 1) {
finalString = _buildFinalString(finalString, FOOD_NAME);
curr = next;
break;
}
next++;
continue;
}
if (nextWord === AND && str[next + 1] === FOOD_NAME) {
if (next + 1 === str.length - 1) {
finalString = _buildFinalString(finalString, FOOD_NAME);
curr = next + 1;
break;
}
next += 2;
continue;
}
else {
match = false;
finalString = _buildFinalString(finalString, (FOOD_NAME + ' ' + nextWord));
curr = next;
break;
}
}
}
else {
finalString = _buildFinalString(finalString, currWord);
}
curr++;
}
return finalString;
};
/**
* Helper functions
*
*/
var _buildFinalString = function (finalString, word) {
finalString = finalString ? finalString + ' ' + word : word;
return finalString;
};
var _sanitizeString = function (str) {
str = str.split(/[ , ]/).filter(word => word !== '');
return str;
};
/**
* Driver program with ip (inputs) and output being returned in the asked format.
*
*/
var runIt = function () {
var ip = [
'I want to have @food_name, @food_name, @food_name and @food_name now',
'I want to have @food_name now',
'I want to have @food_name, @food_name and @food_name now',
'I want to have @food_name , @food_name, @food_name now',
'I want to have @food_name , @food_name , @food_name , @food_name now',
'Is @food_name and @food_name better than @food_name, @food_name and @food_name now',
'This is some random text',
//some additional inputs for testing
'I want to have @food_name , @food_name and , @food_name @food_name now', //exp op : 'I want to have @food_name now'
'I want to @food_name ,and , , @food_name, and ,@food_name, @food_name', //exp op : 'I want to @food_name'
'@food_name and @food_name, @food_name, @food_name and @food_name is', //exp op : '@food_name is'
'@food_name', //exp op : '@food_name'
'@food_name is', //exp op : '@food_name is'
'@food_name and @food_name and and @food_name' //exp op : '@food_name and and @food_name'
];
var op = [];
ip.forEach(function (str) {
op.push([str, replaceMultipleFoodName(str)]);
});
return op;
};
// Driver run
console.log(runIt());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment