Skip to content

Commit 6722060

Browse files
committed
[main] Funcs
1 parent f58484d commit 6722060

File tree

11 files changed

+268
-0
lines changed

11 files changed

+268
-0
lines changed

jsProblems/bumps/bumps.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Your car is old, it breaks easily. The shock absorbers are gone and you think it can handle about 15 more bumps before it dies totally.
3+
* Unfortunately for you, your drive is very bumpy! Given a string showing either flat road (_) or bumps (n). If you are able to reach home safely by encountering 15 bumps or less, return Woohoo!, otherwise return Car Dead
4+
*
5+
* @param {string} x - the road
6+
* @returns Whether your car makes it or not
7+
*/
8+
function bump(x) {
9+
return x.split().filter(e => e === "n").length > 15 ? "Car Dead" : "Woohoo!";
10+
}

jsProblems/highestRank/highestRank.js

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Array.prototype.sameStructureAs = function (other) {
2+
/**
3+
*
4+
* @param {any[]} arr
5+
*/
6+
const mapStructure = (
7+
arr,
8+
mappedDepth = { 0: { 0: 0 } },
9+
elemNumber = 0,
10+
currentLevel = 0
11+
) => {
12+
for (let i = 0; i < arr.length; i++) {
13+
const eachelement = arr[i];
14+
if (eachelement.constructor.name === "Array") {
15+
mapStructure(eachelement, mappedDepth, i, currentLevel + 1);
16+
} else if (elemNumber in mappedDepth) {
17+
if (currentLevel in mappedDepth[elemNumber]) {
18+
mappedDepth[elemNumber][currentLevel] += 1;
19+
} else {
20+
mappedDepth[elemNumber][currentLevel] = 1;
21+
}
22+
} else {
23+
mappedDepth[elemNumber] = {};
24+
mappedDepth[elemNumber][currentLevel] = 1;
25+
}
26+
}
27+
return mappedDepth;
28+
};
29+
if (
30+
typeof other !== "object" ||
31+
typeof this !== "object" ||
32+
other.constructor.name !== "Array" ||
33+
this.constructor.name !== "Array"
34+
) {
35+
return false;
36+
}
37+
const mapStructureOther = mapStructure(other);
38+
const mapStructureThis = mapStructure(this);
39+
return JSON.stringify(mapStructureOther) === JSON.stringify(mapStructureThis);
40+
};
41+
42+
console.log([1, 1, 1].sameStructureAs([2, 2, 2]), "[1,1,1] same as [2,2,2]");
43+
44+
console.log(
45+
[1, [1, 1]].sameStructureAs([2, [2, 2]]) === true
46+
// "[1,[1,1]] same as [2,[2,2]]"
47+
);
48+
console.log(
49+
[(1, [1, 1])].sameStructureAs([[2, 2], 2]) === false
50+
// "[1,[1,1]] not same as [[2,2],2]"
51+
);
52+
console.log(
53+
[1, [1, 1]].sameStructureAs([2, [2]]) === false
54+
// "[1,[1,1]] not same as [2,[2]]"
55+
);
56+
57+
console.log(
58+
[[[], []]].sameStructureAs([[[], []]]) === true
59+
// "[[[],[]]] same as [[[],[]]]"
60+
);
61+
console.log(
62+
[[[], []]].sameStructureAs([[1, 1]]) === false
63+
// "[[[],[]]] not same as [[1,1]]"
64+
);
65+
66+
console.log([1, [[[1]]]].sameStructureAs([2, [[[2]]]]) === true);
67+
// , "[1,[[[1]]]] same as [2,[[[2]]]]");
68+
69+
console.log([].sameStructureAs(1) === false);
70+
// , "[] not same as 1");
71+
console.log([].sameStructureAs({}) === false);
72+
// , "[] not same as {}");
73+
74+
console.log(
75+
[1, "[", "]"].sameStructureAs(["[", "]", 1]) === true
76+
// "[1,'[',']'] same as ['[',']',1]"
77+
);
78+
79+
console.log([1, 2].sameStructureAs([[3], 3]) === false);
80+
// , "[1,2] not same as [[3],3]");

