Skip to content

Commit c844c03

Browse files
committed
update
1 parent d85571f commit c844c03

14 files changed

+715
-39
lines changed

caesars-cipher.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* JavaScript Algorithms and Data Structures Projects: Caesars Cipher
3+
*
4+
* One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
5+
*
6+
* A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
7+
*
8+
* Write a function which takes a ROT13 encoded string as input and returns a decoded string.
9+
*
10+
* All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
11+
*/
12+
13+
function rot13(str) { // LBH QVQ VG!
14+
let decode = '';
15+
16+
for (let i = 0, l = str.length; i < l; i++) {
17+
if (/[A-Z]/.test(str.charAt(i))) {
18+
const code = 65 + (str.charCodeAt(i) - 65 + 13) % 26;
19+
decode += String.fromCharCode(code);
20+
} else {
21+
decode += str.charAt(i);
22+
}
23+
}
24+
25+
return decode;
26+
}
27+
28+
// Change the inputs below to test
29+
console.log(rot13("SERR PBQR PNZC"));

factorial.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Factorial
3+
*
4+
* Write a function to return the factorial of a number.
5+
* Factorial of a number is given by :
6+
*
7+
* n! = n * (n-1) * (n-2) * ..... * 1
8+
*
9+
* For example :
10+
* 3! = 3*2*1 = 6
11+
* 4! = 4*3*2*1 = 24
12+
*
13+
* Note :
14+
* 0! = 1
15+
*/
16+
function factorial(n) {
17+
if (n <= 1) {
18+
return 1;
19+
}
20+
return n * factorial(n - 1);
21+
}
22+
23+
/**
24+
* Get n numbers of factorials
25+
* @param {Number} n
26+
* @return {Number[]}
27+
* @example
28+
* factorial_nums(4) // [1, 1, 2, 6]
29+
* debug:
30+
* [1]
31+
* [1, 1],
32+
* [1, 1, 2]
33+
* [1, 1, 2, 6]
34+
*/
35+
function factorial_nums(n) {
36+
if (n <= 1) {
37+
return [1];
38+
}
39+
const nums = factorial_nums(n - 1);
40+
nums.push(nums[nums.length - 1] * (n - 1));
41+
return nums;
42+
}
43+
44+
console.log(factorial(3));
45+
console.log(factorial(4));
46+
47+
console.log(factorial_nums(3));
48+
console.log(factorial_nums(4));

factors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Factors of an integer
3+
*
4+
* Write a function that returns the factors of a positive integer.
5+
*
6+
* These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.
7+
*/
8+
9+
function factors (num) {
10+
const nums = [];
11+
12+
for (let i = 1; i <= num; i++) {
13+
if (num % i === 0) {
14+
nums.push(i);
15+
}
16+
}
17+
18+
return nums;
19+
}
20+
21+
22+
console.log(factors(45)); // should return [1,3,5,9,15,45]
23+
console.log(factors(53)); //should return [1,53]
24+
console.log(factors(64)); //should return [1,2,4,8,16,32,64]

fibonacci.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Rosetta Code: Fibonacci sequence
3+
* Write a function to generate the nth Fibonacci number.
4+
*
5+
* The nth Fibonacci number is given by :
6+
*
7+
* Fn = Fn-1 + Fn-2
8+
*
9+
* The first two terms of the series are 0, 1.
10+
*
11+
* Hence, the series is : 0, 1, 1, 2, 3, 5, 8, 13...
12+
*/
13+
14+
function fibonacci_recursive(n) {
15+
if (n === 1) {
16+
return 0;
17+
} if (n === 2) {
18+
return 1;
19+
}
20+
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
21+
}
22+
23+
function fibonacci(n) {
24+
const nums = [0, 1];
25+
26+
if (n <= 2) {
27+
return nums[n - 1];
28+
}
29+
30+
for (let i = 2; i < n; i++) {
31+
const next = nums[0] + nums[1];
32+
nums[0] = nums[1];
33+
nums[1] = next;
34+
}
35+
return nums[1];
36+
}
37+
38+
console.log(fibonacci_recursive(3)); // 1
39+
console.log(fibonacci_recursive(5)); // 3
40+
console.log(fibonacci_recursive(10)); // 34
41+
42+
console.log("===");
43+
44+
console.log(fibonacci(3)); // 1
45+
console.log(fibonacci(5)); // 3
46+
console.log(fibonacci(10)); // 34

find-missing-element-in-two-arrays.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function find(arr1, arr2) {
2+
const n = arr1.length;
3+
const m = arr2.length;
4+
5+
if ((n !== m - 1 && m !== n - 1) || m === n) {
6+
throw new Error('Invalid input');
7+
}
8+
9+
let ans = 0;
10+
11+
for (let v of arr1) {
12+
ans ^= v;
13+
}
14+
15+
for (let v of arr2) {
16+
ans ^= v;
17+
}
18+
19+
return ans;
20+
}
21+
22+
23+
console.log(find([1,4,5,7,9], [4,5,7,9])); // 1
24+
console.log(find([2,3,4,5], [2,3,4,5,6])); // 6
25+
console.log(find([2,3,4,5,6], [1, 2,3,4,5,6])); // 6
26+
// 5, 6
27+
28+
// 5 !== 5 && 6 !== 4

