Skip to content

Commit 29d2a84

Browse files
committed
Dynamic-Programming : remove live code & console.log, leave examples as comments.
1 parent 449ef45 commit 29d2a84

14 files changed

+95
-124
lines changed

Dynamic-Programming/ClimbingStairs.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,4 @@ const climbStairs = (n) => {
1616
return cur
1717
}
1818

19-
const main = () => {
20-
const number = 5
21-
22-
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(number))
23-
}
24-
25-
// testing
26-
main()
19+
export { climbStairs }

Dynamic-Programming/EditDistance.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,4 @@ const minimumEditDistance = (word1, word2) => {
5151
return dp[m][n]
5252
}
5353

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()
54+
export { minimumEditDistance }

Dynamic-Programming/FibonacciNumber.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@ const fibonacci = (N) => {
1111
return memo[N]
1212
}
1313

14-
// testing
15-
(() => {
16-
const number = 5
17-
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
18-
})()
14+
export { fibonacci }

Dynamic-Programming/FindMonthCalendar.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Month {
1313
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
1414
}
1515

16-
printCal (days, startDay) {
17-
console.log('M T W Th F S Su')
16+
printCal (days, startDay, output = value => console.log(value)) {
17+
output('M T W Th F S Su')
1818
const dates = []; let i
1919
for (i = 1; i <= days; i++) {
2020
dates.push(i)
@@ -30,7 +30,7 @@ class Month {
3030
row += ' '
3131
}
3232
}
33-
console.log(row)
33+
output(row)
3434
if (dates.length === 0) break
3535
}
3636
}
@@ -108,6 +108,7 @@ class Month {
108108
}
109109
}
110110

111-
// testing
112-
const x = new Month()
113-
x.generateMonthCal('1/2021')
111+
export { Month }
112+
113+
// const x = new Month()
114+
// x.generateMonthCal('1/2021')

Dynamic-Programming/LevenshteinDistance.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function costOfSubstitution (x, y) {
1717
return x === y ? 0 : 1
1818
}
1919

20+
// Levenshtein distance between x and y
2021
function calculate (x, y) {
2122
const dp = new Array(x.length + 1)
2223
for (let i = 0; i < x.length + 1; i++) {
@@ -38,12 +39,4 @@ function calculate (x, y) {
3839
return dp[x.length][y.length]
3940
}
4041

41-
function main () {
42-
const x = '' // enter your string here
43-
const y = '' // enter your string here
44-
45-
console.log('Levenshtein distance between ' + x + ' and ' + y + ' is: ')
46-
console.log(calculate(x, y))
47-
}
48-
49-
main()
42+
export { calculate }

Dynamic-Programming/LongestCommonSubsequence.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ function longestCommonSubsequence (x, y, str1, str2, dp) {
2222
}
2323
}
2424

25-
function main () {
26-
const str1 = 'ABCDGH'
27-
const str2 = 'AEDFHR'
28-
const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0))
29-
const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp)
30-
console.log(res)
31-
}
25+
// Example
26+
27+
// const str1 = 'ABCDGH'
28+
// const str2 = 'AEDFHR'
29+
// const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0))
30+
// const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp)
3231

33-
main()
32+
export { longestCommonSubsequence }

Dynamic-Programming/LongestIncreasingSubsequence.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* https://en.wikipedia.org/wiki/Longest_increasing_subsequence
44
*/
55

6-
function main () {
7-
const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
6+
// Return the length of the Longest Increasing Subsequence, given array x
7+
function longestIncreasingSubsequence (x) {
88
const length = x.length
99
const dp = Array(length).fill(1)
1010

@@ -21,7 +21,7 @@ function main () {
2121
}
2222
}
2323

24-
console.log('Length of Longest Increasing Subsequence is:', res)
24+
return res
2525
}
2626

27-
main()
27+
export { longestIncreasingSubsequence }

Dynamic-Programming/MaxNonAdjacentSum.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ function maximumNonAdjacentSum (nums) {
1919
return Math.max(maxExcluding, maxIncluding)
2020
}
2121

22-
function main () {
23-
console.log(maximumNonAdjacentSum([1, 2, 3]))
24-
console.log(maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6]))
25-
console.log(maximumNonAdjacentSum([-1, -5, -3, -7, -2, -2, -6]))
26-
console.log(maximumNonAdjacentSum([499, 500, -3, -7, -2, -2, -6]))
27-
}
22+
// Exmaple
23+
24+
// maximumNonAdjacentSum([1, 2, 3]))
25+
// maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6]))
26+
// maximumNonAdjacentSum([-1, -5, -3, -7, -2, -2, -6]))
27+
// maximumNonAdjacentSum([499, 500, -3, -7, -2, -2, -6]))
2828

29-
main()
29+
export { maximumNonAdjacentSum }

Dynamic-Programming/MinimumCostPath.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@ const minCostPath = (matrix) => {
2626
return moves[n - 1][m - 1]
2727
}
2828

29-
const main = () => {
30-
console.log(
31-
minCostPath([
32-
[2, 1],
33-
[3, 1],
34-
[4, 2]
35-
])
36-
)
37-
console.log(
38-
minCostPath([
39-
[2, 1, 4],
40-
[2, 1, 3],
41-
[3, 2, 1]
42-
])
43-
)
44-
}
29+
export { minCostPath }
30+
31+
// Example
32+
33+
// minCostPath([
34+
// [2, 1],
35+
// [3, 1],
36+
// [4, 2]
37+
// ])
38+
39+
// minCostPath([
40+
// [2, 1, 4],
41+
// [2, 1, 3],
42+
// [3, 2, 1]
43+
// ])
4544

