Skip to content

Instantly share code, notes, and snippets.

@pde-rent
Last active March 7, 2021 14:08
Show Gist options
  • Save pde-rent/2266811d346dadc02bd0c5c0a3945502 to your computer and use it in GitHub Desktop.
Save pde-rent/2266811d346dadc02bd0c5c0a3945502 to your computer and use it in GitHub Desktop.
Bonus questions: hasAllNumeric + missingCharacter
const NUMERIC_REGEX = /^[-+]?[\d.]+(?:e-?\d+)?$/;
/**
* Checks if a string only contains digits, and complies as number
* @param {String} str - The string
* @returns {boolean}
*/
function isNumeric(str) {
return NUMERIC_REGEX.test(str);
}
/**
* Checks if a number contains all numeric from 0 to 9
* @param {number} num - The number in which we check for numerics
* @returns {boolean}
*/
function hasAllNumeric(num) {
// we could also check for .toString() availability on i type by reflexion
str = num.toString();
if (!isNumeric(str)) {
console.log("Error: input should be numeric");
return false;
}
const occurences = new Array(10).fill(false); // list of 10 >> 0 to 9
// we could also map the string to leverage async execution
for (let i = 0; i < str.length; i++) {
// we here leverage js soft typing such an occurence["x"] == occurence[x]
// if using ts, we would have used parseInt(str[i], 10) or such
occurences[str[i]] = true;
// start checking if matched all digits from i == 9 not to waste checks
// we check at every iteration in order to stop as soon as we meet the constraint
// and not iterate over the whole sequence
// we could benchmark [].every against reducing w/ binary AND
if (i > 9 && occurences.every(c => c)) { return true; }
}
}
/**
* Checks if an array is ordered using the variable built-in comparison operators
* @constructor
* @param {Array.<any>} arr - The array which ordering is checked
* @returns {boolean}
*/
function isOrdered(arr) {
return arr.every((val, i, self) => !i || self[i - 1] <= val);
}
/**
* Checks for a missing character in an ordered sequence
* @constructor
* @param {Array.<String>} chars - The character sequence
* @returns {String} the missing char
*/
function missingCharacter(chars) {
if (!isOrdered(chars)) {
console.log("Error: input characters should be ordered");
return false;
}
// declare the missing char variable as well as the iteration indices
let missingChar, prevCharCode, currentCharCode;
for (let i = 0; i < chars.length; i++) {
if (chars[i].length > 1) {
console.log("Error: only single chars are expected as part of the sequence");
return null;
}
prevCharCode = currentCharCode;
// use the value of the single char string element from the sequence as comparable
currentCharCode = chars[i].charCodeAt(0);
// if we just iterating, no need to compare
if (!i) { continue; }
const step = currentCharCode - prevCharCode;
// if the charCode step is over 2, we skipped too many characters
// if we already have found a missing char and still have skipped a step
// we return an error as well
if (step > 2 || (missingChar && step == 2)) {
console.log("Error: more than a single char missing to the sequence");
return null;
}
// if the step is two, get the in-between value as string
if (step == 2) {
missingChar = String.fromCharCode(prevCharCode + 1);
}
}
return missingChar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment