Skip to content

Commit e75c885

Browse files
author
As'ad Saleh Umar
committed
initial commit
0 parents  commit e75c885

19 files changed

+955
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_store

anagram.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function anagramChecker(inputA: string, inputB: string): boolean {
2+
const sortedA = inputA
3+
.toLowerCase()
4+
.split("")
5+
.filter((e) => e != " ")
6+
.sort()
7+
.join("");
8+
const sortedB = inputB
9+
.toLowerCase()
10+
.split("")
11+
.filter((e) => e != " ")
12+
.sort()
13+
.join("");
14+
return sortedA === sortedB;
15+
}
16+
17+
// What is anagram ?
18+
// Anagram is when 2 words/phrases consist of exactly simmilar characters.
19+
// So we can say: "monkeys write" is an anagram for "New York Times".
20+
21+
// Test case:
22+
console.log(anagramChecker("New York Times", "monkeys write") === true);
23+
console.log(anagramChecker("restful", "fluster") === true);
24+
console.log(anagramChecker("Dormitory", "dirty room") === true);
25+
console.log(
26+
anagramChecker("some random words", "totally not it's anagrams ") === false
27+
);

coding-test/index.html

Whitespace-only changes.

coding-test/iterative.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// A function for reversing word order in a paragraph, but only
2+
// taking words which length are greater than 5.
3+
function doStuff(text) {
4+
if (typeof text !== "string") {
5+
console.error("This function expect a string");
6+
return "";
7+
}
8+
return text
9+
.toLocaleLowerCase()
10+
.split(" ")
11+
.reverse()
12+
.filter((text) => text.length > 5)
13+
.join(", ");
14+
}

