-
Notifications
You must be signed in to change notification settings - Fork 269
Classics #10
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
Merged
Classics #10
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
12f9ba2
--update: initial implementation
ashokdey c376de5
--fix: edge case of upper case alphabet
ashokdey c801b3e
--fix: missing case for -ve new index
ashokdey 76b9ace
--update: final implementation
ashokdey 408dc13
--fix: spell fix
ashokdey 479e925
--update: get pair of index for two numbers sum to N
ashokdey 20280c8
--update: replaced indexof with Map(), throw error on missing arguments
ashokdey c2eec66
--update: added fibonacci
ashokdey dbd3dbd
--update: added optimized fibonacci using cache
ashokdey 8a9d8b8
--update: 32-bit signed int reversal
ashokdey f04d86c
--fix: removed outdated files
ashokdey 602df1f
--fix-conflict: got files from master
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/] | ||
* @param {String} str | ||
* @param {Number} num | ||
*/ | ||
|
||
function caesarCipher(str, num) { | ||
if (!num) throw new Error('Missing argument: num'); | ||
|
||
const lowerCaseString = str.toLowerCase(); | ||
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split(''); | ||
const totalAlphabets = alphabets.length; | ||
let result = ''; | ||
|
||
// handle large number, like 300 or -300 | ||
num %= totalAlphabets; | ||
|
||
const alphabetsMap = new Map(); | ||
|
||
for (const index in alphabets) { | ||
alphabetsMap[alphabets[index]] = index; | ||
} | ||
|
||
for (let index in lowerCaseString) { | ||
// get the current character | ||
const currentCharacter = lowerCaseString[index]; | ||
|
||
// if character is space, add it to the result and continue to next | ||
if (currentCharacter === ' ') { | ||
result += currentCharacter; | ||
continue; | ||
} | ||
|
||
// determine the new index | ||
/** | ||
* const currentIndex = alphabets.indexOf(currentCharacter); | ||
* | ||
* With indexOf complexity will be O(n*26) | ||
* With Map complexity will be O(n). | ||
*/ | ||
const currentIndex = Number(alphabetsMap[currentCharacter]); | ||
let newIndex = currentIndex + num; | ||
|
||
// if the index passes 25, restart from 0 | ||
if (newIndex > totalAlphabets - 1) { | ||
newIndex -= totalAlphabets; | ||
} | ||
|
||
if (newIndex < 0) { | ||
newIndex = totalAlphabets + newIndex; | ||
} | ||
|
||
// check if the character in original string was upper case | ||
if (str[index] === str[index].toUpperCase()) { | ||
result += alphabets[newIndex].toUpperCase(); | ||
} else { | ||
result += alphabets[newIndex]; | ||
} | ||
} | ||
return result; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// the algorithm has time complexity of O(n^2), very bad! | ||
function fibonacci(position) { | ||
// if position is 1 or 2, the number in fibonacci sequence will be 1 | ||
if (position < 3) { | ||
return 1; | ||
} | ||
// else the element in fibonacci sequence will be the sum of | ||
// element at position(p) (p -1) and (p - 2) | ||
return fibonacci(position - 2) + fibonacci(position - 1); | ||
} | ||
|
||
/** | ||
* Memoization. In computing, memoization or memoisation is an | ||
* optimization technique used primarily to speed up computer | ||
* programs by storing the results of expensive function | ||
* calls and returning the cached result when the | ||
* same inputs occur again | ||
*/ | ||
|
||
// Linear time, test with index as 510 for both the functions | ||
function fibonacciMemoized(index, cache) { | ||
cache = cache || []; | ||
|
||
if (cache[index]) { | ||
return cache[index]; | ||
} else { | ||
if (index < 3) { | ||
return 1; | ||
} else { | ||
cache[index] = | ||
fibonacciMemoized(index - 1, cache) + | ||
fibonacciMemoized(index - 2, cache); | ||
} | ||
} | ||
return cache[index]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,43 @@ | ||
const { reverseNumber } = require('.'); | ||
const { reverseNumber, reverse32BitInt } = require('.'); | ||
|
||
describe('Reverse Numbers', () => { | ||
it('Should return a number', () => { | ||
expect(typeof reverseNumber(1) === 'number'); | ||
}); | ||
describe('Normal Reverse', () => { | ||
it('Should return a number', () => { | ||
expect(typeof reverseNumber(1) === 'number'); | ||
}); | ||
|
||
it('Should reverse 45 to 54', () => { | ||
expect(reverseNumber(45)).toEqual(54); | ||
}); | ||
it('Should reverse 45 to 54', () => { | ||
expect(reverseNumber(45)).toEqual(54); | ||
}); | ||
|
||
it('Should reverse -2 to -2', () => { | ||
expect(reverseNumber(-2)).toEqual(-2); | ||
}); | ||
it('Should reverse -2 to -2', () => { | ||
expect(reverseNumber(-2)).toEqual(-2); | ||
}); | ||
|
||
it('Should reverse -1234567 to -7654321', () => { | ||
expect(reverseNumber(-1234567)).toEqual(-7654321); | ||
}); | ||
|
||
it('Should reverse -1234567 to -7654321', () => { | ||
expect(reverseNumber(-1234567)).toEqual(-7654321); | ||
it('Should throw error for invalid argument', () => { | ||
expect(() => reverseNumber('hello')).toThrow('Invalid Argument'); | ||
}); | ||
}); | ||
|
||
it('Should throw error for invalid argument', () => { | ||
expect(() => reverseNumber('hello')).toThrow('Invalid Argument'); | ||
describe('32-bit signed integer reversal', () => { | ||
it('Should return a number', () => { | ||
expect(typeof reverse32BitInt(1) === 'number'); | ||
}); | ||
|
||
it('Should reverse 123 to 321', () => { | ||
expect(reverse32BitInt(123)).toEqual(321); | ||
}); | ||
|
||
it('Should reverse -871 to -178', () => { | ||
expect(reverse32BitInt(-871)).toEqual(-178); | ||
}); | ||
|
||
it('Should return 0 for 1534236469 because of overflow when reversed', () => { | ||
expect(reverse32BitInt(1534236469)).toEqual(0); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to solve problem for input > 2^31 -1 .
It is also possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it can be done, I tried that before adding the
return 0;
block. I simply posted the solution for one of the coding challenge platforms.