Skip to content

Commit b1e82c7

Browse files
committed
Changes?
1 parent 6328d29 commit b1e82c7

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

module-7-programming-assignment.html

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@
2424

2525
// 1. Implement fibonnacci using a recursive function and test your code.
2626

27+
function fibonacci(num) {
28+
if (num == 1)
29+
return 0;
30+
if (num == 2)
31+
return 1;
32+
return fibonacci(num - 1) + fibonacci(num - 2);
33+
}
2734

35+
console.log("Fibonacci of 5 is: " +fibonacci(5));
36+
console.log("Fibonacci of 13 is: " +fibonacci(13));
2837

2938
//---------------------//
3039
// Dynamic programming //
@@ -34,7 +43,40 @@
3443
// 1. Implement the minimum coin change problem using dynamic programming.
3544
// Test your code.
3645

37-
46+
function MinCoinChange(coins) {
47+
var coins = coins;
48+
var cache = {};
49+
50+
this.makeChange = function(amount) {
51+
var me = this;
52+
if (!amount) {
53+
return [];
54+
}
55+
if (cache[amount]) {
56+
return cache[amount];
57+
}
58+
var min = [], newMin, newAmount;
59+
for (var i=0; i<coins.length; i++) {
60+
var coin = coins[i];
61+
newAmount = amount - coin;
62+
if (newAmount >= 0) {
63+
newMin = me.makeChange(newAmount);
64+
}
65+
if (
66+
newAmount >= 0 &&
67+
(newMin.length < min.length - 1 || !min.length)
68+
&& (newMin.length || !newAmount))
69+
{
70+
min = [coin].concat(newMin);
71+
console.log('new Min ' + min + ' for ' + amount);
72+
}
73+
}
74+
return (cache[amount] = min);
75+
};
76+
}
77+
78+
var minCoinChange = new MinCoinChange([1, 5, 10, 25]);
79+
console.log(minCoinChange.makeChange(36));
3880

3981
//-------------------//
4082
// Greedy algorithms //
@@ -44,6 +86,24 @@
4486
// 1. Implement the minimum coin change problem using a greedy algorithm.
4587
// Test your code.
4688

89+
function minCoinChange(coins) {
90+
var coins = coins;
91+
92+
this.makeChange = function(amount) {
93+
var change = [];
94+
total = 0;
95+
for (var i=coins.length; i>=0; i--) {
96+
var coin = coins[i];
97+
while (total + coinm <- amount) {
98+
change.push(coin);
99+
total += coin;
100+
}
101+
}
102+
return change;
103+
};
104+
}
105+
var minCoinChange = new MinCoinChange([1, 5, 10, 25]);
106+
console.log(minCoinChange.makeChange(36));
47107

48108

49109
//----------------------------------------//
@@ -54,9 +114,39 @@
54114
// 1. Solve the problem of obtaining all the positive numbers in an array using
55115
// imperative style.
56116

117+
var printPosNums = funtion(array) {
118+
for (var i=0; i<array.length; i++) {
119+
if (array[i] >= 0) {
120+
console.log(array[i]);
121+
}
122+
}
123+
}
124+
125+
let newNumsArray = [-4, -5, 4, 8, -1, 0, 13];
126+
127+
printPosNums(newNumsArray);
128+
57129
// 2. Solve the problem of obtaining all the positive numbers in an array using
58130
// functional style.
59131

132+
var forEach = function(array, action) {
133+
let array = array;
134+
135+
for (var i = 0; i<array.length; i++) {
136+
if (isPos(array[i])) {
137+
console.log("The value is " + array[i]);
138+
139+
}
140+
}
141+
142+
this.isPos(num) {
143+
if (num > 0) {
144+
return true;
145+
}
146+
}
147+
148+
149+
}
60150

61151

62152
</script>

module-7-programming-project.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,61 @@
2424

2525
// 1. Implement factorial using a recursive function and test your code.
2626

27+
function factorial(num){
28+
return factorial((num - 1) * num);
29+
}
30+
31+
32+
console.log(factorial(12));
33+
2734
// 2. Implement a function that returns the complete fibonacci sequence in an
2835
// array for a given number.
2936

37+
function fibSeq(num) {
38+
fibArray = [];
39+
fibArray = fibSeq(num - 1) + (num - 2);
40+
41+
return fibArray;
42+
}
43+
3044
// 3. Implement finding the longest common subsequence using dynamic
3145
// programming. Test your code.
3246

47+
48+
3349
// 4. Solve the problem of summing all the numbers in an array using
3450
// imperative style.
3551

52+
var sumArray = function(array) {
53+
let sum = 0;
54+
55+
for (var i=0; i<array.length; i++) {
56+
sum = sum + array[i];
57+
}
58+
59+
return sum;
60+
}
61+
62+
let impArray = sumArray([3, 5, 6, 10, 8]);
63+
console.log("The sum is " + impArray);
64+
65+
66+
3667
// 5. Solve the problem of summing all the numbers in an array using
3768
// functional style.
3869

70+
71+
72+
73+
3974
// 6. Solve problem 14 of Project Euler: https://projecteuler.net/problem=14
4075
// Note: This may be a bit challenging. Try to get as far as you can.
4176
// Instead of one million you could use a smaller number for testing.
4277

4378

4479

80+
81+
4582
</script>
4683
</head>
4784
<body>

module-8-programming-assignment.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
return true;
3535
}
3636

37+
38+
3739
// 2. Write a program that finds the minimum element in a stack in O(1) time
3840
// complexity. Note that earlier solutions searched through the elements on
3941
// the stack but that leads to an O(n) solution.
4042
// NOTE: this question is sometimes asked on interviews.
4143

44+
//reorder the stack
45+
//split stack in half
4246

4347

4448
//----------------------------//
@@ -48,6 +52,9 @@
4852

4953
// 1. Solve Project Euler problem 1 (https://projecteuler.net/problem=1).
5054

55+
56+
57+
5158
// 2. Solve Project Euler problem 2 (https://projecteuler.net/problem=2).
5259

5360

0 commit comments

Comments
 (0)