Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done coding problem of assignment #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions amIPerfect.js
Original file line number Diff line number Diff line change
@@ -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));
46 changes: 46 additions & 0 deletions hideThatPin.js
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 36 additions & 0 deletions nChai.js
Original file line number Diff line number Diff line change
@@ -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: []
2 changes: 2 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Here is my actual repo link of my node js assignment with commits
https://github.com/Jisu70/node-logistics-assignment
36 changes: 36 additions & 0 deletions shortenMe.js
Original file line number Diff line number Diff line change
@@ -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'));