Uploaded by jieyichin04

cs1101s-final-cheat-sheet-for-eyesight-training

advertisement
lOMoARcPSD|29220415
CS1101S Final Cheat Sheet for Eyesight Training
Programming Methodology (National University of Singapore)
Studocu is not sponsored or endorsed by any college or university
Downloaded by Jie Yi Chin (jieyichin04@gmail.com)
lOMoARcPSD|29220415
CS1101S Final Cheat Sheet by Wang Xiyu 23/24
Sortings:
Insertion Sort: L&A
function selection_sort(xs) {
function smallest(xs) {
return accumulate((x, y) => x < y ? x :
y,
head(xs), tail(xs));
}
if (is_null(xs)) {
return xs;
}
else {
const x = smallest(xs);
return pair(x, selection_sort(remove(x,
xs)));
}
}
function insertion_sort2(xs, cmp) {
function insert(x, xs, cmp) {
return is_null(xs)
? list(x)
: cmp(x, head(xs))
? pair(x, xs)
: pair(head(xs), insert(x,
tail(xs), cmp));
}
return is_null(xs)
? xs
: insert(head(xs),
insertion_sort2(tail(xs), cmp),
cmp);
}
function merge_sort(xs) {
const mid1 = math_floor(length(xs) / 2);
const mid2 = math_ceil(length(xs) / 2);
function merge(xs1, xs2) {
return is_null(xs1)
? xs2
: is_null(xs2)
? xs1
: head(xs1) >= head(xs2)
? pair(head(xs2), merge(xs1,
tail(xs2)))
: pair(head(xs1), merge(xs2,
tail(xs1)));
}
function first_list(xs, p) {
return p === 0
? null
: pair(head(xs), first_list(tail(xs), p 1));
}
function second_list(xs, p) {
return p === 0
? xs
: pair(head(reverse(xs)),
first_list(tail(reverse(xs)), p 1));
}
return is_null(xs) || is_null(tail(xs))
? xs
: merge(merge_sort(first_list(xs, mid1)),
merge_sort(second_list(xs, mid2)));
}
function bubblesort_list(L) {
const len = length(L);
for (let i = len - 1; i >= 1; i = i - 1) {
let p = L;
for (let j = 0; j < i; j = j + 1) {
if (head(p) > head(tail(p))) {
const temp = head(p);
set_head(p, head(tail(p)));
set_head(tail(p), temp);
}
p = tail(p);
}
}
// Note: list mutated;
}
function quicksort(xs) {
function partition(xs, p) {
return pair(filter((e) => e <= p, xs),
filter((e) => e > p, xs)
);
}
if (is_null(xs) || is_null(tail(xs))) {
return xs;
}
else {
const p = head(xs);
const partition_list = partition(xs, p);
const smaller_list =
tail(head(partition_list));
const larger_list =
tail(partition_list);
return append(quicksort(smaller_list),
pair(p, quicksort(larger_list)));
}}
Searching:
function linear_search(A, v) {
const len = array_length(A);
let i = 0;
while (i < len && A[i] !== v) {
i = i + 1; }
return i < len; }
function binary_search(A, v) { // ARRAY
function search(low, high) {
if (low > high) {
return false;
}
else {
const mid = math_floor((low +high) / 2);
return (v === A[mid] ||
(v < A[mid]? search(low, mid - 1)
:search(mid + 1, high)));
}}
return search(0, array_length(A) - 1);
}
function binary_search(xs, n) { // LIST
const end = length(xs) - 1;
function iter(s, e) {
if (s > e) {
return false;
}
else {
const m = math_floor((s + e) / 2);
const v = list_ref(xs, m);
return v === n ||
(v > n ? iter(s,m - 1) : iter(m + 1, e));
}
}
return iter(0, end);
}
function BST_search(x, bst) {
return is_null(bst)
? false
: x === head(bst)
? true
: x > head(bst)
? BST_search(x, head(tail(tail(bst))))
: BST_search(x, head(tail(bst)));
}
Memoisation
function memoize(f) {
const mem = [];
function mf(x) {
if (mem[x] !== undefined) {
return mem[x];
}
else {
const result = f(x);
mem[x] = result; // store in mem
return result;
}
}
return mf;
}
const mtrib = memoize((n) =>
n === 0 ? 0
: n === 1 ? 1 : n === 2 ? 1
: mtrib(n - 1) + mtrib(n - 2) + mtrib(n 3)
); // call memoized version instead
function two_d_memoize(f) {
const mem = [];
function read(x, y) {
return (mem[x] === undefined)
? undefined
: mem[x][y];
}
function write(x, y, value) {
if (mem[x] === undefined) {
mem[x] = [];
} else { }
mem[x][y] = value;
}
function mf(x, y) {
const mem_xy = read(x, y);
if (mem_xy !== undefined) {
return mem_xy;
}
else {
const result = f(x, y);
write(x, y, result);
return result;
}
}
return mf;
}
const memoed = two_d_memoize((n, m) => {…});
function memo_fun(fun) { //stream
let already_run = false;
let result = undefined;
function mfun() {
if (!already_run) {
result = fun();
already_run = true;
return result;
}
else {
return result;
}
}
return mfun;
}
const onesA = pair(1, () => ms("A", onesA));
function ms(m, s) {display(m); return s;}
stream_ref(onesA, 3); //"A","A","A", 1;
const onesB =pair(1, memo_fun(() => ms("B",
onesB)));
stream_ref(onesB, 3); //"B", 1
Data Processing
function permutation(xs) {
return is_null(xs)
? list(null)
: accumulate(append, null,
map(x => map(y => pair(x, y),
permutation(remove(x, xs))),
xs));
}
function combinations(xs, k) {
if (k === 0) { return list(null); }
else if (is_null(xs)) {
return null;
}
else {
const combi_A = combinations(tail(xs),
k - 1);
const combi_B =
combinations(tail(xs),k);
const A_help = map((x) =>
pair(head(xs), x), combi_A);
return append(A_help, combi_B);
}
}
Downloaded by Jie Yi Chin (jieyichin04@gmail.com)
function makeup_amount(x, coins) {
if (x === 0) {
return list(null);
} else if (x < 0 || is_null(coins)) {
return null;
} else {
const add_head = x => pair(head(coins), x);
const combi_B = map(add_head,
makeup_amount(x - head(coins), tail(coins)));
const combi_A = makeup_amount(x, tail(coins));
return append(combi_A, combi_B);
}
}
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;
function combrep(xs, len_str) {
let res = xs;
for (let i = 0; i < len_str - 1; i = i + 1)
{
res = accumulate(append, null,
map(x => map(y => x + y, xs),
res));
}
return res;
}
function choose(n, k) { // returns a number
if (k === 0) {return 1;}
return (n * choose(n - 1, k - 1)) / k;
}
function power_set(xs) { // returns power set
return accumulate(
(x, ss) => append(ss, map((s) => pair(x,
s),ss)),
list(null),
xs);
}
Stream Processing
function stream_append_pickle(xs, ys) {
return is_null(xs)
? ys()
: pair(head(xs), () =>
stream_append_pickle(
stream_tail(xs), ys));
}
function add_streams(s1, s2) {
if (is_null(s1)) {
return s2;
}
else if (is_null(s2)) {
return s1;
}
else {
return pair(head(s1) + head(s2),
() => add_streams(stream_tail(s1),
stream_tail(s2)));
}
}
function interleave_stream_append(s1, s2) {
return is_null(s1)
? s2
: pair(head(s1),
() => interleave_stream_append(s2,
stream_tail(s1)));
}
function mul_series(s1, s2) {
return pair(head(s1) * head(s2),
() => add_streams(scale_stream(head(s2),
stream_tail(s1)),
mul_series(s1,
stream_tail(s2))));
}
function loop(stream) {
function helper(s) {
return is_null(tail(s))
? pair(head(s), () => loop(stream))
: pair(head(s), () =>
helper(stream_tail(s)));
}
return helper(stream);
}
function zip_list_of_streams(xss) {
return pair(head(head(xss)),
() => zip_list_of_streams(append(tail(xss),
list(head(xss)))));
} // S1 item #1,…,Sn item #1, S1 item #2…
// Example: Fib from stream
const fibs = pair(0, () => pair(1,
() => add_streams(stream_tail(fibs),
fibs)));
Other Arr Processing Functions:
function back_append(x, A) {
A[array_length(y)] = x;
return A;
}
function front_append(x, A) {
const len = array_length(A);
let M = [0];
for (let i = 1; i < len + 1; i = i + 1) {
M[i] = A[i - 1];
}
return M;
}
lOMoARcPSD|29220415
function reverse_array(A) {
const len = array_length(A);
const halflen = math_floor(len / 2);
for (let i = 0; i < halflen; i = i + 1) {
let tmp = A[i];
A[i] = A[len - 1 - i];
A[len - 1 - i] = tmp;
}
return A;
}
function map_array(f, A) {
const len = array_length(A);
for (let i = 0; i < len; i = i + 1) {
A[i] = f(A[i]);
}
return A;
}
function filter_array(pred, A) {
const len = array_length(A);
let M =[];
let n = 0;
for (let i = 0; i < len; i = i + 1) {
if (pred(A[i])) {
M[n] = A[i];
n = n + 1;
}
else { }
}
return M;
}
function remov_arr_elem(A, index_start,
index_end) {
const len = array_length(A);
let M =[];
let n = 0;
for (let i = 0; i < len; i = i + 1) {
if (i >= index_start && i <= index_end)
{
}
else {
M[n] = A[i];
n = n + 1;
}
}
return M;
}
function transpose(M) {
const row = array_length(M);
let N = [];
const col = array_length(M[0]);
function write(n, k, value) {
if (N[n] === undefined) {
N[n] = [];
}
N[n][k] = value;
}
for (let i = 0; i < col; i = i + 1) {
for (let j = 0; j < row; j = j + 1) {
write(i, j, M[j][i]);
}
}
return N;
}
function combinations_rep(A, len) {
const size = array_length(A);
const total_comb = math_pow(size, len);
let result = [];
for (let i = 0; i < len; i = i + 1) {
result[i] = [];
result[i][total_comb - 1] = undefined;
}
for (let j = 1; j < len + 1; j = j + 1) {
const segment_length = total_comb /
(math_pow(size, j));
for (let k = 0; k < total_comb; k = k +
1) {
const index = math_floor(k/segment_length) %
size;
result[j - 1][k] = A[index];
}
}
return transpose(result);
}
Other List Processing Functions
function multizip(ls) {
return is_null(head(ls))
? null
: pair(map(x => head(x), ls),
multizip(map(x => tail(x), ls)));
}
function fast_power(b, n) {
return n % 2 === 0
? n === 0
? 1
: fast_power(b, n / 2) * fast_power(b, n /
2)
: n === 0
? 1
: b * fast_power(b, n - 1);
}
Element shifting
function arr_shift_right(A) {
const len = array_length(A);
let M = [];
M[0] = A[len - 1];
for (let i = 0; i < len - 1; i = i + 1) {
M[i + 1] = A[i];
}
return M; }
function arr_shift_left(A) {
const len = array_length(A);
let M = [];
M[len - 1] = A[0];
for (let i = 0; i < len - 1; i = i + 1) {
M[i] = A[i + 1];
}
return M; }
function list_shift_right(xs) {
const rev = reverse(xs);
return pair(head(rev),
reverse(tail(rev)));
}
function list_shift_left(xs) {
return reverse(pair(head(xs),
reverse(tail(xs)))); }
Lst <-> Arr conversion
function list_to_array(xs) {
if (is_null(xs)) {
return undefined;
}
else if (!is_list(xs)) {
return xs;
}
else {
xs = map(x => list_to_array(x), xs);
let M =[];
for (let i = 0; i < length(xs); i = i +
1) {
M[i] = list_ref(xs, i);
}
return M;
}}
function list_to_arr(xs) {
function append(x, y) {
y[array_length(y)] = x;
return y;
}
if (is_null(xs)) {
return undefined;
}
else if (!is_list(xs)) {
return xs;
}
// To retain pair structures:
(to not prodece sublists of length 2 but
pairs)
// else if (array_length(A) === 2) {
//
return pair(A[0], A[1]); }
else {
xs = reverse(map(x => list_to_arr(x), xs));
return accumulate((x, y)=>append(x, y), [],
xs);
}}
function arr_to_list(A) {
function map_array(f, A) {
const len = array_length(A);
for (let i = 0; i < len; i = i + 1) {
A[i] = f(A[i]);
}
return A; }
if (is_undefined(A)) {
return null; }
else if (!is_array(A)) {
return A; }
else {
A = map_array(x => arr_to_list(x), A);
let xs = null;
for (let i = 0; i < array_length(A); i=i+1)
{
xs = pair(A[array_length(A) - i - 1],
xs);}
return xs;
}}
Diagnolisation
function expand(M) {
const size = array_length(M);
M[2*size - 2] = undefined;
for (let j = size; j < 2*size - 1; j = j +
1) {
M[j] = [];
for (let k = 0; k < size; k = k + 1) {
M[j][size - 1] = undefined;
}
}
for (let i = 0; i < size; i = i + 1) {
const len = array_length(M[i]);
M[i][2*size - 2] = undefined;
}
return M;
}
function map_array(f, A) {
const len = array_length(A);
for (let i = 0; i < len; i = i + 1) {
A[i] = f(A[i]);
}
return A;
}
function flatten_list(xs) {
return accumulate(append, null, xs);
}
function remov_ex_nulls(xs) {
return accumulate(
(x, y)=>(!is_null(x)) ? pair(x, y) : y, null,
xs);
}
function straightened_array(M, n) {
let A = [];
const coord = 2 * n - 2;
for (let i = 0; i <= coord; i = i + 1) {
A[i] = [];
if (i % 2 !== 0) {
for (let j = 0; j < i + 1; j = j +
1) {
A[i][j] = M[j][i - j];
}
}
else {
for (let j = 0; j < i + 1; j = j +
1) {
A[i][j] = M[i - j][j];
}
}
}
return A; }
Downloaded by Jie Yi Chin (jieyichin04@gmail.com)
function main(M) {
const size = array_length(M[0]);
return
remov_ex_nulls(flatten_list(arr_to_list(straig
htened_array(expand(M), size))));
}
function mergeA(xs, ys) {
if (is_null(xs) && is_null(ys)) {
return null;
}
else if (is_null(xs)) {
return pair(head(ys), mergeA(xs,
tail(ys)));
}
else if (is_null(ys)) {
return pair(head(xs), mergeA(tail(xs),
ys));
}
else if (head(xs) <= head(ys)) {
return pair(head(xs), mergeA(tail(xs),
ys));
}
else {
return pair(head(ys), mergeA(xs,
tail(ys)));
}
}
function merge(xs, ys) {
if (is_null(xs) && is_null(ys)) {
return null;
}
else if (is_null(xs)) {
set_tail(ys, merge(xs, tail(ys)));
return ys;
}
else if (is_null(ys)) {
set_tail(xs, merge(tail(xs), ys));
return xs;
}
else if (head(xs) <= head(ys)) {
set_tail(xs, merge(tail(xs), ys));
return xs;
}
else {
set_tail(ys, merge(xs, tail(ys)));
return ys;
}
}
Rucer
function take(xs, n) {
return n === 0
? null
: pair(head(xs), take(tail(xs), n - 1));
}
function drop(xs, n) {
return is_null(xs) || n === length(xs)
? null
: pair(list_ref(xs, n), drop(xs, n + 1));
}
function solvable(xs, n) {
const end_index = length(xs) - 1;
function helper(xs, focus, moves) {
if (focus === end_index) {
return moves; }
else if
(length(drop(xs,focus))>=list_ref(xs, focus))
{ return helper(xs,list_ref(xs, focus) +
focus,
moves + 1); }
else if
(focus >= list_ref(xs, focus))
{ return helper(xs, focus - list_ref(xs,
focus),
moves + 1); }
else {
return -1;
}}
return n >= helper(xs, 0, 0); }
Circular lists
function make_circular(xs, ys) { // Make xs
=== ys
if (is_null(xs)) {
return ys;
} else {
set_tail(xs, make_circular(tail(xs),
ys));
return xs;
}
}
Download