deep_equal.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
type MyObject = { [key: string]: any };
2+
3+
function deepEqual(obj1: MyObject, obj2: MyObject) {
4+
if (obj1 === null && obj2 === null) {
5+
}
6+
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
7+
return false;
8+
}
9+
for (const key in obj1) {
10+
if (Array.isArray(obj1[key])) {
11+
deepEqual(obj1[key], obj2[key]);
12+
} else if (typeof obj1[key] === "object") {
13+
deepEqual(obj1[key], obj2[key]);
14+
} else if (obj1[key] !== obj2[key]) {
15+
console.log(
16+
`Not equal!\nThe diff is in key "${key}" inside the object "${JSON.stringify(
17+
obj1,
18+
null,
19+
2
20+
)}"`
21+
);
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
const one = {
29+
name: "asad",
30+
age: 23,
31+
pets: ["cat", "mouse", "bird"],
32+
address: {
33+
street: "siaga raya 42B",
34+
country: "indonesia",
35+
},
36+
foo: null,
37+
};
38+
39+
const two = {
40+
name: "asad",
41+
age: 23,
42+
pets: ["cat", "mouse", "bird"],
43+
address: {
44+
street: "siaga raya 42B",
45+
country: "indonesia",
46+
},
47+
foo: "bar",
48+
};
49+
50+
// TEST
51+
console.log(deepEqual(one, two));
52+
// console.log(deepEqual(["cat", "mouse", "bird"], ["cat", "mouse", "bird"]));

fibonacci.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// What is a fibonacci?
2+
// f(0) = 0
3+
// f(1) = 1
4+
// f(n) = f(n-1) + f(n-2)
5+
6+
// Solution one. Simple.
7+
let i = 0;
8+
9+
function fibonacci(index: number): number {
10+
if (index === 0) {
11+
return 0;
12+
}
13+
if (index === 1) {
14+
return 1;
15+
}
16+
i++;
17+
// console.log(index);
18+
return fibonacci(index - 1) + fibonacci(index - 2);
19+
}
20+
21+
// console.log(fibonacci(9));
22+
// console.log({ i });
23+
24+
// Solution two. Memoized.
25+
type MapNumberNumber = { [a: number]: number };
26+
let memo: MapNumberNumber = {};
27+
let j = 0;
28+
function optimizedFib(index: number): number {
29+
if (index === 0) {
30+
memo[0] = 0;
31+
return 0;
32+
}
33+
if (index === 1) {
34+
memo[1] = 1;
35+
return 1;
36+
}
37+
38+
// console.log(index);
39+
if (memo[index] != null) {
40+
return memo[index];
41+
} else {
42+
j++;
43+
const result = optimizedFib(index - 1) + optimizedFib(index - 2);
44+
memo[index] = result;
45+
return result;
46+
}
47+
}
48+
49+
console.log(optimizedFib(9));
50+
console.log({ j, memo });

find-missing-integer.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function findMissingConsecutiveInteger(array: Array<number>): number {
2+
let isMissing = false;
3+
let i = 1;
4+
let result = 0;
5+
while (!isMissing) {
6+
if (!isIncluded(array, i)) {
7+
isMissing = true;
8+
result = i;
9+
break;
10+
}
11+
i++;
12+
}
13+
return result;
14+
}
15+
16+
function isIncluded<T>(array: Array<T>, target: T): boolean {
17+
let result = false;
18+
for (let i = 0; i < array.length; i++) {
19+
if (array[i] === target) {
20+
result = true;
21+
break;
22+
}
23+
}
24+
return result;
25+
}
26+
27+
console.log(findMissingConsecutiveInteger([4, 3, 2, 1, 8, 9, 10]));
28+
console.log(findMissingConsecutiveInteger([5, 4, 3, 6, 2, 1, 8, 9, 10]));
29+
console.log(findMissingConsecutiveInteger([-3, -2, -1]));

fizbaz.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function fizbaz(n: number = 50) {
2+
Array.from({ length: n }, (v, i) => i + 1)
3+
.map((e) => {
4+
if (e % 3 === 0 && e % 5 === 0) {
5+
return "FizBaz";
6+
}
7+
if (e % 3 === 0) {
8+
return "Fiz";
9+
}
10+
if (e % 5 === 0) {
11+
return "Baz";
12+
}
13+
return e;
14+
})
15+
.forEach((e) => console.log(e));
16+
}
17+
18+
fizbaz();

lake-cheat.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Javascript program to count islands in boolean 2D matrix
2+
3+
// No of rows and columns
4+
let ROW = 5,
5+
COL = 5;
6+
7+
// A function to check if a given cell (row, col) can
8+
// be included in DFS
9+
function isSafe(M, row, col, visited) {
10+
// row number is in range, column number is in range
11+
// and value is 1 and not yet visited
12+
return (
13+
row >= 0 &&
14+
row < ROW &&
15+
col >= 0 &&
16+
col < COL &&
17+
M[row][col] == 1 &&
18+
!visited[row][col]
19+
);
20+
}
21+
22+
// A utility function to do DFS for a 2D boolean matrix.
23+
// It only considers the 8 neighbors as adjacent vertices
24+
function DFS(M, row, col, visited) {
25+
// These arrays are used to get row and column numbers
26+
// of 8 neighbors of a given cell
27+
let rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1];
28+
let colNbr = [-1, 0, 1, -1, 1, -1, 0, 1];
29+
30+
// Mark this cell as visited
31+
visited[row][col] = true;
32+
33+
// Recur for all connected neighbours
34+
for (let k = 0; k < 8; ++k) {
35+
if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited)) {
36+
DFS(M, row + rowNbr[k], col + colNbr[k], visited);
37+
}
38+
}
39+
}
40+
41+
// The main function that returns count of islands in a given
42+
// boolean 2D matrix
43+
function countIslands(M) {
44+
// Make a bool array to mark visited cells.
45+
// Initially all cells are unvisited
46+
let visited = new Array(ROW);
47+
for (let i = 0; i < ROW; i++) {
48+
visited[i] = new Array(COL);
49+
}
50+
for (let i = 0; i < ROW; i++) {
51+
for (let j = 0; j < COL; j++) {
52+
visited[i][j] = false;
53+
}
54+
}
55+
// Initialize count as 0 and traverse through the all cells
56+
// of given matrix
57+
let count = 0;
58+
for (let i = 0; i < ROW; ++i) {
59+
for (let j = 0; j < COL; ++j) {
60+
if (M[i][j] == 1 && !visited[i][j]) {
61+
// value 1 is not
62+
// visited yet, then new island found, Visit all
63+
// cells in this island and increment island count
64+
DFS(M, i, j, visited);
65+
count++;
66+
}
67+
}
68+
}
69+
return count;
70+
}
71+
72+
// Driver method
73+
let M = [
74+
[1, 1, 0, 0, 0],
75+
[0, 1, 0, 0, 1],
76+
[1, 0, 0, 1, 1],
77+
[0, 0, 0, 0, 0],
78+
[1, 0, 1, 0, 1],
79+
];
80+
81+
console.log(countIslands(M));
82+
83+
// This code is contributed by avanitrachhadiya2155

