Let's say we have this string:
BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF
As you can see, here B is occurring 4 times at first but B is also present before DDDD.
Similarly, A is occurring 4 times at the beginning and later 6 times.
I want the expected output if I am searching B it should 4 times as the max occurrence B is 4. However if I am searching A then it should return 6 because the most occurrence for A is 6.
Here is my code I tried:
function checkRepeatativeString(str) {
let hashMap = {};
let seen = new Set();
let counter = 1;
let maxValue = 1;
let isPreviousValueSame = false;
let isNextValueSame = true;
for (let i = 0; i < str.length; i++) {
/**
* is previous value same
*/
if (str[i] == str[i-1]) {
isPreviousValueSame = true;
}
/**
* is next value same
*/
if (str[i] == str[i+1]) {
isNextValueSame = true;
}
if (seen.has(str[i]) && isPreviousValueSame) {
hashMap[str[i]][0]++;
hashMap[str[i]][1]++;
isPreviousValueSame = false;
} else if(seen.has(str[i]) && !isNextValueSame) {
maxValue = Math.max(hashMap[str[i]][1], maxValue);
counter = 0;
hashMap[str[i]] = [counter, maxValue];
} else {
maxValue = Math.max(maxValue, counter);
seen.add(str[i]);
hashMap[str[i]] = [counter, maxValue];
isPreviousValueSame = false;
}
}
return hashMap;
}
let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));
This code is working but if you look for B, I am getting stuck at the beginning of value.
My program returns out for B:
B: [ 1, 1 ]
^ ^
Inside array, 1 is a counter which scans the string and second 1 in array is a max value which should return the output. However my program is returning 1 for B. I am expecting 4 as max value.
Help would be appreciated~
Quick and dirty.
function maxConsecutiveCharacters(check, haystack) {
if(check.length !== 1) return false;
let result = 0;
let buffer = 0;
for(let i = 0; i < haystack.length; i++) {
if(haystack[i] === check) {
buffer++;
}
else {
if(buffer > result) {
result = buffer;
}
buffer = 0;
}
if(buffer > result) {
result = buffer;
}
}
return result;
}
That looks overly complicated. Consider approaching the problem from a different angle - first split up the string into segments of repeating characters, and group them into an object based on the length of the longest substring for a given character.
const checkRepeatativeString = (str) => {
const longestCounts = {};
for (const consecutive of (str.match(/(.)\1*/g) || [])) {
const char = consecutive[0];
longestCounts[char] = Math.max(
longestCounts[char] || 0, // Use the existing value in the object if it exists and is higher
consecutive.length // Otherwise, use the length of the string iterated over
);
}
return longestCounts;
};
let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));
Simpler code often means less surface area for bugs.
I've been trying to solve this codewars challenge. The idea is to return the string, rearranged according to its hierarchy, or separated into chunks according to the repeating character.
You will receive a string consisting of lowercase letters, uppercase letters and digits as input. Your task is to return this string as blocks separated by dashes ("-"). The elements of a block should be sorted with respect to the hierarchy listed below, and each block cannot contain multiple instances of the same character.
The hierarchy is:
lowercase letters (a - z), in alphabetic order
uppercase letters (A - Z), in alphabetic order
digits (0 - 9), in ascending order
Examples
"21AxBz" -> "xzAB12"
since input does not contain repeating characters, you only need 1 block
"abacad" -> "abcd-a-a"
character "a" repeats 3 times, thus 3 blocks are needed
"" -> ""
an empty input should result in an empty output
What I've tried actually works for the given test cases:
describe("Sample tests", () => {
it("Tests", () => {
assert.equal(blocks("21AxBz"), "xzAB12");
assert.equal(blocks("abacad"), "abcd-a-a");
assert.equal(blocks(""), "");
});
});
But fails when there are any repeating characters, besides in the test cases:
function repeatingChar(str){
const result = [];
const strArr = str.toLowerCase().split("").sort().join("").match(/(.)\1+/g);
if (strArr != null) {
strArr.forEach((elem) => {
result.push(elem[0]);
});
}
return result;
}
function blocks(s) {
if (s.length === 0) {
return '';
}
//if string does not contain repeating characters
if (!/(.).*\1/.test(s) === true) {
let lower = s.match(/[a-z]/g).join('');
let upper = s.match(/[A-Z]/g).join('');
let numbers = s.match(/[\d]/g).sort().join('');
return lower + upper + numbers;
}
//if string contains repeating characters
if (!/(.).*\1/.test(s) === false) {
let repeatChar = (repeatingChar(s)[0]);
let repeatRegex = new RegExp(repeatingChar(s)[0]);
let repeatCount = s.match(/[repeatRegex]/gi).length;
let nonAChars = s.match(/[^a]/gi).join('');
function getPosition(string, subString, index) {
return s.split(repeatChar, index).join(repeatChar).length;
}
let index = getPosition(s, repeatChar, 2);
// console.log('indexxxxx', index);
return s.slice(0, index) + nonAChars.slice(1) + ('-' + repeatChar).repeat(repeatCount - 1);
}
}
console.log(blocks("abacad"));
And actually, I'm not sure what's wrong with it, because I don't know how to unlock any other tests on Codewars.
You can see that what I'm trying to do, is find the repeating character, get all characters that are not the repeating character, and slice the string from starting point up until the 2 instance of the repeating character, and then add on the remaining repeating characters at the end, separated by dashes.
Any other suggestions for how to do this?
For funzies, here's how I would have approached the problem:
const isLower = new RegExp('[a-z]');
const isUpper = new RegExp('[A-Z]');
const isDigit = new RegExp('[0-9]');
const isDigitOrUpper = new RegExp('[0-9A-Z]');
const isDigitOrLower = new RegExp('[0-9a-z]');
const isLowerOrUpper = new RegExp('[a-zA-Z]');
function lowerUpperNumber(a, b)
{
if(isLower.test(a) && isDigitOrUpper.test(b))
{
return -1;
}
else if(isUpper.test(a) && isDigitOrLower.test(b))
{
if(isDigit.test(b))
{
return -1;
}
else if(isLower.test(b))
{
return 1;
}
}
else if(isDigit.test(a) && isLowerOrUpper.test(b))
{
return 1;
}
else if(a > b)
{
return 1;
}
else if(a < b)
{
return -1;
}
return 0;
}
function makeBlocks(input)
{
let sortedInput = input.split('');
sortedInput.sort(lowerUpperNumber);
let output = '';
let blocks = [];
for(let c of sortedInput)
{
let inserted = false;
for(let block of blocks)
{
if(block.indexOf(c) === -1)
{
inserted = true;
block.push(c);
break;
}
}
if(!inserted)
{
blocks.push([c]);
}
}
output = blocks.map(block => block.join('')).join('-');
return output;
}
console.log(makeBlocks('21AxBz'));
console.log(makeBlocks('abacad'));
console.log(makeBlocks('Any9Old4String22With7Numbers'));
console.log(makeBlocks(''));
The first obvious error I can see is let repeatCount = s.match(/[repeatRegex]/gi).length;. What you really want to do is:
let repeatRegex = new RegExp(repeatingChar(s)[0], 'g');
let repeatCount = s.match(repeatRegex).length;
The next is that you only look at one of the repeating characters, and not all of them, so you won't get blocks of the correct form, so you'll need to loop over them.
let repeatedChars = repeatingChar(s);
for(let c of repeatedChars)
{
//figure out blocks
}
When you're building the block, you've decided to focus on everything that's not "a". I'm guessing that's not what you originally wrote, but some debugging code, to work on that one sample input.
If I understand your desire correctly, you want to take all the non-repeating characters and smoosh them together, then take the first instance of the first repeating character and stuff that on the front and then cram the remaining instances of the repeating character on the back, separated by -.
The problem here is that the first repeating character might not be the one that should be first in the result. Essentially you got lucky with the repeating character being a.
Fixing up your code, I would create an array and build the blocks up individually, then join them all together at the end.
let repeatedChars = repeatingChar(s);
let blocks = []
for(let c of repeatedChars)
{
let repeatRegex = new RegExp(c, 'g');
let repeatCount = s.match(repeatRegex).length;
for(let i = 1; i <= repeatCount; i++)
{
if(blocks.length < i)
{
let newBlock = [c];
blocks.push(newBlock);
}
else
{
block[i - 1].push(c);
}
}
}
let tailBlocks = blocks.map(block => block.join('')).join('-');
However, this leaves me with a problem of how to build the final string with the non-repeating characters included, all in the right order.
So, to start with, let's make the initial string. To do so we'll need a custom sort function (sorry, it's pretty verbose. If only we could use regular ASCII ordering):
function lowerUpperNumber(a, b)
{
if(a.match(/[a-z]/) && b.match(/[A-Z0-9]/))
{
return -1;
}
else if(a.match(/[A-Z]/) && (b.match(/[0-9]/) || b.match(/[a-z]/)))
{
if(b.match(/[0-9]/))
{
return -1;
}
else if(b.match(/[a-z]/))
{
return 1;
}
}
else if(a.match(/[0-9]/) && b.match(/[a-zA-Z]/))
{
return 1;
}
else if(a > b)
{
return 1;
}
else if(a < b)
{
return -1;
}
return 0;
}
Then create the head of the final output:
let firstBlock = [...(new Set(s))].sort(lowerUpperNumber);
The Set creates a set of unique elements, i.e. no repeats.
Because we've created the head string, when creating the blocks of repeated characters, we'll need one fewer than the above loop gives us, so we'll be using s.match(repeatRegex).length-1.
I get the desire to short circuit the complicated bit and return quickly when there are no repeated characters, but I'm going to remove that bit for brevity, and also I don't want to deal with undefined values (for example try '123' as your input).
Let's put it all together:
function lowerUpperNumber(a, b)
{
if(a.match(/[a-z]/) && b.match(/[A-Z0-9]/))
{
return -1;
}
else if(a.match(/[A-Z]/) && (b.match(/[0-9]/) || b.match(/[a-z]/)))
{
if(b.match(/[0-9]/))
{
return -1;
}
else if(b.match(/[a-z]/))
{
return 1;
}
}
else if(a.match(/[0-9]/) && b.match(/[a-zA-Z]/))
{
return 1;
}
else if(a > b)
{
return 1;
}
else if(a < b)
{
return -1;
}
return 0;
}
function makeBlocks(s)
{
if (s.length === 0)
{
return '';
}
let firstBlock = [...(new Set(s))].sort(lowerUpperNumber);
let firstString = firstBlock.join('');
let blocks = [];
for(let c of firstString)
{
let repeatRegex = new RegExp(c, 'g');
let repeatCount = s.match(repeatRegex).length - 1;
for(let i = 1; i <= repeatCount; i++)
{
if(blocks.length < i)
{
let newBlock = [c];
blocks.push(newBlock);
}
else
{
blocks[i - 1].push(c);
}
}
}
blocks.unshift(firstBlock);
return blocks.map(block => block.join('')).join('-');
}
console.log(makeBlocks('21AxBz'));
console.log(makeBlocks('abacad'));
console.log(makeBlocks('Any9Old4String22With7Numbers'));
console.log(makeBlocks(''));
You'll see I've not bothered generating the characters that repeat, because I can just skip the ones that don't.
I have this question that is medium level and couldn't even think on how to solve this problem, my solution could be overkill as I have no idea on how to traverse a bunch of numbers in an array to check whether it is a binary tree or not. The program always returns false no matter what
If you have a better answer to the question that would be perfect
Have the function TreeConstructor(strArr) take the array of strings stored in strArr, which will contain pairs of integers in the following format (i1, i2) where i1 represents a child a node in a tree and the second integer i2 signifies that it is the parent of i1. For example if strArr is ["(1,2)", "(2,4)", "(7,2)"]
4
/
2
/ \
1 7
which you can see forms a proper binary tree. Your program should, in this case, return the string true because a valid binary tree can be formed. If a proper binary cannot be formed with the integer pairs, then return the string false. All of the integers within the tree will be unique, which means there can only be one node in the tree with the given integer value
Examples
input: ["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]
output: true
input ["(1,2)", "(1,3)"]
output: false
I came out with an attempt, but it always returns false. Most likely my code is overkill.
class Node {
// The constructor
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
// Basic insert node
insert(value) {
let currentNode = this;
while (true) {
if (value < currentNode.value) {
if (currentNode.left === null) {
currentNode.left = new Node(value);
break;
} else {
currentNode = currentNode.left;
}
} else {
if (currentNode.right === null) {
currentNode.right = new Node(value);
break;
} else {
currentNode = currentNode.right;
}
}
}
return currentNode
}
// check if BST is valid or not
isValidBST(node, min = null, max = null) {
if (!node) return true;
if (max !== null && node.value >= max) {
return false;
}
if (min !== null && node.value <= min) {
return false;
}
const leftSide = this.isValidBST(node.left, min, node.value);
const rightSide = this.isValidBST(node.right, node.value, max);
return leftSide && rightSide;
}
}
// Convert the strings to a number
function convertListToNumber(str, i) {
return str[i].split('(').join('').split(')').join('').split(',').join('')
}
This is the main function
function TreeConstructorTwo(strArr) {
// code goes here
startValueFromList = convertListToNumber(strArr, 0)
// Parent Node here
startParentNode = startValueFromList[1];
// Child Node here
startChildNode = startValueFromList[0];
// Add parent Node and childNode
node = new Node(startParentNode);
node.insert(startChildNode);
// Loop through the entire array
for (i = 1; i < strArr.length; i++) {
myListValue = convertListToNumber(strArr, i);
console.log(myListValue.length)
// Loop the "12" in the string and convert it to a number
for (j = 0; j < myListValue.length; j++) {
node.insert(myListValue[j])
}
parentNode = Number(myListValue[0])
}
// Check if the BST is valid or not
return node.isValidBST(node)
}
// keep this function call here
console.log(TreeConstructorTwo(["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]));
You seem to have misunderstood the assignment. The function should return true when the represented tree is a binary tree, not necessarily a binary search tree.
Your code is creating a tree from the first element and then takes any next node to insert it into that tree keeping with the binary search property, without taking into account that the pair from the input demands that the first is a direct child of the second. (Your variable parentNode is not used for anything)
Instead, you should just look at the child-parent relationships that are given in the input as representing edges, and use that information to build the graph. Finally you should verify that that graph represents a binary tree. Think about what are the distinctive characteristics of a binary tree and how to verify them.
Hint 1:
No node should have two parents
Hint 2:
No node should have 3 children
Hint 3:
All upward paths should end in the same node (the root)
The spoiler solution below does not return true/false, but a string that indicates whether the tree is "ok", or why it is not. This is more useful for debugging and still easy to convert to a boolean.
// This function returns the reason why it considers the input
// not a binary tree. "ok" otherwise.
function isBinaryTree(edgesStr) {
const childToParent = new Map(edgesStr.map(edge => edge.match(/\d+/g)));
// No node should have 2 parents
if (childToParent.size < edgesStr.length) return "node with two parents";
// No node should have 3 children
const degree = {};
for (const [child, parent] of childToParent) {
if ((++degree[parent] || (degree[parent] = 1)) > 2) return "node with three children";
}
// All upward paths lead to the same root (no cycles)
const nodes = {};
let current = 0;
let countRoots = 0;
for (let node of childToParent.keys()) {
current++;
while (node && !nodes[node]) {
nodes[node] = current;
node = childToParent.get(node);
}
if (!node && countRoots++) return "disconnected";
if (node && nodes[node] == current) return "cycle";
}
return "ok";
}
const tests = [
["(2,1)", "(3,1)", "(4,2)", "(5,2)", "(6,3)", "(7,3)"],
["(1,2)", "(3,2)", "(2,12)", "(5,2)"],
["(2,1)", "(3,1)", "(5,4)", "(5,2)"],
["(2,1)", "(4,3)"],
["(1,2)", "(3,4)", "(4,5)", "(5,3)"],
];
for (const test of tests) {
console.log(isBinaryTree(test));
}
NB: I would name the function with an initial lowercase letter as it is the common practice to reserve initial capital letters for class names.
Recently I've did the solution for this task, check my solution below:
const isBinaryTree = (array) => {
const getChildrenNode = (node) => node.slice(1, 2);
const childrenNodes = array.map(x => getChildrenNode(x));
const isChildrenNodesIsUnique = (array) => array.length === new Set(array).size;
return isChildrenNodesIsUnique(childrenNodes);
};
console.log(isBinaryTree(["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]));
console.log(isBinaryTree(["(1,2)", "(1,3)"]));
I did this by collecting the parent ids in an array and checking if any parent id is exists more than twice. Not sure though it will
["(1,2)", "(3,2)", "(2,12)", "(5,2)"]
since 2 came 3 times at the right of the pair its a false (a root or leaf node cannot have more than 2 childs)
const isBinaryTree = (array) => {
// find the parent child
const parentChildFreq = (value) => {
const charFreqency = {};
for (let index = 0; index < value.length; index++) {
if (charFreqency[value[index]]) {
// if parent have more the 2 children then this is not binary-tree
if (charFreqency[value[index]] >= 2) {
return false;
}
charFreqency[value[index]] += 1;
} else charFreqency[value[index]] = 1;
}
return true;
};
const isChildrenNodesIsUnique = (array) =>
array.length === new Set(array).size;
const childrenNodes = array.map((node) => node[1]);
let parentsNodes = array.map((node) => node[3]);
parentsNodes = Array.from(parentsNodes, Number);
return isChildrenNodesIsUnique(childrenNodes) && parentChildFreq(parentsNodes)
? " \n Binary-tree"
: "not-binary-tree";
};
console.log(isBinaryTree(["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]));
//console.log(isBinaryTree(["(1,2)", "(3,2)", "(2,12)", "(5,2)"]));
I had it at a coding interview, could not make it on time but later I did this. It seems to be working but it is as good as my tests are. It seems that this two conditions are enough:
-A node can have less than 3 child.
-Every node has a parent but one.
function ArrayChallenge(strArr) {
//Make a num array
const numArray = strArr.map((str) => {
var mySubString = str.substring(str.indexOf("(") + 1, str.lastIndexOf(")"));
return mySubString.split(",").map(Number);
});
//Get parent nodes
const parents = numArray.map((arr) => {
return arr[1];
});
//Get children nodes
const children = numArray.map((arr) => {
return arr[0];
});
//Conditions
//a parent can't have 3 children
const threeChildrenCondition = findRepeated(parents) < 3 ? true : false;
const allHaveParentsButOneCondition = allHaveParentsButOne();
//every node has a parent except for one
function allHaveParentsButOne() {
var parentOfAll = parents.filter(function (num) {
return children.indexOf(num) == -1;
});
//Will remove duplicate numbers (parent appearing two times)
let unique = [...new Set(parentOfAll)];
const result = unique.length == 1 ? true : false;
return result;
}
//Returns how many times a number is repeated in an array
function findRepeated(array) {
let repeatedNumbers = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] === array[j]) {
repeatedNumbers.push(array[i]);
}
}
}
const length = repeatedNumbers.length;
return length;
}
const result = threeChildrenCondition && allHaveParentsButOneCondition;
return result;
}
//CHALLENGES
//Must be false
const f0 = ["(1,2)", "(3,2)", "(2,12)", "(5,2)"];
const f1 = ["(1,4)", "(3,2)", "(2,12)", "(5,2)"];
const f2 = ["(1,7)", "(3,7)", "(2,7)"];
const f3 = ["(10,20)", "(20,50)", "(20,8)", "(8,4)"];
console.log(ArrayChallenge(f0));
console.log(ArrayChallenge(f1));
console.log(ArrayChallenge(f2));
console.log(ArrayChallenge(f3));
//Must be true
const t0 = ["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"];
const t1 = ["(2,3)", "(1,2)", "(4,9)", "(9,3)", "(12,9)", "(6,4)"];
const t2 = ["(1,2)", "(2,4)", "(7,4)"];
const t3 = ["(5,6)", "(6,3)", "(2,3)", "(12,5)"];
const t4 = ["(10,20)", "(20,50)"];
console.log(ArrayChallenge(t0));
console.log(ArrayChallenge(t1));
console.log(ArrayChallenge(t2));
console.log(ArrayChallenge(t3));
console.log(ArrayChallenge(t4));
public static bool TreeConstructor(string[] strArr)
{
var treeList = new List<int[]>();
foreach (var node in strArr)
{
treeList.Add(Array.ConvertAll(node.Replace("(", "").Replace(")", "").Split(","), s => int.Parse(s)));
}
bool isChildNotUnique = treeList.GroupBy(s => s[0]).Any(g => g.Count() > 1);
bool isMoreThanTwoChild = treeList.GroupBy(s => s[1]).Any(g => g.Count() > 2);
int[] rootNode = null;
if (isChildNotUnique || isMoreThanTwoChild)
return false;
foreach (var node in treeList)
{
var childNodes = treeList.Where(s => node[1] == s[1]);
bool leftChild = childNodes.Any(s => s[0] < node[1]);
bool rightChild = childNodes.Any(s => s[0] > node[1]);
if (childNodes.Count() > 1 && (!rightChild || !rightChild))
{
return false;
}
else if (!leftChild && !rightChild)
return false;
bool isNotRootNode = treeList.Any(s => s[0] == node[1]);
if (!isNotRootNode)
if (rootNode != null && rootNode[1] != node[1])
return false;
else
rootNode = node;
}
return true;
}
How do i check that a given word is an isogram with pure javascript, using a function. the function must return true or false.
An isogram is a word with a repeated character.
I know this code works, but i need a better solution.
function isIsogram(word){
x = false; y = false;
for(i = 0; i < word.length; i++){
wordl = word.substring(0,i)
wordr = word.substring(i)
x = wordl.includes(word.charAt(i))
y = wordr.includes(word.charAt(i))
//console.log(x,wordl,wordr)
}
return x&&y
}
isIsogram("thomas");//False
isIsogram("moses"); //True
Remove the duplicate letter from string then check both length. if same its an isogram.
function isIsogram(str){
return str.split('').filter((item, pos, arr)=> arr.indexOf(item) == pos).length == str.length;
}
console.log(isIsogram('thomas'));
console.log(isIsogram('moses'));
One way of doing this!
function isIsogram(str){
return !str.match(/([a-z]).*\1/i);
}
Here is a simple approach using .split() and .every():
let isIsogram = (str) => str.split("").every((c, i) => str.indexOf(c) == i);
console.log(isIsogram("thomas")); /* no repeating letter */
console.log(isIsogram("moses")); /* s repeat 2 times */
console.log(isIsogram("hello")); /* l repeat 2 times */
console.log(isIsogram("world")); /* no repeating letter */
console.log(isIsogram("a b c")); /* space character repeat 2 times */
Docs:
String.prototype.split()
String.prototype.indexOf()
Array.prototype.every()
Building on kishea's answer:
function isIsogram(sWord)
{
for (iCharIndex = 0; iCharIndex < sWord.length; iCharIndex++)
if (sWord.substring(iCharIndex + 1).includes(sWord.charAt(iCharIndex)))
return false;
return true;
}
If the character at the current position (charAt) is found (includes) to the right of the current position (substring), false is returned. Otherwise the loop runs to the end and true is returned.
const isIsogram = (word) => {
return new Set(word.toLowerCase()).size === word.length
}
console.log(isIsogram('Thor'));//true
console.log(isIsogram('Loki'));//true
console.log(isIsogram('America'));//false
function isIsogram(str) {
return new Set(str.toUpperCase()).size == str.length
}
What about :
> function isIsogram(word) {
... var a = word.split('')
... var b = Array.from(new Set(a))
... return a.length === b.length;
... }
undefined
> isIsogram("mesos")
false
> isIsogram("thomas")
true
Or better (checking each char only once) :
> function isIsogram2(word) {
... for(var i=0,len=word.length-1;i<len;++i) {
..... var c = word[i]
..... if(word.indexOf(c,i+1) !== -1) return false;
..... }
... return true;
... }
undefined
> isIsogram2("mesos")
false
> isIsogram2("thomas")
true
function isIsogram(word){
return !/(.).*\1|\d/i.test(word)
}
var str=prompt('Enter a string');
var strlen=str.length;
for(i=0;i<strlen;i++){
var stc=str.charAt(i);
var flag=0;
for(j=0;j<strlen;j++){
var stk=str.charAt(j);
if(stc==stk){
flag=flag+1;
}
}
if(flag!=1){
break;
}
}
if(flag!=1){
alert('It is not an isogram');
}
else{
alert('It is an isogram');
}
While given a word, this function if splits the word into two,
That is wordl and wordr respectively.
Both splittings are checked to include a character in the original word. If wordl and wordr both contain any character in the original word. Then surely this is an isogram
function isIsogram(word){
x = false; y = false;
for(i = 0; i < word.length; i++){
wordl = word.substring(0,i)
wordr = word.substring(i)
x = wordl.includes(word.charAt(i))
y = wordr.includes(word.charAt(i))
//console.log(x,wordl,wordr)
}
return !x&&y
}
isIsogram("thomas");//True
isIsogram("moses"); //False
const isIsogram = (string) => {
const lowerCased = string.toLowerCase()
const result = lowerCased.split('').every((v,i)=>lowerCased.indexOf(v)===i)
return result
}
console.log(isIsogram('ambidExtRously')) // true
console.log(isIsogram('patteRN')) // false
function isIsogram(word) {
let uniqueCharacters = new Set(word.split(''));
uniqueCharacters = Array.from(uniqueCharacters); //get all the unique char
let charFreq = {}; //e.g {a:2, b:3}
for (element of uniqueCharacters) {
charFreq[element] = 0;
} //set the frequency of each char to zero
function updateFrequency(element) {
charFreq[element] += 1;
}//callback used directly below
word.split('').forEach(updateFrequency); //for each char encountered, update frequency
let frequencyOfCharacter = [];
for (keys in charFreq) {
frequencyOfCharacter.push(charFreq[keys]);
}
function equal(item) {
return item === frequencyOfCharacter[0];
}
//check if all the frequencies are the same, an isogram means all characters occur at the same frequency
return frequencyOfCharacter.every(equal);
}
console.log(isIsogram('try'), isIsogram('baba'), isIsogram('tests'));
I have the following code
var utils = require(`${__dirname}/../../utils/utils.js`);
...
let object = utils.parse(input);
if (object === undefined){
let helper = utils.recognize(input);
msg.channel.sendMessage("\"" + input + "\" not recognized. Did you mean \"" + helper[0] + "\"?");
object = utils.parse(helper[0]);
}
//code related to object
console.log(object.strLength);
where "parse" tries to match the input to an object in a database, and "recognize" tries to find the best match if the input is spelled incorrectly (Levenshtein) (along with additional info such as how close the match was).
Currently the issue is that the code is ran asynchronously; "object.strLength" returns an undefined before utils.recognize() returns a value. If I copy/paste the recognize() and parse() functions into the file, then the code is run synchronously and I do not run into any issues. However I would rather keep those functions in a separate file as I reuse them in other files.
Is there a way to specify that the functions in utils must be synch? I know that there are libraries that convert asynch into synch but I prefer to use as few libraries as I can help it. I tried to have the recognize functions return a Promise but it ended up as a jumbled mess
edit: here's parse. I did not think it was necessary to answer this question so I did not include it initially:
var db = require(`${__dirname}/../data/database.js`);
...
var parse = (input) => {
let output = db[output];
if (output === null) {
Object.keys(db).forEach((item) => {
if (db[item].num === parseInt(input) || (db[item].color + db[item].type === input)){
output = db[item];
return false;
}
});
}
return output;
}
I solved the issue, thanks everyone. Here's what was wrong, it was with recognize(). It was my mistake to not show the code for it initially.
Original recognize:
var recognize = (item) => {
//iterate through our databases and get a best fit
let bestItem = null;
let bestScore = 99999; //arbitrary large number
//let bestType = null;
//found algorithm online by milot-mirdita
var levenshtein = function(a, b) {
if (a.length == 0) { return b.length; }
if (b.length == 0) { return a.length; }
// swap to save some memory O(min(a,b)) instead of O(a)
if(a.length > b.length) {
let tmp = a;
a = b;
b = tmp;
}
let row = [];
for(let i = 0; i <= a.length; i++) {
row[i] = i;
}
for (let i = 1; i <= b.length; i++) {
let prev = i;
for (let j = 1; j <= a.length; j++) {
let val;
if (b.charAt(i-1) == a.charAt(j-1)) {
val = row[j-1]; // match
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
row[j - 1] = prev;
prev = val;
}
row[a.length] = prev;
}
return row[a.length];
}
//putting this here would make the code work
//console.log("hi");
Object.keys(db).forEach((key) => {
if (levenshtein(item, key) < bestScore) {
bestItem = key;
bestScore = levenshtein(item, key);
}
});
return [bestItem, bestScore];
}
My solution was to move the levenshtein function outside of the recognize function, so if I wanted to I can call levenshtein from another function
#user949300 and #Robert Moskal, I changed the forEach loop into a let...in loop. There is no functional difference (as far as I can tell) but the code does look cleaner.
#Thomas, I fixed the let output = db[output]; issue, oops.
Again, thanks for all of your help, I appreciate it. And happy New Year too