-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudio9inclass.js
190 lines (157 loc) · 3.98 KB
/
studio9inclass.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
function bubblesort_array(A) {
const len = array_length(A);
for (let i = len - 1; i >= 1; i = i - 1) {
for (let j = 0; j < i; j = j + 1) {
if (A[j] > A[j + 1]) {
const temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
const AA = [3, 5, 2, 4, 1];
bubblesort_array(AA);
AA;
// time complexity: Theta(n^2)
*/
//Viswa
// function bubblesort_list(L) {
// const len = length(L);
// for(let i = len - 1; i >= 1; i = i - 1) {
// let curr = L;
// for(let j = 0; j < i; j = j + 1) {
// if (head(curr) > head(tail(curr))) {
// const temp = head(curr);
// set_head(curr, head(tail(curr)));
// set_head(tail(curr), temp);
// }
// curr = tail(curr);
// }
// }
// }
/*
function bubblesort_list(L) {
for (let i = 0; i < length(L); i = i + 1) {
if (is_null(L) || is_null(tail(L))) {
return null;
} else if (head(L) > head(tail(L))) {
const temp = head(L);
set_head(L, head(tail(L)));
set_head(tail(L), temp);
}
bubblesort_list(tail(L));
}
}
const LL = list(3, 5, 2, 4, 1);
bubblesort_list(LL);
LL; // should show [1, [2, [3, [4, [5, null]]]]]
*/
/*
const mem = [];
function read(n, k) {
return mem[n] === undefined
? undefined
: mem[n][k];
}
function write(n, k, value) {
if (mem[n] === undefined) {
mem[n] = [];
}
mem[n][k] = value;
}
function first_denomination(kinds_of_coins) {
return kinds_of_coins === 1 ? 5 :
kinds_of_coins === 2 ? 10 :
kinds_of_coins === 3 ? 20 :
kinds_of_coins === 4 ? 50 :
kinds_of_coins === 5 ? 100 : 0;
}
// The non-memoized version.
function cc(amount, kinds_of_coins) {
return amount === 0
? 1
: amount < 0 || kinds_of_coins === 0
? 0
: cc(amount, kinds_of_coins - 1)
+
cc(amount - first_denomination(kinds_of_coins),
kinds_of_coins);
}
// n = amount, k = kinds_of_coins
function mcc(n, k) {
if (n >= 0 && k >= 0 && read(n, k) !== undefined) {
return read(n, k);
} else {
const result = n === 0
? 1
: n < 0 || k === 0
? 0
: mcc(n, k - 1)
+
mcc(n - first_denomination(k), k);
if (n >= 0 && k >= 0) {
write(n, k, result);
}
return result;
}
}
// space: Theta(nk)
// time: Theta(nk)
// mcc(365, 5); // Expected result: 1730
*/
function rotate_matrix(M) {
const n = array_length(M);
function swap(r1, c1, r2, c2){
const temp = M[r1][c1];
M[r1][c1] = M[r2][c2];
M[r2][c2] = temp;
}
// matrix transpose
for (let i = 0; i < n; i = i + 1){
for (let j = i + 1; j < n; j = j + 1){
swap(i, j, j, i);
}
}
// reverse each row
const half_len = math_floor(n / 2);
for (let i = 0; i < n; i = i + 1){
for (let j = 0; j < half_len; j = j + 1){
swap(i, j, i, n - 1 - j);
}
}
}
function transpose(A) {
for( let i = 0; i < array_length(A); i = i + 1){
for( let j = i + 1; j < array_length(A[0]); j = j + 1){
const temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
}
}
function swap(A, i, j) {
let temp = A[i];
A[i] = A[j];
A[j] = temp;
}
function reverse_array(A) {
const len = array_length(A);
const half_len = math_floor(len / 2);
let i = 0;
while (i < half_len) {
const j = len - 1 - i;
swap(A, i, j);
i = i + 1;
}
}
function rotate(A) {
transpose(A);
for ( let i = 0; i < array_length(A); i = i + 1) {
reverse_array(A[i]);
}
}
const A = [[ 1, 2, 3, 4], [5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16]];
rotate(A);
A;