Skip to content

Commit d70595e

Browse files
authored
Merge branch 'master' into add-trapping-water
2 parents 826d75b + d272b1e commit d70595e

File tree

130 files changed

+10268
-1047
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+10268
-1047
lines changed

.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"endOfLine": "lf",
5+
"insertPragma": false,
6+
"printWidth": 80,
7+
"proseWrap": "preserve",
8+
"quoteProps": "as-needed",
9+
"requirePragma": false,
10+
"semi": false,
11+
"singleQuote": true,
12+
"tabWidth": 2,
13+
"trailingComma": "none",
14+
"useTabs": false
15+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Ciphers/ROT13.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Transcipher a ROT13 cipher
3+
* @param {String} text - string to be encrypted
4+
* @return {String} - decrypted string
5+
*/
6+
const transcipher = (text) => {
7+
const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
8+
const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
9+
const index = x => originalCharacterList.indexOf(x)
10+
const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x
11+
return text.split('').map(replace).join('')
12+
}
13+
14+
(() => {
15+
const messageToBeEncrypted = 'The quick brown fox jumps over the lazy dog'
16+
console.log(`Original Text = "${messageToBeEncrypted}"`)
17+
const rot13CipheredText = transcipher(messageToBeEncrypted)
18+
console.log(`Ciphered Text = "${rot13CipheredText}"`)
19+
const rot13DecipheredText = transcipher(rot13CipheredText)
20+
console.log(`Deciphered Text = "${rot13DecipheredText}"`)
21+
})()

