Last active
July 29, 2024 11:27
-
-
Save bwafi/ee122ac14c0c0041e1e29eade144c177 to your computer and use it in GitHub Desktop.
Pre Assessment Software Engineer - Back End Ultra Voucher
This file contains hidden or 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
function bubbleSort(chars) { | |
let n = chars.length; | |
for (let i = 0; i < n - 1; i++) { | |
for (let j = 0; j < n - i - 1; j++) { | |
if (chars[j] > chars[j + 1]) { | |
let temp = chars[j]; | |
chars[j] = chars[j + 1]; | |
chars[j + 1] = temp; | |
} | |
} | |
} | |
} | |
function sortWord(word) { | |
let chars = word.split(''); | |
bubbleSort(chars); | |
return chars.join(''); | |
} | |
function isInArray(arr, word) { | |
for (let i = 0; i < arr.length; i++) { | |
if (arr[i].includes(word)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function anagrams(words) { | |
let groups = []; | |
for (const word of words) { | |
const sortedWord = sortWord(word); | |
let found = false; | |
for (let group of groups) { | |
if (sortWord(group[0]) === sortedWord) { | |
group.push(word); | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
groups.push([word]); | |
} | |
} | |
return groups; | |
} | |
const words = ['cook', 'save', 'taste', 'aves', 'vase', 'state', 'map']; | |
const anagramResult = anagrams(words); | |
console.log(anagramResult); |
This file contains hidden or 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
CREATE TABLE users ( | |
id SERIAL PRIMARY KEY, | |
name varchar(255), | |
parent_id INTEGER REFERENCES users(id) | |
); | |
INSERT INTO users (id, name, parent_id) VALUES | |
(1, 'Zaki', 2), | |
(2, 'Ilham', NULL), | |
(3, 'Irwan', 2), | |
(4, 'Arka', 3); | |
SELECT * FROM users; | |
SELECT child.id, child.name, parent.name AS parent_name FROM users child | |
LEFT JOIN users parent ON child.parent_id = parent.id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment