Skip to content

Commit 4ff441b

Browse files
authored
Merge pull request #10 from knaxus/classics
Classics
2 parents 181b1a6 + 602df1f commit 4ff441b

File tree

4 files changed

+172
-14
lines changed

4 files changed

+172
-14
lines changed

src/_Classics_/caeser_cipher/index.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/]
3+
* @param {String} str
4+
* @param {Number} num
5+
*/
6+
7+
function caesarCipher(str, num) {
8+
if (!num) throw new Error('Missing argument: num');
9+
10+
const lowerCaseString = str.toLowerCase();
11+
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
12+
const totalAlphabets = alphabets.length;
13+
let result = '';
14+
15+
// handle large number, like 300 or -300
16+
num %= totalAlphabets;
17+
18+
const alphabetsMap = new Map();
19+
20+
for (const index in alphabets) {
21+
alphabetsMap[alphabets[index]] = index;
22+
}
23+
24+
for (let index in lowerCaseString) {
25+
// get the current character
26+
const currentCharacter = lowerCaseString[index];
27+
28+
// if character is space, add it to the result and continue to next
29+
if (currentCharacter === ' ') {
30+
result += currentCharacter;
31+
continue;
32+
}
33+
34+
// determine the new index
35+
/**
36+
* const currentIndex = alphabets.indexOf(currentCharacter);
37+
*
38+
* With indexOf complexity will be O(n*26)
39+
* With Map complexity will be O(n).
40+
*/
41+
const currentIndex = Number(alphabetsMap[currentCharacter]);
42+
let newIndex = currentIndex + num;
43+
44+
// if the index passes 25, restart from 0
45+
if (newIndex > totalAlphabets - 1) {
46+
newIndex -= totalAlphabets;
47+
}
48+
49+
if (newIndex < 0) {
50+
newIndex = totalAlphabets + newIndex;
51+
}
52+
53+
// check if the character in original string was upper case
54+
if (str[index] === str[index].toUpperCase()) {
55+
result += alphabets[newIndex].toUpperCase();
56+
} else {
57+
result += alphabets[newIndex];
58+
}
59+
}
60+
return result;
61+
}

src/_Classics_/fibonacci/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// the algorithm has time complexity of O(n^2), very bad!
2+
function fibonacci(position) {
3+
// if position is 1 or 2, the number in fibonacci sequence will be 1
4+
if (position < 3) {
5+
return 1;
6+
}
7+
// else the element in fibonacci sequence will be the sum of
8+
// element at position(p) (p -1) and (p - 2)
9+
return fibonacci(position - 2) + fibonacci(position - 1);
10+
}
11+
12+
/**
13+
* Memoization. In computing, memoization or memoisation is an
14+
* optimization technique used primarily to speed up computer
15+
* programs by storing the results of expensive function
16+
* calls and returning the cached result when the
17+
* same inputs occur again
18+
*/
19+
20+
// Linear time, test with index as 510 for both the functions
21+
function fibonacciMemoized(index, cache) {
22+
cache = cache || [];
23+
24+
if (cache[index]) {
25+
return cache[index];
26+
} else {
27+
if (index < 3) {
28+
return 1;
29+
} else {
30+
cache[index] =
31+
fibonacciMemoized(index - 1, cache) +
32+
fibonacciMemoized(index - 2, cache);
33+
}
34+
}
35+
return cache[index];
36+
}

src/_Problems_/reverse-number/index.js

+41
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ function reverseNumber(num) {
1515
return reverse * Math.sign(num);
1616
}
1717

18+
/**
19+
*
20+
* Given a 32-bit signed integer, reverse digits of an integer.
21+
22+
Example 1:
23+
24+
Input: 123
25+
Output: 321
26+
Example 2:
27+
28+
Input: -123
29+
Output: -321
30+
Example 3:
31+
32+
Input: 1534236469
33+
Output: 0 // overflows
34+
Note:
35+
Assume we are dealing with an environment which could only
36+
store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1].
37+
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
38+
*/
39+
40+
function reverse32BitInt(x) {
41+
let isNegetive = 0;
42+
if (x < 0) {
43+
x *= -1;
44+
isNegetive = 1;
45+
}
46+
let reverse = 0;
47+
while (x >= 1) {
48+
const r = Math.floor(x % 10);
49+
reverse = reverse * 10 + r;
50+
x = Math.floor(x / 10);
51+
}
52+
if (reverse > 0x7fffffff) {
53+
return 0;
54+
}
55+
return isNegetive ? reverse * -1 : reverse;
56+
}
57+
1858
module.exports = {
1959
reverseNumber,
60+
reverse32BitInt
2061
};
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
const { reverseNumber } = require('.');
1+
const { reverseNumber, reverse32BitInt } = require('.');
22

33
describe('Reverse Numbers', () => {
4-
it('Should return a number', () => {
5-
expect(typeof reverseNumber(1) === 'number');
6-
});
4+
describe('Normal Reverse', () => {
5+
it('Should return a number', () => {
6+
expect(typeof reverseNumber(1) === 'number');
7+
});
78

8-
it('Should reverse 45 to 54', () => {
9-
expect(reverseNumber(45)).toEqual(54);
10-
});
9+
it('Should reverse 45 to 54', () => {
10+
expect(reverseNumber(45)).toEqual(54);
11+
});
1112

12-
it('Should reverse -2 to -2', () => {
13-
expect(reverseNumber(-2)).toEqual(-2);
14-
});
13+
it('Should reverse -2 to -2', () => {
14+
expect(reverseNumber(-2)).toEqual(-2);
15+
});
16+
17+
it('Should reverse -1234567 to -7654321', () => {
18+
expect(reverseNumber(-1234567)).toEqual(-7654321);
19+
});
1520

16-
it('Should reverse -1234567 to -7654321', () => {
17-
expect(reverseNumber(-1234567)).toEqual(-7654321);
21+
it('Should throw error for invalid argument', () => {
22+
expect(() => reverseNumber('hello')).toThrow('Invalid Argument');
23+
});
1824
});
1925

20-
it('Should throw error for invalid argument', () => {
21-
expect(() => reverseNumber('hello')).toThrow('Invalid Argument');
26+
describe('32-bit signed integer reversal', () => {
27+
it('Should return a number', () => {
28+
expect(typeof reverse32BitInt(1) === 'number');
29+
});
30+
31+
it('Should reverse 123 to 321', () => {
32+
expect(reverse32BitInt(123)).toEqual(321);
33+
});
34+
35+
it('Should reverse -871 to -178', () => {
36+
expect(reverse32BitInt(-871)).toEqual(-178);
37+
});
38+
39+
it('Should return 0 for 1534236469 because of overflow when reversed', () => {
40+
expect(reverse32BitInt(1534236469)).toEqual(0);
41+
});
2242
});
2343
});

0 commit comments

Comments
 (0)