Skip to content

Commit c4d5746

Browse files
authored
Merge pull request #257 from loiane/loiane/p2-ts-migrations
feat: P2 TypeScript migrations — set, recursion, bigO, heap
2 parents cebf338 + 58982a9 commit c4d5746

35 files changed

Lines changed: 2290 additions & 0 deletions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
export {};
2+
3+
// O(1) - Constant Time
4+
function secondsInDays(numberOfDays: number): number {
5+
if (numberOfDays <= 0 || !Number.isInteger(numberOfDays)) {
6+
throw new Error('Invalid number of days');
7+
}
8+
return 60 * 60 * 24 * numberOfDays;
9+
}
10+
11+
console.log('O(1) - Constant Time');
12+
console.log('Seconds in 1 day: ', secondsInDays(1)); // 86400
13+
console.log('Seconds in 10 days: ', secondsInDays(10)); // 864000
14+
console.log('Seconds in 100 days: ', secondsInDays(100)); // 8640000
15+
16+
// O(n) - Linear Time
17+
function calculateTotalExpenses(monthlyExpenses: number[]): number {
18+
let total = 0;
19+
for (let i = 0; i < monthlyExpenses.length; i++) {
20+
total += monthlyExpenses[i]!;
21+
}
22+
return total;
23+
}
24+
25+
console.log('*******************');
26+
console.log('O(n) - Linear Time');
27+
console.log('January: ', calculateTotalExpenses([100, 200, 300])); // 600
28+
console.log('February: ', calculateTotalExpenses([200, 300, 400])); // 900
29+
console.log('March: ', calculateTotalExpenses([30, 40, 50, 100, 50])); // 270
30+
31+
// O(n^2) - Quadratic Time
32+
function calculateExpensesMatrix(monthlyExpenses: number[][]): number {
33+
let total = 0;
34+
for (let i = 0; i < monthlyExpenses.length; i++) {
35+
for (let j = 0; j < monthlyExpenses[i]!.length; j++) {
36+
total += monthlyExpenses[i]![j]!;
37+
}
38+
}
39+
return total;
40+
}
41+
42+
console.log('************************');
43+
console.log('O(n^2) - Quadratic Time');
44+
const monthlyExpenses = [
45+
[100, 105, 100, 115, 120, 135],
46+
[180, 185, 185, 185, 200, 210],
47+
[30, 30, 30, 30, 30, 30],
48+
[2000, 2000, 2000, 2000, 2000, 2000],
49+
[600, 620, 610, 600, 620, 600],
50+
[150, 100, 130, 200, 150, 100]
51+
];
52+
console.log('Total expenses: ', calculateExpensesMatrix(monthlyExpenses)); // 18480
53+
54+
// calculating the time complexity of the function calculateExpensesMatrix
55+
function multiplicationTable(num: number, x: number): void {
56+
let s = '';
57+
const numberOfAsterisks = num * x;
58+
for (let i = 1; i <= numberOfAsterisks; i++) {
59+
s += '*';
60+
}
61+
console.log(s);
62+
63+
for (let i = 1; i <= num; i++) {
64+
console.log(`Multiplication table for ${i} with x = ${x}`);
65+
for (let j = 1; j <= x; j++) {
66+
console.log(`${i} * ${j} = `, i * j);
67+
}
68+
}
69+
}
70+
71+
// to see the output of this file use the command: node src/02-bigOnotation/01-big-o-intro.ts
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export {};
2+
3+
/* What is the time and space complexities for each of the following functions.
4+
Try them with different inputs to explore how they behave with different inputs */
5+
6+
// time complexity: O(1) - Constant Time
7+
// space complexity: O(1) - Constant Space
8+
const oddOrEven = (array: unknown[]): string => array.length % 2 === 0 ? 'even' : 'odd';
9+
10+
console.log(oddOrEven([1, 2, 3, 4, 5])); // odd
11+
console.log(oddOrEven([1, 2, 3, 4, 5, 6])); // even
12+
13+
// time complexity: O(n) - Linear Time
14+
// space complexity: O(1) - Constant Space
15+
function calculateAverage(array: number[]): number {
16+
let sum = 0;
17+
for (let i = 0; i < array.length; i++) {
18+
sum += array[i]!;
19+
}
20+
return sum / array.length;
21+
}
22+
23+
console.log(calculateAverage([1, 2, 3, 4, 5])); // 3
24+
console.log(calculateAverage([1, 2, 3, 4, 5, 6])); // 3.5
25+
26+
// time complexity: O(n^2) - Quadratic Time
27+
// space complexity: O(1) - Constant Space
28+
function hasCommonElements(array1: number[], array2: number[]): boolean {
29+
for (let i = 0; i < array1.length; i++) {
30+
for (let j = 0; j < array2.length; j++) {
31+
if (array1[i] === array2[j]) {
32+
return true;
33+
}
34+
}
35+
}
36+
return false;
37+
}
38+
39+
console.log(hasCommonElements([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])); // false
40+
console.log(hasCommonElements([1, 2, 3, 4, 5], [5, 6, 7, 8, 9])); // true
41+
42+
// time complexity: O(n) - Linear Time
43+
// space complexity: O(n) - Linear Space
44+
function getOddNumbers(array: number[]): number[] {
45+
const result: number[] = [];
46+
for (let i = 0; i < array.length; i++) {
47+
if (array[i]! % 2 !== 0) {
48+
result.push(array[i]!);
49+
}
50+
}
51+
return result;
52+
}
53+
54+
console.log(getOddNumbers([1, 2, 3, 4, 5])); // [1, 3, 5]
55+
console.log(getOddNumbers([1, 2, 3, 4, 5, 6])); // [1, 3, 5]
56+
57+
// to see the output of this file use the command: node src/02-bigOnotation/03-exercises.ts