Conversions/BinaryToDecimal.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
function binaryToDeicmal (binaryNumber) {
1+
const binaryToDecimal = (binaryString) => {
22
let decimalNumber = 0
3-
const binaryDigits = binaryNumber.split('').reverse() // Splits the binary number into reversed single digits
3+
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
44
binaryDigits.forEach((binaryDigit, index) => {
55
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
66
})
7-
console.log(`Decimal of ${binaryNumber} is ${decimalNumber}`)
7+
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
8+
return decimalNumber
89
}
910

10-
binaryToDeicmal('111001')
11-
binaryToDeicmal('101')
11+
(() => {
12+
binaryToDecimal('111001')
13+
binaryToDecimal('101')
14+
})()

Conversions/HexToRGB.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function hexStringToRGB (hexString) {
2-
var r = (hexString.substring(1, 3)).toUpperCase()
3-
var g = hexString.substring(3, 5).toUpperCase()
4-
var b = hexString.substring(5, 7).toUpperCase()
2+
var r = hexString.substring(0, 2)
3+
var g = hexString.substring(2, 4)
4+
var b = hexString.substring(4, 6)
55

66
r = parseInt(r, 16)
77
g = parseInt(g, 16)
@@ -11,4 +11,4 @@ function hexStringToRGB (hexString) {
1111
return obj
1212
}
1313

14-
console.log(hexStringToRGB('javascript rock !!'))
14+
console.log(hexStringToRGB('ffffff'))

Conversions/RGBToHex.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function RGBToHex (r, g, b) {
2+
if (
3+
typeof r !== 'number' ||
4+
typeof g !== 'number' ||
5+
typeof b !== 'number'
6+
) {
7+
throw new TypeError('argument is not a Number')
8+
}
9+
10+
const toHex = n => (n || '0').toString(16).padStart(2, '0')
11+
12+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
13+
}
14+
15+
console.log(RGBToHex(255, 255, 255) === '#ffffff')
16+
console.log(RGBToHex(255, 99, 71) === '#ff6347')

DIRECTORY.md

Lines changed: 112 additions & 8 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* A LinkedList based solution for Detect a Cycle in a list
3+
* https://en.wikipedia.org/wiki/Cycle_detection
4+
*/
5+
6+
function main () {
7+
/*
8+
Problem Statement:
9+
Given head, the head of a linked list, determine if the linked list has a cycle in it.
10+
11+
Note:
12+
* While Solving the problem in given link below, don't use main() function.
13+
* Just use only the code inside main() function.
14+
* The purpose of using main() function here is to aviod global variables.
15+
16+
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
17+
*/
18+
const head = '' // Reference to head is given in the problem. So please ignore this line
19+
let fast = head
20+
let slow = head
21+
22+
while (fast != null && fast.next != null && slow != null) {
23+
fast = fast.next.next
24+
slow = slow.next
25+
if (fast === slow) {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
32+
main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* A LinkedList based solution for Rotating a List to the right by k places
3+
*/
4+
5+
function main () {
6+
/*
7+
Problem Statement:
8+
Given a linked list, rotate the list to the right by k places, where k is non-negative.
9+
10+
Note:
11+
* While Solving the problem in given link below, don't use main() function.
12+
* Just use only the code inside main() function.
13+
* The purpose of using main() function here is to aviod global variables.
14+
15+
Link for the Problem: https://leetcode.com/problems/rotate-list/
16+
*/
17+
// Reference to both head and k is given in the problem. So please ignore below two lines
18+
let head = ''
19+
let k = ''
20+
let i = 0
21+
let current = head
22+
while (current) {
23+
i++
24+
current = current.next
25+
}
26+
k %= i
27+
current = head
28+
let prev = null
29+
while (k--) {
30+
if (!current || !current.next) {
31+
return current
32+
} else {
33+
while (current.next) {
34+
prev = current
35+
current = current.next
36+
}
37+
prev.next = current.next
38+
current.next = head
39+
head = current
40+
}
41+
}
42+
return head
43+
}
44+
45+
main()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class Node {
2+
constructor (data, next = null) {
3+
this.data = data
4+
this.next = next
5+
}
6+
}
7+
8+
class SinglyCircularLinkedList {
9+
constructor () {
10+
this.head = null
11+
this.size = 0
12+
}
13+
14+
insert (data) {
15+
const node = new Node(data)
16+
17+
if (!this.head) {
18+
node.next = node
19+
this.head = node
20+
this.size++
21+
} else {
22+
node.next = this.head
23+
24+
let current = this.head
25+
26+
while (current.next.data !== this.head.data) {
27+
current = current.next
28+
}
29+
30+
current.next = node
31+
this.size++
32+
}
33+
}
34+
35+
insertAt (index, data) {
36+
const node = new Node(data)
37+
38+
if (index < 0 || index > this.size) return
39+
40+
if (index === 0) {
41+
this.head = node
42+
this.size = 1
43+
return
44+
}
45+
46+
let previous
47+
let count = 0
48+
let current = this.head
49+
50+
while (count < index) {
51+
previous = current
52+
current = current.next
53+
count++
54+
}
55+
56+
node.next = current
57+
previous.next = node
58+
this.size++
59+
}
60+
61+
remove () {
62+
if (!this.head) return
63+
64+
let prev
65+
let current = this.head
66+
67+
while (current.next !== this.head) {
68+
prev = current
69+
current = current.next
70+
}
71+
72+
prev.next = this.head
73+
this.size--
74+
}
75+
76+
printData () {
77+
let count = 0
78+
let current = this.head
79+
80+
while (current !== null && count !== this.size) {
81+
console.log(current.data + '\n')
82+
current = current.next
83+
count++
84+
}
85+
}
86+
}
87+
88+
const ll = new SinglyCircularLinkedList()
89+
90+
ll.insert(10)
91+
ll.insert(20)
92+
ll.insert(30)
93+
ll.insert(40)
94+
ll.insert(50)
95+
ll.insertAt(5, 60)
96+
ll.remove(5)
97+
ll.printData()

Dynamic-Programming/ClimbingStairs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* You are climbing a stair case. It takes n steps to reach to the top.
3+
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
*/
5+
6+
const climbStairs = (n) => {
7+
let prev = 0
8+
let cur = 1
9+
let temp
10+
11+
for (let i = 0; i < n; i++) {
12+
temp = prev
13+
prev = cur
14+
cur += temp
15+
}
16+
return cur
17+
}
18+
19+
const main = () => {
20+
const number = 5
21+
22+
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(5))
23+
}
24+
25+
// testing
26+
main()

Dynamic-Programming/EditDistance.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Wikipedia -> https://en.wikipedia.org/wiki/Edit_distance
3+
4+
Q. -> Given two strings `word1` and `word2`. You can perform these operations on any of the string to make both strings similar.
5+
- Insert
6+
- Remove
7+
- Replace
8+
Find the minimum operation cost required to make both same. Each operation cost is 1.
9+
10+
Algorithm details ->
11+
time complexity - O(n*m)
12+
space complexity - O(n*m)
13+
*/
14+
15+
const minimumEditDistance = (word1, word2) => {
16+
const n = word1.length
17+
const m = word2.length
18+
const dp = new Array(m + 1).fill(0).map(item => [])
19+
20+
/*
21+
fill dp matrix with default values -
22+
- first row is filled considering no elements in word2.
23+
- first column filled considering no elements in word1.
24+
*/
25+
26+
for (let i = 0; i < n + 1; i++) {
27+
dp[0][i] = i
28+
}
29+
30+
for (let i = 0; i < m + 1; i++) {
31+
dp[i][0] = i
32+
}
33+
34+
/*
35+
indexing is 1 based for dp matrix as we defined some known values at first row and first column/
36+
*/
37+
38+
for (let i = 1; i < m + 1; i++) {
39+
for (let j = 1; j < n + 1; j++) {
40+
const letter1 = word1[j - 1]
41+
const letter2 = word2[i - 1]
42+
43+
if (letter1 === letter2) {
44+
dp[i][j] = dp[i - 1][j - 1]
45+
} else {
46+
dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) + 1
47+
}
48+
}
49+
}
50+
51+
return dp[m][n]
52+
}
53+
54+
const main = () => {
55+
console.log(minimumEditDistance('horse', 'ros'))
56+
console.log(minimumEditDistance('cat', 'cut'))
57+
console.log(minimumEditDistance('', 'abc'))
58+
console.log(minimumEditDistance('google', 'glgool'))
59+
}
60+
61+
main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://en.wikipedia.org/wiki/Fibonacci_number
2+
3+
const fibonacci = (N) => {
4+
// creating array to store values
5+
const memo = new Array(N + 1)
6+
memo[0] = 0
7+
memo[1] = 1
8+
for (let i = 2; i <= N; i++) {
9+
memo[i] = memo[i - 1] + memo[i - 2]
10+
}
11+
return memo[N]
12+
}
13+
14+
// testing
15+
(() => {
16+
const number = 5
17+
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
18+
})()

0 commit comments

Comments
 (0)