46-
main()

Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ function NumberOfSubsetSum (array, sum) {
2323
return dp[sum]
2424
}
2525

26-
function main () {
27-
const array = [1, 1, 2, 2, 3, 1, 1]
28-
const sum = 4
29-
const result = NumberOfSubsetSum(array, sum)
30-
console.log(result)
31-
}
32-
main()
26+
// example
27+
28+
// const array = [1, 1, 2, 2, 3, 1, 1]
29+
// const sum = 4
30+
// const result = NumberOfSubsetSum(array, sum)
31+
32+
export { NumberOfSubsetSum }

Dynamic-Programming/Shuf.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ function randomizeOutputFromDataset (datasetSource, output) {
7676
return newOutput
7777
}
7878

79-
const main = () => {
80-
/**
81-
* Generates a random range of data, with values between 0 and 2^31 - 1
82-
* @param {number} length The number of data items to generate
83-
* @returns {Iterable<number>} Random iterable data
84-
*/
85-
function * generateRandomData (length) {
86-
const maxValue = Math.pow(2, 31) - 1
87-
for (let i = 0; i < length; i++) {
88-
yield Math.floor(Math.random() * maxValue)
89-
}
90-
}
79+
// Example
9180

92-
const source = generateRandomData(1000)
93-
const result = shuf(source, 10)
94-
console.log(result)
81+
/**
82+
* Generates a random range of data, with values between 0 and 2^31 - 1
83+
* @param {number} length The number of data items to generate
84+
* @returns {Iterable<number>} Random iterable data
85+
*/
86+
function * generateRandomData (length) {
87+
const maxValue = Math.pow(2, 31) - 1
88+
for (let i = 0; i < length; i++) {
89+
yield Math.floor(Math.random() * maxValue)
90+
}
9591
}
96-
main()
92+
93+
// const source = generateRandomData(1000)
94+
// const result = shuf(source, 10)
95+
96+
export { shuf, generateRandomData }

Dynamic-Programming/SieveOfEratosthenes.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ function sieveOfEratosthenes (n) {
1818
return primes
1919
}
2020

21-
function main () {
22-
const n = 69 // number till where we wish to find primes
23-
const primes = sieveOfEratosthenes(n)
24-
for (let i = 2; i <= n; i++) {
25-
if (primes[i]) {
26-
console.log(i)
27-
}
28-
}
29-
}
21+
// Example
22+
23+
// const n = 69 // number till where we wish to find primes
24+
// const primes = sieveOfEratosthenes(n)
3025

31-
main()
26+
export { sieveOfEratosthenes }

Dynamic-Programming/SudokuSolver.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
const _board = [
2-
['.', '9', '.', '.', '4', '2', '1', '3', '6'],
3-
['.', '.', '.', '9', '6', '.', '4', '8', '5'],
4-
['.', '.', '.', '5', '8', '1', '.', '.', '.'],
5-
['.', '.', '4', '.', '.', '.', '.', '.', '.'],
6-
['5', '1', '7', '2', '.', '.', '9', '.', '.'],
7-
['6', '.', '2', '.', '.', '.', '3', '7', '.'],
8-
['1', '.', '.', '8', '.', '4', '.', '2', '.'],
9-
['7', '.', '6', '.', '.', '.', '8', '1', '.'],
10-
['3', '.', '.', '.', '9', '.', '.', '.', '.']
11-
]
121

132
const isValid = (board, row, col, k) => {
143
for (let i = 0; i < 9; i++) {
@@ -43,8 +32,18 @@ const sudokuSolver = (data) => {
4332
}
4433

4534
// testing
46-
(() => {
47-
if (sudokuSolver(_board)) {
48-
console.log(_board)
49-
}
50-
})()
35+
36+
// const board = [
37+
// ['.', '9', '.', '.', '4', '2', '1', '3', '6'],
38+
// ['.', '.', '.', '9', '6', '.', '4', '8', '5'],
39+
// ['.', '.', '.', '5', '8', '1', '.', '.', '.'],
40+
// ['.', '.', '4', '.', '.', '.', '.', '.', '.'],
41+
// ['5', '1', '7', '2', '.', '.', '9', '.', '.'],
42+
// ['6', '.', '2', '.', '.', '.', '3', '7', '.'],
43+
// ['1', '.', '.', '8', '.', '4', '.', '2', '.'],
44+
// ['7', '.', '6', '.', '.', '.', '8', '1', '.'],
45+
// ['3', '.', '.', '.', '9', '.', '.', '.', '.']
46+
// ]
47+
// sudokuSolver(board) // -> board updated by reference
48+
49+
export { sudokuSolver }

Dynamic-Programming/ZeroOneKnapsack.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const zeroOneKnapsack = (arr, n, cap, cache) => {
2020
}
2121
}
2222

23-
const main = () => {
23+
const example = () => {
2424
/*
2525
Problem Statement:
2626
You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity.
@@ -40,6 +40,8 @@ const main = () => {
4040
input.shift()
4141
const length = input.length
4242

43+
let output = []
44+
4345
let i = 0
4446
while (i < length) {
4547
const cap = Number(input[i].trim().split(' ')[0])
@@ -62,9 +64,11 @@ const main = () => {
6264
cache.push(temp)
6365
}
6466
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
65-
console.log(result)
67+
output.push(result)
6668
i += currlen + 1
6769
}
70+
71+
return output
6872
}
6973

70-
main()
74+
export { zeroOneKnapsack, example }

0 commit comments

Comments
 (0)