Created
January 10, 2022 07:24
-
-
Save rmdhfz/ebcfee789fb5b581346e665d352e1b95 to your computer and use it in GitHub Desktop.
Technical Test - Pre Assessment Software Engineer (Jr. Back End) Ultra Voucher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const words = ["cook", "save", "taste", "aves", "vase", "state", "map"]; | |
const findAnagrams = (words) => { | |
const uniqueAnagrams = [words[0]], | |
combinedAnagrams = [words[0]]; | |
for (let i = 1; i < words.length; i++) { | |
const word = words[i]; | |
let matched = false; | |
for (let j = 0; j < uniqueAnagrams.length; j++) { | |
const testWord = uniqueAnagrams[j]; | |
let matchCount = 0; | |
for (let k = 0; k < word.length; k++) { | |
const letter = word[k], | |
match = testWord.includes(letter); | |
match && matchCount++; | |
} | |
if (matchCount === testWord.length) { | |
matched = true; | |
Array.isArray(combinedAnagrams[j]) | |
? combinedAnagrams[j].push(word) | |
: (combinedAnagrams[j] = [testWord, word]); | |
break; | |
} | |
} | |
if (!matched) { | |
combinedAnagrams.push(word); | |
uniqueAnagrams.push(word); | |
} | |
} | |
return combinedAnagrams; | |
}; | |
console.log(findAnagrams(words)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
child:
parent:
Query SQL:
SELECT a.id, a.name, b.name as parent_name FROM child a LEFT JOIN parent b ON a.parent_id = b.id;