tsProblems/animal/animal.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export abstract class Animal {
2+
/** @param {number} value The length of the animal in parrots. */
3+
protected constructor(public value: number) {}
4+
5+
convertTo(someone: Animal): number {
6+
if (someone instanceof Parrot) {
7+
return 38;
8+
} else if (someone instanceof Monkey) {
9+
return 5;
10+
} else {
11+
this.value = 0;
12+
return 0;
13+
}
14+
}
15+
}
16+
17+
export class Boa extends Animal {
18+
constructor() {
19+
super(1);
20+
}
21+
convertTo(someone: Animal): number {
22+
if (someone instanceof Parrot) {
23+
return 38;
24+
} else if (someone instanceof Monkey) {
25+
return 5;
26+
}
27+
return 0;
28+
}
29+
}
30+
31+
export class Parrot extends Animal {
32+
constructor() {
33+
super(1);
34+
}
35+
convertTo(someone: Animal): number {
36+
if (someone instanceof Boa) {
37+
return 1;
38+
}
39+
if (someone instanceof Monkey) {
40+
return 1;
41+
}
42+
return 0;
43+
}
44+
}
45+
46+
export class Monkey extends Animal {
47+
constructor() {
48+
super(1);
49+
}
50+
convertTo(someone: Animal): number {
51+
if (someone instanceof Parrot) {
52+
return 1;
53+
} else if (someone instanceof Boa) {
54+
return 1;
55+
}
56+
return 0;
57+
}
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.countDigit = void 0;
4+
function countDigit(number, digit, base, fromBase) {
5+
var convertedNumber = Number.parseInt(number, fromBase).toString(base);
6+
return convertedNumber.split("").filter(function (e) { return e === digit; }).length;
7+
}
8+
exports.countDigit = countDigit;
9+
console.log(countDigit("10", "a", 11, 10));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function countDigit(
2+
number: string,
3+
digit: string,
4+
base: number,
5+
fromBase: number
6+
): number {
7+
const convertedNumber = Number.parseInt(number, fromBase).toString(base);
8+
return convertedNumber.split("").filter((e) => e === digit).length;
9+
}
10+
11+
console.log(countDigit("10", "a", 11, 10));

tsProblems/dollars/dollars.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.dollars = void 0;
4+
var dollars = function (amounts) {
5+
console.log(typeof amounts, amounts.constructor, JSON.stringify(amounts));
6+
var colorSet = new Set(amounts);
7+
return (colorSet.size === 1 &&
8+
(colorSet.has("red") || colorSet.has("blue") || colorSet.has("green")));
9+
};
10+
exports.dollars = dollars;
11+
console.log((0, exports.dollars)(["red", "red", "purple"]));

tsProblems/dollars/dollars.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const dollars = (amounts: string[]): boolean => {
2+
console.log(typeof amounts, amounts.constructor, JSON.stringify(amounts));
3+
4+
const colorSet = new Set(amounts);
5+
return (
6+
colorSet.size === 1 &&
7+
(colorSet.has("red") || colorSet.has("blue") || colorSet.has("green"))
8+
);
9+
};
10+
11+
console.log(dollars(["red", "red", "purple"]));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
var __read = (this && this.__read) || function (o, n) {
3+
var m = typeof Symbol === "function" && o[Symbol.iterator];
4+
if (!m) return o;
5+
var i = m.call(o), r, ar = [], e;
6+
try {
7+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8+
}
9+
catch (error) { e = { error: error }; }
10+
finally {
11+
try {
12+
if (r && !r.done && (m = i["return"])) m.call(i);
13+
}
14+
finally { if (e) throw e.error; }
15+
}
16+
return ar;
17+
};
18+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20+
if (ar || !(i in from)) {
21+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22+
ar[i] = from[i];
23+
}
24+
}
25+
return to.concat(ar || Array.prototype.slice.call(from));
26+
};
27+
exports.__esModule = true;
28+
exports.duplicated = exports.isDuplicate = void 0;
29+
function isDuplicate(arr, keyValue) {
30+
return arr.filter(function (e) { return e === keyValue; }).length > 0;
31+
}
32+
exports.isDuplicate = isDuplicate;
33+
function duplicated(arr, keys) {
34+
var dupes = new Set();
35+
var formattedArr = arr.map(function (eachObj) {
36+
return keys.map(function (eachKey) { return eachObj[eachKey]; });
37+
});
38+
console.log(formattedArr);
39+
formattedArr.forEach(function (eachObj, ind) {
40+
if (!dupes.has(arr[ind]) && isDuplicate(arr, eachObj)) {
41+
dupes.add(arr[ind]);
42+
}
43+
});
44+
return __spreadArray([], __read(dupes), false);
45+
}
46+
exports.duplicated = duplicated;
47+
var objs = [
48+
{ x: 1, y: 1 },
49+
{ x: 2, y: 2 },
50+
{ x: 1, z: 1 },
51+
{ x: 1, y: 1, z: 3 },
52+
];
53+
var keys = ["x", "y"];
54+
console.log(duplicated(objs.map(function (x) { return Object.assign({}, x); }), keys.slice()));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function isDuplicate(arr: any[], keyValue: any[]): boolean {
2+
return arr.filter((e) => e === keyValue).length > 0;
3+
}
4+
5+
export function duplicated(arr: any[], keys: string[]): any[] {
6+
const dupeResults = {};
7+
arr.forEach((eachObj) => {});
8+
}
9+
10+
const objs = [
11+
{ x: 1, y: 1 },
12+
{ x: 2, y: 2 },
13+
{ x: 1, z: 1 },
14+
{ x: 1, y: 1, z: 3 },
15+
];
16+
const keys = ["x", "y"];
17+
18+
console.log(
19+
duplicated(
20+
objs.map((x) => Object.assign({}, x)),
21+
keys.slice()
22+
)
23+
);

0 commit comments

Comments
 (0)