src/07-set/set.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class MySet {
2+
#items: Record<string, boolean> = {};
3+
#size = 0;
4+
5+
add(value: string): boolean {
6+
if (!this.has(value)) {
7+
this.#items[value] = true;
8+
this.#size++;
9+
return true;
10+
}
11+
return false;
12+
}
13+
14+
addAll(values: string[]): void {
15+
values.forEach(value => this.add(value));
16+
}
17+
18+
delete(value: string): boolean {
19+
if (this.has(value)) {
20+
delete this.#items[value];
21+
this.#size--;
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
has(value: string): boolean {
28+
return Object.prototype.hasOwnProperty.call(this.#items, value);
29+
}
30+
31+
values(): string[] {
32+
return Object.keys(this.#items);
33+
}
34+
35+
get size(): number {
36+
return this.#size;
37+
}
38+
39+
getSizeWithoutSizeProperty(): number {
40+
let count = 0;
41+
for (const key in this.#items) {
42+
if (Object.prototype.hasOwnProperty.call(this.#items, key)) {
43+
count++;
44+
}
45+
}
46+
return count;
47+
}
48+
49+
isEmpty(): boolean {
50+
return this.#size === 0;
51+
}
52+
53+
clear(): void {
54+
this.#items = {};
55+
this.#size = 0;
56+
}
57+
58+
union(otherSet: MySet): MySet {
59+
const unionSet = new MySet();
60+
this.values().forEach(value => unionSet.add(value));
61+
otherSet.values().forEach(value => unionSet.add(value));
62+
return unionSet;
63+
}
64+
65+
intersection(otherSet: MySet): MySet {
66+
const intersectionSet = new MySet();
67+
const [smallerSet, largerSet] = this.size <= otherSet.size ? [this, otherSet] : [otherSet, this];
68+
smallerSet.values().forEach(value => {
69+
if (largerSet.has(value)) {
70+
intersectionSet.add(value);
71+
}
72+
});
73+
return intersectionSet;
74+
}
75+
76+
difference(otherSet: MySet): MySet {
77+
const differenceSet = new MySet();
78+
this.values().forEach(value => {
79+
if (!otherSet.has(value)) {
80+
differenceSet.add(value);
81+
}
82+
});
83+
return differenceSet;
84+
}
85+
86+
isSubsetOf(otherSet: MySet): boolean {
87+
if (this.size > otherSet.size) {
88+
return false;
89+
}
90+
return this.values().every(value => otherSet.has(value));
91+
}
92+
93+
isSupersetOf(otherSet: MySet): boolean {
94+
if (this.size < otherSet.size) {
95+
return false;
96+
}
97+
return otherSet.values().every(value => this.has(value));
98+
}
99+
100+
toString(): string {
101+
return this.values().join(', ');
102+
}
103+
}
104+
105+
export default MySet;

src/09-recursion/02-factorial.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export {};
2+
3+
// iterative approach
4+
function factorialIterative(number: number): number | undefined {
5+
if (number < 0) {
6+
return undefined;
7+
}
8+
let total = 1;
9+
for (let n = number; n > 1; n--) {
10+
total *= n;
11+
}
12+
return total;
13+
}
14+
15+
console.log('5! =', factorialIterative(5)); // 5! = 120
16+
17+
// recursive approach
18+
function factorial(number: number): number | undefined {
19+
if (number < 0) { return undefined; }
20+
if (number === 1 || number === 0) { // base case
21+
return 1;
22+
}
23+
return number * (factorial(number - 1) ?? 1);
24+
}
25+
26+
console.log('Recursive 5! =', factorial(5)); // Recursive 5! = 120
27+
28+
// to see the output of this file use the command: node src/09-recursion/02-factorial.ts

src/09-recursion/04-fibonacci.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export {};
2+
3+
// iterative approach
4+
function fibonacciIterative(n: number): number {
5+
if (n < 0) {
6+
throw new Error('Input must be a non-negative integer');
7+
}
8+
if (n < 2) { return n; }
9+
10+
let prevPrev = 0;
11+
let prev = 1;
12+
let current = 0;
13+
14+
for (let i = 2; i <= n; i++) { // n >= 2
15+
current = prev + prevPrev; // f(n-1) + f(n-2)
16+
prevPrev = prev;
17+
prev = current;
18+
}
19+
20+
return current;
21+
}
22+
23+
console.log('fibonacciIterative(2)', fibonacciIterative(2)); // 1
24+
console.log('fibonacciIterative(3)', fibonacciIterative(3)); // 2
25+
console.log('fibonacciIterative(4)', fibonacciIterative(4)); // 3
26+
console.log('fibonacciIterative(5)', fibonacciIterative(5)); // 5
27+
28+
// recursive approach
29+
function fibonacci(n: number): number {
30+
if (n < 0) {
31+
throw new Error('Input must be a non-negative integer');
32+
}
33+
if (n < 2) { return n; } // base case
34+
return fibonacci(n - 1) + fibonacci(n - 2); // recursive case
35+
}
36+
37+
console.log('fibonacci(5)', fibonacci(5)); // 5
38+
39+
// memoization approach
40+
function fibonacciMemoization(n: number): number {
41+
if (n < 0) {
42+
throw new Error('Input must be a non-negative integer');
43+
}
44+
const memo: number[] = [0, 1];
45+
const fibonacci = (n: number): number => {
46+
if (memo[n] != null) return memo[n]!;
47+
return (memo[n] = fibonacci(n - 1) + fibonacci(n - 2));
48+
};
49+
return fibonacci(n);
50+
}
51+
52+
console.log('fibonacciMemoization(5)', fibonacciMemoization(5)); // 5
53+
54+
// to see the output of this file use the command: node src/09-recursion/04-fibonacci.ts

src/10-tree/01-bst.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// src/10-tree/01-bst.ts
2+
import BinarySearchTree from './binary-search-tree';
3+
4+
class Student {
5+
idNumber: number;
6+
name: string;
7+
gradeLevel: number;
8+
9+
constructor(idNumber: number, name: string, gradeLevel: number, address?: string) {
10+
this.idNumber = idNumber;
11+
this.name = name;
12+
this.gradeLevel = gradeLevel;
13+
}
14+
}
15+
16+
// create student comparator to compare idNumber
17+
const studentComparator = (a: Student, b: Student) => a.idNumber - b.idNumber;
18+
19+
const studentTree = new BinarySearchTree(studentComparator);
20+
21+
studentTree.insert(new Student(11, 'Darcy', 10));
22+
studentTree.insert(new Student(7, 'Tory', 10));
23+
studentTree.insert(new Student(5, 'Caleb', 10));
24+
studentTree.insert(new Student(9, 'Sofia', 10));
25+
studentTree.insert(new Student(15, 'Max', 10));
26+
27+
// 11
28+
// / \
29+
// 7 15
30+
// / \
31+
// 5 9
32+
33+
studentTree.insert(new Student(12, 'Seth', 10));
34+
35+
// 11
36+
// / \
37+
// 7 15
38+
// / \ /
39+
// 5 9 12
40+
41+
42+
// to see the output of this file use the command: node src/10-tree/01-bst.js
43+
44+
export {};

0 commit comments

Comments
 (0)