Top tasks
Integer to English
https://www.codewars.com/kata/53c94a82689f84c2dd00007d/train/javascript
function intToEnglish(number) {
const units = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
];
const tens = [
"",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
];
const scales = [
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
];
if (number === 0) return "zero";
function convertToWords(n) {
if (n < 20) return units[n];
if (n < 100)
return tens[Math.floor(n / 10)] + (n % 10 ? "-" + units[n % 10] : "");
if (n < 1000)
return (
units[Math.floor(n / 100)] +
" hundred" +
(n % 100 ? " and " + convertToWords(n % 100) : "")
);
for (let scaleIndex = scales.length - 1; scaleIndex >= 0; scaleIndex--) {
const scaleValue = Math.pow(1000, scaleIndex);
if (n >= scaleValue) {
const prefix =
convertToWords(Math.floor(n / scaleValue)) + " " + scales[scaleIndex];
const suffix = n % scaleValue;
return prefix + (suffix ? " " + convertToWords(suffix) : "");
}
}
}
return convertToWords(number).trim();
}
https://leetcode.com/problems/integer-to-english-words/
const numberToWords = (num) => {
if (num === 0) return "Zero";
const ones = [
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
];
const tens = [
"",
"",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
];
const thousands = ["", "Thousand", "Million", "Billion"];
const convertHundreds = (num) => {
let result = "";
if (num >= 100) {
result += ones[Math.floor(num / 100)] + " Hundred ";
num %= 100;
}
if (num >= 20) {
result += tens[Math.floor(num / 10)] + " ";
num %= 10;
}
if (num > 0) {
result += ones[num] + " ";
}
return result.trim();
};
let result = "";
let unitIndex = 0;
while (num > 0) {
const part = num % 1000;
if (part > 0) {
result =
convertHundreds(part) +
(thousands[unitIndex] ? " " + thousands[unitIndex] : "") +
" " +
result;
}
num = Math.floor(num / 1000);
unitIndex++;
}
return result.trim();
};
Print folder structure
class Node {
constructor(name) {
this.name = name;
this.children = {};
}
}
class Tree {
constructor() {
this.root = new Node("");
}
insertFile(filePath) {
const folders = filePath.split("/").slice(1);
let current = this.root;
for (const folder of folders) {
if (!current.children[folder]) {
current.children[folder] = new Node(folder);
}
current = current.children[folder];
}
}
printFiles() {
this.dfsPrint(this.root, "");
}
dfsPrint(node, indent) {
if (Object.keys(node.children).length === 0) {
console.log(indent + "-- " + node.name);
return;
}
console.log(indent + "-- " + node.name);
for (const child of Object.values(node.children)) {
this.dfsPrint(child, indent + " ");
}
}
}
const files = [
"/webapp/assets/html/a.html",
"/webapp/assets/html/b.html",
"/webapp/assets/js/c.js",
"/webapp/index.html",
];
const tree = new Tree();
for (const file of files) {
tree.insertFile(file);
}
tree.printFiles();
Longest Absolute File Path
https://leetcode.com/problems/longest-absolute-file-path/
var lengthLongestPath = function (input) {
let maxLength = 0;
const stack = [];
const parts = input.split("\n");
for (let part of parts) {
// Count the depth level (number of '\t')
const level = part.lastIndexOf("\t") + 1;
const name = part.slice(level);
// Calculate current path length and adjust the stack
const currentLength = (stack[level - 1] || 0) + name.length + 1; // +1 for '/'
// If it's a file, check if it's the longest path so far
if (name.includes(".")) {
maxLength = Math.max(maxLength, currentLength - 1); // -1 to exclude the last '/'
} else {
stack[level] = currentLength; // Update stack for directories
}
}
return maxLength;
};