-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-7-programming-assignment.html
157 lines (125 loc) · 3.58 KB
/
module-7-programming-assignment.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CMPS 260: Module 7 Programming Assignment</title>
<style>* { font-family: monospace; }</style>
<script>
// NOTE: You must implement the data structures using the no prototype approach.
// This is what the book uses, so you can copy it.
// See also: https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// NOTE: Please review the following links regularly:
// https://it.pointpark.edu/tutorials/arrays-vs-objects/
// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// https://it.pointpark.edu/tutorials/implementation-vs-interface/
//-----------//
// Recursion //
//-----------//
console.log("Recursion");
// 1. Implement fibonnacci using a recursive function and test your code.
function fibonacci(num) {
if (num == 1)
return 0;
if (num == 2)
return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
console.log("Fibonacci of 5 is: " +fibonacci(5));
console.log("Fibonacci of 13 is: " +fibonacci(13));
//---------------------//
// Dynamic programming //
//---------------------//
console.log("Dynamic programming");
// 1. Implement the minimum coin change problem using dynamic programming.
// Test your code.
function MinCoinChange(coins) {
var coins = coins;
var cache = {};
this.makeChange = function(amount) {
var me = this;
if (!amount) {
return [];
}
if (cache[amount]) {
return cache[amount];
}
var min = [], newMin, newAmount;
for (var i=0; i<coins.length; i++) {
var coin = coins[i];
newAmount = amount - coin;
if (newAmount >= 0) {
newMin = me.makeChange(newAmount);
}
if (
newAmount >= 0 &&
(newMin.length < min.length - 1 || !min.length)
&& (newMin.length || !newAmount))
{
min = [coin].concat(newMin);
console.log('new Min ' + min + ' for ' + amount);
}
}
return (cache[amount] = min);
};
}
var minCoinChange = new MinCoinChange([1, 5, 10, 25]);
console.log(minCoinChange.makeChange(36));
//-------------------//
// Greedy algorithms //
//-------------------//
console.log("Greedy algorithms");
// 1. Implement the minimum coin change problem using a greedy algorithm.
// Test your code.
function minCoinChange(coins) {
var coins = coins;
this.makeChange = function(amount) {
var change = [];
total = 0;
for (var i=coins.length; i>=0; i--) {
var coin = coins[i];
while (total + coinm <- amount) {
change.push(coin);
total += coin;
}
}
return change;
};
}
var minCoinChange = new MinCoinChange([1, 5, 10, 25]);
console.log(minCoinChange.makeChange(36));
//----------------------------------------//
// Introduction to functional programming //
//----------------------------------------//
console.log("Introduction to functional programming");
// 1. Solve the problem of obtaining all the positive numbers in an array using
// imperative style.
var printPosNums = funtion(array) {
for (var i=0; i<array.length; i++) {
if (array[i] >= 0) {
console.log(array[i]);
}
}
}
let newNumsArray = [-4, -5, 4, 8, -1, 0, 13];
printPosNums(newNumsArray);
// 2. Solve the problem of obtaining all the positive numbers in an array using
// functional style.
var forEach = function(array, action) {
let array = array;
for (var i = 0; i<array.length; i++) {
if (isPos(array[i])) {
console.log("The value is " + array[i]);
}
}
this.isPos(num) {
if (num > 0) {
return true;
}
}
}
</script>
</head>
<body>
See console!
</body>
</html>