lake.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Ada matriks 2 dimensi n*n yang elemennya terdiri dari . dan +
2+
// Titik, itu kita anggap sebagai danau. + kita anggap sebagai daratan.
3+
// sample input:
4+
var lakes = [
5+
[".", ".", ".", ".", "."],
6+
["+", "+", "+", "+", "+"],
7+
[".", ".", ".", ".", "."],
8+
[".", ".", ".", ".", "."],
9+
["+", "+", "+", "+", "+"],
10+
];
11+
var adjacentElementDict = [
12+
[-1, -1],
13+
[-1, 0],
14+
[-1, 1],
15+
[0, -1],
16+
// [0, 0],
17+
[0, 1],
18+
[1, -1],
19+
[1, 0],
20+
[1, 1],
21+
];
22+
function solve() {
23+
var richLake = lakes.map(function (arrayEl) {
24+
return arrayEl.map(function (el) { return ({ val: el, label: "" }); });
25+
});
26+
function getElement(x, y) {
27+
var res = null;
28+
if (x >= 0 || y >= 0) {
29+
res = richLake[x][y];
30+
}
31+
console.log({ getElement: res });
32+
return res;
33+
}
34+
function getAdjacentElements(x, y) {
35+
var res = adjacentElementDict.map(function (_a) {
36+
var adjX = _a[0], adjY = _a[1];
37+
return getElement(x + adjX, y + adjY);
38+
});
39+
console.log({ getAdjacentElements: res });
40+
return res;
41+
}
42+
function isAdjacentElementAlreadyALake(x, y) {
43+
var adjacentElements = getAdjacentElements(x, y);
44+
var isAlreadyLake = false;
45+
var element = null;
46+
adjacentElements.forEach(function (el) {
47+
if ((el === null || el === void 0 ? void 0 : el.val) === ".") {
48+
isAlreadyLake = true;
49+
element = el;
50+
}
51+
});
52+
return {
53+
isAlreadyLake: isAlreadyLake,
54+
element: element
55+
};
56+
}
57+
var lakeCount = 0;
58+
richLake.forEach(function (arrLake, i) {
59+
arrLake.forEach(function (el, j) {
60+
var currentEl = getElement(i, j);
61+
// If it's a lake
62+
// If sebelahnya udah lake => kasih label yang sama dengan sebelahnya,
63+
// else sebelahnya belum lake => buat label baru.
64+
if ((currentEl === null || currentEl === void 0 ? void 0 : currentEl.val) === ".") {
65+
var element = isAdjacentElementAlreadyALake(i, j).element;
66+
if (element) {
67+
currentEl.label = element.label;
68+
}
69+
else {
70+
// Buat label baru
71+
currentEl.label = "lake-".concat(lakeCount);
72+
lakeCount += 1;
73+
}
74+
}
75+
// If it's not a lake
76+
// do nothing.
77+
});
78+
});
79+
console.log({ richLake: richLake });
80+
// Setelah gue punya rich lake beserta labelnya.
81+
// grouping berdasarkan label:
82+
var obj = richLake
83+
.map(function (_a) {
84+
var x = _a[0];
85+
return x;
86+
})
87+
.reduce(function (acc, cur) {
88+
var label = cur.label;
89+
if (acc[label]) {
90+
acc[label] = acc[label].push(cur);
91+
}
92+
else {
93+
acc[label] = [cur];
94+
}
95+
return acc;
96+
}, {});
97+
console.log({ obj: obj });
98+
var result = Object.keys(obj).length;
99+
console.log({ result: result });
100+
return result;
101+
}

0 commit comments

Comments
 (0)