From c8741df336ed2eaeab3f60ad77718284381d9a0d Mon Sep 17 00:00:00 2001 From: Sudipta Jana Date: Sun, 27 Aug 2023 14:27:17 +0530 Subject: [PATCH] done coding problem of assignment --- amIPerfect.js | 24 ++++++++++++++++++++++++ hideThatPin.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ nChai.js | 36 ++++++++++++++++++++++++++++++++++++ notes.txt | 2 ++ shortenMe.js | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 amIPerfect.js create mode 100644 hideThatPin.js create mode 100644 nChai.js create mode 100644 notes.txt create mode 100644 shortenMe.js diff --git a/amIPerfect.js b/amIPerfect.js new file mode 100644 index 0000000..6006c4a --- /dev/null +++ b/amIPerfect.js @@ -0,0 +1,24 @@ +// Find perfect number +const findPerfect = (number) => { + const factors = []; + + for (let i = 1; i < number; i++) { + if (number % i === 0) { + factors.push(i); + } + } + let sum = factors.reduce((acc, cur) => { + return acc + cur; + }, 0); + if (sum === number) { + return `${number} is perfect number`; + } else if (sum > number) { + return `${number} is Abundant number`; + } else { + return `${number} is Deficient number`; + } +}; + +const number = 8; + +console.log(findPerfect(8)); \ No newline at end of file diff --git a/hideThatPin.js b/hideThatPin.js new file mode 100644 index 0000000..ed973bb --- /dev/null +++ b/hideThatPin.js @@ -0,0 +1,46 @@ +function hideThatPin(n) { + const dic = { + 1: "pop", + 10: "double rip", + 100: "hide your mints", + 1000: "fall" + }; + + let s = ""; + while (n >= 1) { + s = (n % 2) + s; + n = Math.floor(n / 2); + } + + const bin_number = parseInt(s); + + const l = []; + let i = 0; + + let tempBinNumber = bin_number; + while (tempBinNumber > 0) { + const x = tempBinNumber % 10; + const v = x * Math.pow(10, i); + i++; + + tempBinNumber = Math.floor(tempBinNumber / 10); + if (v !== 0) { + l.push(v); + } else { + continue; + } + } + + const final = []; + for (const x of l) { + if (x === 10000) { + final.reverse(); + } else { + final.push(dic[x]); + } + } + + return final; +} + +console.log(hideThatPin(19)) diff --git a/nChai.js b/nChai.js new file mode 100644 index 0000000..da58d86 --- /dev/null +++ b/nChai.js @@ -0,0 +1,36 @@ +function nChai(n, k, g, b) { + let result = []; + let pushGreen = Array(k).fill("Green") + + let pushBlack = Array(k).fill("Black") + + while (n > 0) { + if (g > 0 && (result.length < k || result.slice(-k).every(type => type === "Black"))) { + result = result.concat(pushGreen); + g -= k; + n -= k; + } else if (b > 0 && (result.length < k || result.slice(-k).every(type => type === "Green"))) { + result = result.concat(pushBlack); + b -= k; + n -= k; + } else { + // It's impossible to fulfill the conditions + return []; + } + + } + + return result; +} + + +// Example 1 +const output1 = nChai(5, 1, 3, 2); +console.log(output1); + +const output2 = nChai(14, 2, 8, 6); +console.log(output2); + +// Example 2 +const output3 = nChai(4, 3, 4, 0); +console.log(output3); // Output: [] diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..e7c8c7d --- /dev/null +++ b/notes.txt @@ -0,0 +1,2 @@ +Here is my actual repo link of my node js assignment with commits +https://github.com/Jisu70/node-logistics-assignment \ No newline at end of file diff --git a/shortenMe.js b/shortenMe.js new file mode 100644 index 0000000..02d1e7a --- /dev/null +++ b/shortenMe.js @@ -0,0 +1,36 @@ + +// Encoding Function +function encodeString(input) { + let encoded = ''; + let count = 1; + + for (let i = 0; i < input.length; i++) { + if (input[i] === input[i + 1]) { + count++; + } else { + encoded += count + input[i]; + count = 1; + } + } + return encoded; +} +let str = 'AAAAAAAAAAABWWWWWWWWWWWBB'; +console.log(encodeString(str)); +// Decoding Function +function decodeString(encodedStr) { + let decoded = ''; + let count = ''; + + for (let char of encodedStr) { + if (Number.isNaN(Number(char))) { + decoded += char.repeat(parseInt(count) || 1); + count = ''; + } else { + count += char; + } + } + + return decoded; +} + +console.log(decodeString('11A1B11W2B'));