integer-to-roman.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Integer to Roman
3+
* Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
4+
*
5+
* Symbol Value
6+
* I 1
7+
* V 5
8+
* X 10
9+
* L 50
10+
* C 100
11+
* D 500
12+
* M 1000
13+
*
14+
* For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
15+
*
16+
* Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
17+
*
18+
* I can be placed before V (5) and X (10) to make 4 and 9.
19+
* X can be placed before L (50) and C (100) to make 40 and 90.
20+
* C can be placed before D (500) and M (1000) to make 400 and 900.
21+
*
22+
* Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
23+
*
24+
* Example 1:
25+
*
26+
* Input: 3
27+
* Output: "III"
28+
*
29+
* Example 2:
30+
*
31+
* Input: 4
32+
* Output: "IV"
33+
*
34+
* Example 3:
35+
*
36+
* Input: 9
37+
* Output: "IX"
38+
*
39+
* Example 4:
40+
*
41+
* Input: 58
42+
* Output: "LVIII"
43+
* Explanation: L = 50, V = 5, III = 3.
44+
*
45+
* Example 5:
46+
*
47+
* Input: 1994
48+
* Output: "MCMXCIV"
49+
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
50+
*/
51+
52+
/**
53+
* @param {number} num
54+
* @return {string}
55+
*/
56+
function intToRoman(num) {
57+
const nums = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
58+
const romans = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
59+
60+
let ans = '';
61+
62+
for (let i = 0; i < nums.length; i++) {
63+
while (num >= nums[i]) {
64+
ans += romans[i];
65+
num -= nums[i];
66+
}
67+
}
68+
69+
return ans;
70+
};

is-balance.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Balanced brackets
3+
*
4+
* Determine whether a generated string of brackets is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
5+
*
6+
* Examples:
7+
* (empty) true
8+
* [] true
9+
* ][ false
10+
* [][] true
11+
* ][][ false
12+
* []][[] false
13+
* [[[[]]]] true
14+
*/
15+
16+
function isBalanced (str) {
17+
let bal = 0;
18+
19+
for (let i = 0; i < str.length; i++) {
20+
if (str.charAt(i) === "[") {
21+
bal += 1;
22+
} else {
23+
bal -= 1;
24+
}
25+
26+
if (bal < 0) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
console.log(isBalanced('') === true);
35+
console.log(isBalanced('[]') === true);
36+
console.log(isBalanced('))][') === false);
37+
console.log(isBalanced('[][]') === true);
38+
console.log(isBalanced('))][][') === false);
39+
console.log(isBalanced('[]][[]') === false);
40+
console.log(isBalanced('[[[[]]]]') === true);

longest-palindromic-substring.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ function longestPalindromeSubstring(str) {
1010
}
1111

1212
let start = 0;
13-
let end = 0;
13+
let maxLen = 0;
1414

1515
for (let i = 0, l = str.length; i < l; i++) {
1616
let len1 = expandAroundCenter(str, i, i);
1717
let len2 = expandAroundCenter(str, i, i + 1);
1818
let len = Math.max(len1, len2);
1919

20-
if (len > end - start) {
20+
if (len > maxLen) {
21+
maxLen = len;
2122
start = i - Math.floor((len - 1) / 2);
22-
end = i + Math.floor(len / 2);
2323
}
2424
}
2525

26-
return str.substring(start, end + 1);
26+
return str.substr(start, maxLen);
2727
}
2828

2929
/**

merge-sort.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,54 @@ function mergeSort(arr) {
2323
const left = mergeSort(arr.slice(0, mid));
2424
const right = mergeSort(arr.slice(mid));
2525

26-
let i = 0;
27-
let j = 0;
28-
let sorted = [];
26+
return merge(left, right);
27+
}
28+
29+
function merge(left, right) {
30+
const arr = [];
2931

30-
while (i < left.length && j < right.length) {
31-
if (left[i] < right[j]) {
32-
sorted.push(left[i++]);
32+
while (left.length && right.length) {
33+
if (left[0] < right[0]) {
34+
arr.push(left.shift());
3335
} else {
34-
sorted.push(right[j++]);
36+
arr.push(right.shift());
3537
}
3638
}
3739

38-
while (i < left.length) {
39-
sorted.push(left[i++]);
40-
}
40+
return arr.concat(left).concat(right);
41+
}
4142

42-
while (j < right.length) {
43-
sorted.push(right[j++]);
44-
}
43+
// function mergeSort(arr) {
44+
// if (arr.length <= 1) {
45+
// return arr;
46+
// }
4547

46-
return sorted;
47-
}
48+
// const mid = Math.floor(arr.length / 2);
49+
// const left = mergeSort(arr.slice(0, mid));
50+
// const right = mergeSort(arr.slice(mid));
51+
52+
// let i = 0;
53+
// let j = 0;
54+
// let sorted = [];
55+
56+
// while (i < left.length && j < right.length) {
57+
// if (left[i] < right[j]) {
58+
// sorted.push(left[i++]);
59+
// } else {
60+
// sorted.push(right[j++]);
61+
// }
62+
// }
63+
64+
// while (i < left.length) {
65+
// sorted.push(left[i++]);
66+
// }
67+
68+
// while (j < right.length) {
69+
// sorted.push(right[j++]);
70+
// }
71+
72+
// return sorted;
73+
// }
4874

4975
console.log(mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));
5076
console.log(mergeSort([1, 4, 2, 8, 345, 3]));

0 commit comments

Comments
 (0)