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
110 changes: 110 additions & 0 deletions src/problem1/sum-to-n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Problem 1: Three Ways to Sum to N
*
* Input: n — a positive integer
* Output: 1 + 2 + 3 + ... + n
*
* Assumption: input always produces result < Number.MAX_SAFE_INTEGER
*/

// =============================================================================
// Implementation A: Gauss's Formula (Mathematical)
// =============================================================================
/**
* Uses the closed-form arithmetic series formula: n * (n + 1) / 2
*
* Explanation:
* Pair the sequence from both ends: (1+n) + (2+n-1) + ...
* There are n/2 such pairs, each summing to (n+1), giving n*(n+1)/2.
* No loops needed — a single arithmetic expression does the job.
*
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
var sum_to_n_a = function (n) {
return (n * (n + 1)) / 2;
};

// =============================================================================
// Implementation B: Iterative Loop
// =============================================================================
/**
* Accumulates the sum by looping from 1 up to n.
*
* Explanation:
* The most straightforward approach — iterate and add each integer.
* Easy to read, trace, and debug; no recursion overhead.
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
var sum_to_n_b = function (n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
};

// =============================================================================
// Implementation C: Recursive
// =============================================================================
/**
* Recursively defines the sum via: sum(n) = n + sum(n - 1), base: sum(0) = 0
*
* Explanation:
* Naturally mirrors the mathematical definition of the summation.
* Each call reduces n by 1 until the base case is reached.
* Note: not ideal for very large n due to call stack limits.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
var sum_to_n_c = function (n) {
if (n === 0) return 0;
return n + sum_to_n_c(n - 1);
};

// =============================================================================
// Test Suite
// =============================================================================

const implementations = [
{ name: "sum_to_n_a (Gauss Formula) ", fn: sum_to_n_a },
{ name: "sum_to_n_b (Iterative Loop)", fn: sum_to_n_b },
{ name: "sum_to_n_c (Recursive) ", fn: sum_to_n_c },
];

const testCases = [
{ input: 1, expected: 1, label: "n=1 → 1" },
{ input: 5, expected: 15, label: "n=5 → 1+2+3+4+5" },
{ input: 10, expected: 55, label: "n=10 → 1+...+10" },
{ input: 100, expected: 5050, label: "n=100 → Gauss classic" },
{ input: 0, expected: 0, label: "n=0 → empty sum" },
];

let allPassed = true;

console.log("=".repeat(58));
console.log(" Running Tests for sum_to_n implementations");
console.log("=".repeat(58));

for (const { name, fn } of implementations) {
console.log(`\n>> ${name}`);
console.log("-".repeat(58));

for (const { input, expected, label } of testCases) {
const result = fn(input);
const passed = result === expected;
if (!passed) allPassed = false;

const status = passed ? "PASS" : "FAIL";
console.log(
` [${status}] ${label.padEnd(28)} got: ${result}, expected: ${expected}`
);
}
}

console.log("\n" + "=".repeat(58));
console.log(allPassed ? " ALL TESTS PASSED" : " SOME TESTS FAILED");
console.log("=".repeat(58));
24 changes: 24 additions & 0 deletions src/problem2/fancy-form/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
73 changes: 73 additions & 0 deletions src/problem2/fancy-form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Oxc](https://oxc.rs)
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)

## React Compiler

The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:

```js
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...

// Remove tseslint.configs.recommended and replace with this
tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
tseslint.configs.stylisticTypeChecked,

// Other configs...
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```

You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:

```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Enable lint rules for React
reactX.configs['recommended-typescript'],
// Enable lint rules for React DOM
reactDom.configs.recommended,
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```
22 changes: 22 additions & 0 deletions src/problem2/fancy-form/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import { defineConfig, globalIgnores } from 'eslint/config'

export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
reactHooks.configs.flat.recommended,
reactRefresh.configs.vite,
],
languageOptions: {
globals: globals.browser,
},
},
])
15 changes: 15 additions & 0 deletions src/problem2/fancy-form/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="NinetyNine DEX — Swap any token instantly at the best on-chain rates. Non-custodial, trustless, lightning-fast." />
<meta name="theme-color" content="#0b0d14" />
<title>NinetyNine DEX — Token Swap</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading