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
42 changes: 42 additions & 0 deletions src/problem1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Mathematic | Time O(1) - Space O(1)
export function sum_to_n_a(n: number): number {
return (n * (n + 1)) / 2;
}

// Iterative | Time O(n) - Space O(1)
export function sum_to_n_b(n: number): number {
let total = 0;
for (let i = 1; i <= n; i++) {
total += i;
}
return total;
}

// Recursive | Time O(n) - Space O(n)
export function sum_to_n_c(n: number): number {
if (n <= 0) return 0;
return n + sum_to_n_c(n - 1);
}

// Performance test
const testCases = [1_00, 1_000, 10_000];

for (const n of testCases) {
console.log(`\n--- n = ${n.toLocaleString()} ---`);

let t = performance.now();
console.log(
`sum_to_n_a (Math): ${sum_to_n_a(n)} | ${(performance.now() - t).toFixed(4)}ms`,
);

t = performance.now();
console.log(
`sum_to_n_b (Iterative): ${sum_to_n_b(n)} | ${(performance.now() - t).toFixed(4)}ms`,
);

// Recursive blows the stack for large n, cap it
t = performance.now();
console.log(
`sum_to_n_c (Recursive): ${sum_to_n_c(n)} | ${(performance.now() - t).toFixed(4)}ms`,
);
}
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?
75 changes: 75 additions & 0 deletions src/problem2/fancy-form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 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 [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## React Compiler

The React Compiler is enabled on this template. See [this documentation](https://react.dev/learn/react-compiler) for more information.

Note: This will impact Vite dev & build performances.

## 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...
},
},
])
```
1,305 changes: 1,305 additions & 0 deletions src/problem2/fancy-form/bun.lock

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/problem2/fancy-form/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"iconLibrary": "lucide",
"rtl": false,
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"registries": {}
}
23 changes: 23 additions & 0 deletions src/problem2/fancy-form/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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: {
ecmaVersion: 2020,
globals: globals.browser,
},
},
])
13 changes: 13 additions & 0 deletions src/problem2/fancy-form/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!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" />
<title>fancy-form</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions src/problem2/fancy-form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "fancy-form",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@tailwindcss/vite": "^4.2.1",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"lucide-react": "^0.576.0",
"radix-ui": "^1.4.3",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"sonner": "^2.0.7",
"tailwind-merge": "^3.5.0",
"tailwindcss": "^4.2.1"
},
"devDependencies": {
"@eslint/js": "^9.39.1",
"@types/node": "^25.3.3",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^5.1.1",
"babel-plugin-react-compiler": "^1.0.0",
"eslint": "^9.39.1",
"eslint-plugin-react-hooks": "^7.0.1",
"eslint-plugin-react-refresh": "^0.4.24",
"globals": "^16.5.0",
"shadcn": "^3.8.5",
"tw-animate-css": "^1.4.0",
"typescript": "~5.9.3",
"typescript-eslint": "^8.48.0",
"vite": "^7.3.1"
}
}
1 change: 1 addition & 0 deletions src/problem2/fancy-form/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
14 changes: 14 additions & 0 deletions src/problem2/fancy-form/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Toaster } from 'sonner'
import { SwapCard } from './components/SwapCard'
import './App.css'

function App() {
return (
<div className="min-h-screen bg-gradient-to-br from-indigo-50 via-violet-50 to-purple-100 flex items-center justify-center p-4">
<SwapCard />
<Toaster position="top-center" richColors />
</div>
)
}

export default App
1 change: 1 addition & 0 deletions src/problem2/fancy-form/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/problem2/fancy-form/src/components/RateDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Token } from '@/types/swap'

interface RateDisplayProps {
fromToken: Token | null
toToken: Token | null
exchangeRate: number | null
}

export function RateDisplay({ fromToken, toToken, exchangeRate }: RateDisplayProps) {
if (!fromToken || !toToken || exchangeRate === null) return null

return (
<div className="flex items-center justify-center text-sm text-muted-foreground py-1">
<span>
1 {fromToken.symbol} ={' '}
<span className="text-foreground font-medium">
{String(parseFloat(exchangeRate.toFixed(6)))} {toToken.symbol}
</span>
</span>
</div>
)
}
26 changes: 26 additions & 0 deletions src/problem2/fancy-form/src/components/SubmitButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Loader2 } from 'lucide-react'
import { Button } from '@/components/ui/button'

interface SubmitButtonProps {
loading: boolean
disabled?: boolean
}

export function SubmitButton({ loading, disabled }: SubmitButtonProps) {
return (
<Button
type="submit"
disabled={loading || disabled}
className="w-full h-12 text-base font-semibold"
>
{loading ? (
<>
<Loader2 className="mr-2 h-5 w-5 animate-spin" />
Confirming...
</>
) : (
'Confirm Swap'
)}
</Button>
)
}
29 changes: 29 additions & 0 deletions src/problem2/fancy-form/src/components/SwapButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState } from 'react'
import { ArrowUpDown } from 'lucide-react'

interface SwapButtonProps {
onClick: () => void
}

export function SwapButton({ onClick }: SwapButtonProps) {
const [rotating, setRotating] = useState(false)

function handleClick() {
setRotating(true)
onClick()
setTimeout(() => setRotating(false), 300)
}

return (
<div className="flex justify-center -my-2 relative z-10">
<button
onClick={handleClick}
className="h-10 w-10 rounded-full bg-card border-2 border-border flex items-center justify-center hover:bg-muted transition-colors shadow-md"
>
<ArrowUpDown
className={`h-4 w-4 text-muted-foreground transition-transform duration-300 ${rotating ? 'rotate-180' : ''}`}
/>
</button>
</div>
)
}
Loading