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 }

0 commit comments

Comments
 (0)