Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Dependencies
node_modules/
**/node_modules/

# Build outputs
dist/
**/dist/
build/
**/build/

# Test coverage
coverage/
**/coverage/
.nyc_output/

# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# IDE files
.vscode/
.idea/
*.swp
*.swo
*~
.project
.classpath
.settings/

# Environment variables
.env
.env.local
.env.*.local

6 changes: 6 additions & 0 deletions src/problem1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
coverage/
*.log
.DS_Store

31 changes: 31 additions & 0 deletions src/problem1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
***Performance Comparision***
| Approach | Time Complexity | Space Complexity | Notes |
| ---------------- | --------------- | ---------------- | ---------------------- |
| **Math formula** | O(1) | O(1) | Fastest possible |
| **For loop** | O(n) | O(1) | Fast, predictable |
| **Array.reduce** | O(n) | O(n) | N/A |

***Pros & Cons***
**Math Formular**
*Pros*
- Fastest
- Lowest complextiy
- Shortest
*Cons*
- Require knowing the formula

**For loop**
*Pros*
- Fast
- Low complexity
- Easy to debug
*Cons*
- Slightly more verbose than formula

**Array.reduce**
*Pros*
- Functional and expressive
- Declaritive style
- Easy to chain with other array operations
*Cons*
- Worst memory usage among all options
31 changes: 31 additions & 0 deletions src/problem1/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { sum_to_n_a, sum_to_n_b, sum_to_n_c } from './index';

describe('Approach A', () => {
test('sum_to_n_a should return the correct sum', () => {
expect(sum_to_n_a(0)).toBe(0);
expect(sum_to_n_a(1)).toBe(1);
expect(sum_to_n_a(2)).toBe(3);
expect(sum_to_n_a(3)).toBe(6);
expect(sum_to_n_a(4)).toBe(10);
});
});

describe('Approach B', () => {
test('sum_to_n_b should return the correct sum', () => {
expect(sum_to_n_b(0)).toBe(0);
expect(sum_to_n_b(1)).toBe(1);
expect(sum_to_n_b(2)).toBe(3);
expect(sum_to_n_b(3)).toBe(6);
expect(sum_to_n_b(4)).toBe(10);
});
});

describe('Approach C', () => {
test('sum_to_n_c should return the correct sum', () => {
expect(sum_to_n_c(0)).toBe(0);
expect(sum_to_n_c(1)).toBe(1);
expect(sum_to_n_c(2)).toBe(3);
expect(sum_to_n_c(3)).toBe(6);
expect(sum_to_n_c(4)).toBe(10);
});
});
23 changes: 23 additions & 0 deletions src/problem1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type SumToN = (n: number) => number;

export const sum_to_n_a: SumToN = (n) => {
if (n <= 1) return n;
if (Number.isNaN(n)) throw new Error('Invalid input');
return (n * (n + 1)) / 2;
}

export const sum_to_n_b: SumToN = (n) => {
if (n <= 1) return n;
if (Number.isNaN(n)) throw new Error('Invalid input');
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

export const sum_to_n_c: SumToN = (n) => {
if (n <= 1) return n;
if (Number.isNaN(n)) throw new Error('Invalid input');
return Array.from({ length: n }, (_, i) => i + 1).reduce((acc, curr) => acc + curr, 0);
}
18 changes: 18 additions & 0 deletions src/problem1/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>'],
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
collectCoverageFrom: [
'*.ts',
'!*.test.ts',
'!*.spec.ts',
'!**/__tests__/**',
],
moduleFileExtensions: ['ts', 'js', 'json'],
verbose: true,
};

Loading