Skip to content

Commit d259135

Browse files
authored
Merge pull request #4 from behzadam/pair-sum
Pair sum
2 parents 47bab18 + 52ac8db commit d259135

File tree

3 files changed

+194
-58
lines changed

3 files changed

+194
-58
lines changed

README.md

Lines changed: 190 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,228 @@
1-
# TypeScript Algorithms and Data Structures
1+
# 🚀 TypeScript Algorithms and Data Structures
22

3-
This repository is a collection of TypeScript algorithms and data structures. It is intended for educational purposes and serves as a reference for developers who want to learn and practice TypeScript.
3+
**A comprehensive collection of algorithms, data structures, and coding challenges implemented in TypeScript**
44

5-
![GitHub top language](https://img.shields.io/github/languages/top/behzadam/algorithms)
6-
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b6d0142c6cb448e28ea0dcc88d77b062)](https://app.codacy.com/gh/behzadam/algorithms/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
7-
![Lines of code](https://img.shields.io/tokei/lines/github/behzadam/algorithms)
5+
[![GitHub top language](https://img.shields.io/github/languages/top/behzadam/algorithms?style=for-the-badge)](https://github.com/behzadam/algorithms)
6+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b6d0142c6cb448e28ea0dcc88b77b062?style=for-the-badge)](https://app.codacy.com/gh/behzadam/algorithms/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
7+
[![Lines of code](https://img.shields.io/tokei/lines/github/behzadam/algorithms?style=for-the-badge)](https://github.com/behzadam/algorithms)
8+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://opensource.org/licenses/MIT)
89

9-
## Introduction
10+
[Features](#-features)[Quick Start](#-getting-started)[Documentation](#-documentation)
1011

11-
It includes code and patterns from the "JavaScript Algorithms" repository, translated from JavaScript to TypeScript. [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms)
12+
---
1213

13-
## Data structure
14+
## 📋 Table of Contents
1415

15-
- [HashTable](src/data-structure/hash-table/hash-table.ts)
16-
- [MinHeap](src/data-structure/heap/min-heap.ts)
17-
- [MaxHeap](src/data-structure/heap/max-heap.ts)
18-
- [LinkedList](src/data-structure/linked-list/linked-list.ts)
19-
- [Queue](src/data-structure/queue/queue.ts)
20-
- [Stack](src/data-structure/stack/stack.ts)
21-
- [MaxPriorityQueue](src/data-structure/priority-queue/max-priority-queue.ts)
22-
- [MinPriorityQueue](src/data-structure/priority-queue/min-priority-queue.ts)
23-
- [Trie](src/data-structure/trie/trie.ts)
16+
<!-- markdownlint-disable MD033 -->
17+
<details>
18+
<summary>Click to expand</summary>
19+
<!-- markdownlint-enable MD033 -->
2420

25-
## Algorithms
21+
- [🚀 TypeScript Algorithms and Data Structures](#-typescript-algorithms-and-data-structures)
22+
- [📋 Table of Contents](#-table-of-contents)
23+
- [🎯 Introduction](#-introduction)
24+
- [✨ Features](#-features)
25+
- [📚 Documentation](#-documentation)
26+
- [📦 Data Structures](#-data-structures)
27+
- [🔍 Algorithms](#-algorithms)
28+
- [🔎 Search Algorithms](#-search-algorithms)
29+
- [🔄 Sorting Algorithms](#-sorting-algorithms)
30+
- [💻 Coding Challenges](#-coding-challenges)
31+
- [👉 Two Pointers](#-two-pointers)
32+
- [🛠️ Utilities](#️-utilities)
33+
- [🚀 Getting Started](#-getting-started)
34+
- [Prerequisites](#prerequisites)
35+
- [Installation](#installation)
36+
- [💻 Development](#-development)
37+
- [Available Scripts](#available-scripts)
38+
- [Project Structure](#project-structure)
39+
- [📝 License](#-license)
40+
- [📧 Contact](#-contact)
2641

27-
### Search
42+
<!-- markdownlint-disable MD033 -->
43+
</details>
44+
<!-- markdownlint-enable MD033 -->
2845

29-
- [BinarySearch](src/algorithms/search/binary-search/binary-search.ts)
30-
- [LinearSearch](src/algorithms/search/linear-search/linear-search.ts)
46+
---
3147

32-
### Sort
48+
## 🎯 Introduction
3349

34-
- [QuickSort](src/algorithms/sort/quick-sort/quick-sort.ts)
35-
- [BubbleSort](src/algorithms/sort/bubble-sort/bubble-sort.ts)
36-
- [BubbleSortSimple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts)
50+
This repository is a **comprehensive collection** of TypeScript algorithms and data structures designed for educational purposes. It serves as a reference for developers who want to learn, practice, and master TypeScript while understanding fundamental computer science concepts.
3751

38-
## Utils
52+
> 💡 **Note:** This repository includes code and patterns from the [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms) repository, translated and enhanced from JavaScript to TypeScript.
3953
40-
- [Comparator](src/ds/comparator/comparator.ts)
54+
---
4155

42-
Please note that the repository is still a work in progress, and new algorithms and patterns will be added over time.
56+
## ✨ Features
4357

44-
## Getting Started
58+
- 🔍 **Well-documented** code with TypeScript types
59+
-**Fully tested** with Jest test suites
60+
- 📚 **Educational** focus with clear implementations
61+
- 🎨 **Clean code** following best practices
62+
- 🚀 **Production-ready** implementations
63+
- 🔄 **Regularly updated** with new algorithms and patterns
4564

46-
To get started with the repository, follow these simple steps:
65+
---
4766

48-
Clone the repository to your local machine using the command:
67+
## 📚 Documentation
4968

50-
```bash
51-
git clone https://github.com/behzadam/algorithms.git
52-
```
69+
### 📦 Data Structures
5370

54-
Install the required dependencies by running the following command:
71+
| Data Structure | Description | Implementation |
72+
| -------------------- | ------------------------------------ | -------------------------------------------------------------------- |
73+
| **HashTable** | Key-value mapping with hash function | [View Code](src/data-structure/hash-table/hash-table.ts) |
74+
| **MinHeap** | Binary heap with minimum root | [View Code](src/data-structure/heap/min-heap.ts) |
75+
| **MaxHeap** | Binary heap with maximum root | [View Code](src/data-structure/heap/max-heap.ts) |
76+
| **LinkedList** | Linear collection of nodes | [View Code](src/data-structure/linked-list/linked-list.ts) |
77+
| **Queue** | FIFO (First In First Out) structure | [View Code](src/data-structure/queue/queue.ts) |
78+
| **Stack** | LIFO (Last In First Out) structure | [View Code](src/data-structure/stack/stack.ts) |
79+
| **MaxPriorityQueue** | Priority queue with max priority | [View Code](src/data-structure/priority-queue/max-priority-queue.ts) |
80+
| **MinPriorityQueue** | Priority queue with min priority | [View Code](src/data-structure/priority-queue/min-priority-queue.ts) |
81+
| **Trie** | Prefix tree for string operations | [View Code](src/data-structure/trie/trie.ts) |
82+
| **BinaryTree** | Hierarchical tree structure | [View Code](src/data-structure/tree/binary-tree.ts) |
5583

56-
```bash
57-
pnpm install
58-
```
84+
### 🔍 Algorithms
85+
86+
#### 🔎 Search Algorithms
87+
88+
- **[Binary Search](src/algorithms/search/binary-search/binary-search.ts)** - Efficient search in sorted arrays
89+
- **[Linear Search](src/algorithms/search/linear-search/linear-search.ts)** - Simple sequential search
90+
91+
#### 🔄 Sorting Algorithms
92+
93+
- **[Quick Sort](src/algorithms/sort/quick/quick-sort.ts)** - Divide and conquer sorting algorithm
94+
- **[Bubble Sort](src/algorithms/sort/bubble-sort/bubble-sort.ts)** - Simple comparison-based sort
95+
- **[Bubble Sort Simple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts)** - Simplified bubble sort implementation
96+
97+
### 💻 Coding Challenges
98+
99+
#### 👉 Two Pointers
100+
101+
- **[Pair Sum Sorted](src/code-challanges/two-pointers/pair-sum-sorted.ts)** - Find pairs in sorted array that sum to target
102+
103+
### 🛠️ Utilities
104+
105+
- **[Comparator](src/utils/comparator.ts)** - Comparison functions for sorting and searching
106+
- **[Range](src/utils/range.ts)** - Utility for generating number ranges
107+
- **[Type Definitions](src/types/)** - Common TypeScript type definitions
108+
109+
---
110+
111+
## 🚀 Getting Started
112+
113+
### Prerequisites
114+
115+
- **Node.js** (v16 or higher)
116+
- **pnpm** (or npm/yarn)
117+
118+
### Installation
119+
120+
1. **Clone the repository**
121+
122+
```bash
123+
git clone https://github.com/behzadam/algorithms.git
124+
cd algorithms
125+
```
126+
127+
2. **Install dependencies**
128+
129+
```bash
130+
pnpm install
131+
```
132+
133+
3. **Run tests**
134+
135+
```bash
136+
pnpm test
137+
```
59138

60-
Run all tests
139+
---
140+
141+
## 💻 Development
142+
143+
### Available Scripts
144+
145+
<!-- markdownlint-disable MD033 -->
146+
<details>
147+
<summary><b>Test Commands</b></summary>
148+
<!-- markdownlint-enable MD033 -->
61149

62150
```bash
151+
# Run all tests
63152
pnpm test
64-
```
65153

66-
Format checking
154+
# Run tests in watch mode
155+
pnpm test:watch
67156

68-
```bash
69-
pnpm format:check
157+
# Run tests with coverage
158+
pnpm test:coverage
70159
```
71160

72-
Format fixing
161+
<!-- markdownlint-disable MD033 -->
162+
</details>
163+
164+
<details>
165+
<summary><b>Code Quality Commands</b></summary>
166+
<!-- markdownlint-enable MD033 -->
73167

74168
```bash
75-
pnpm format:fix
76-
```
169+
# Check code formatting
170+
pnpm format:check
77171

78-
Run ESLint
172+
# Fix code formatting
173+
pnpm format:fix
79174

80-
```bash
175+
# Run ESLint
81176
pnpm lint
177+
178+
# Fix ESLint issues
179+
pnpm lint:fix
180+
```
181+
182+
<!-- markdownlint-disable MD033 -->
183+
</details>
184+
<!-- markdownlint-enable MD033 -->
185+
186+
### Project Structure
187+
188+
```text
189+
algorithms/
190+
├── src/
191+
│ ├── algorithms/ # Algorithm implementations
192+
│ │ ├── search/ # Search algorithms
193+
│ │ └── sort/ # Sorting algorithms
194+
│ ├── code-challanges/ # Coding challenges
195+
│ ├── data-structure/ # Data structure implementations
196+
│ ├── types/ # TypeScript type definitions
197+
│ └── utils/ # Utility functions
198+
├── jest.config.js # Jest configuration
199+
├── tsconfig.json # TypeScript configuration
200+
└── package.json # Project dependencies
82201
```
83202

84-
## License
203+
---
204+
205+
## 📝 License
206+
207+
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
208+
209+
---
210+
211+
## 📧 Contact
212+
213+
Have questions or suggestions? Feel free to reach out!
214+
215+
- **Email:** [[email protected]](mailto:[email protected])
216+
- **GitHub:** [@behzadam](https://github.com/behzadam)
217+
218+
---
219+
220+
<!-- markdownlint-disable MD033 -->
221+
<div align="center">
85222

86-
The "TypeScript Algorithms, Data Structures and Patterns" repository is licensed under the [MIT License](https://opensource.org/licenses/MIT). Feel free to use, modify, and distribute the code as per the terms of the license.
223+
**⭐ Star this repo if you find it helpful!**
87224

88-
## Contact
225+
Made with ❤️ by [behzadam](https://github.com/behzadam)
89226

90-
If you have any questions or suggestions regarding the repository, please feel free to [contact me](mailto:[email protected]).
227+
</div>
228+
<!-- markdownlint-enable MD033 -->

src/code-challanges/two-pointers/pair-sum.test.ts renamed to src/code-challanges/two-pointers/pair-sum-sorted.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { pairSumSortedBruteForce } from "./pair-sum";
1+
import { pairSumSorted } from "./pair-sum-sorted";
22

3-
describe("pairSumSortedBruteForce", () => {
3+
describe("pairSumSorted", () => {
44
it.each([
55
{
66
input: [],
@@ -25,7 +25,7 @@ describe("pairSumSortedBruteForce", () => {
2525
])(
2626
"returns $expected for input $input and target $target",
2727
({ input, target, expected }) => {
28-
expect(pairSumSortedBruteForce(input, target)).toEqual(expected);
28+
expect(pairSumSorted(input, target)).toEqual(expected);
2929
}
3030
);
3131
});

src/code-challanges/two-pointers/pair-sum.ts renamed to src/code-challanges/two-pointers/pair-sum-sorted.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* The time complexity of this solution is O(n) because we are using two pointers to traverse the array.
1212
* The space complexity is O(1) because we are not using any extra space.
1313
*/
14-
function pairSumSortedBruteForce(arr: number[], target: number) {
14+
export function pairSumSorted(arr: number[], target: number) {
1515
let left = 0;
1616
let right = arr.length - 1;
1717
while (left < right) {
@@ -32,5 +32,3 @@ function pairSumSortedBruteForce(arr: number[], target: number) {
3232
}
3333
return [];
3434
}
35-
36-
export { pairSumSortedBruteForce };

0 commit comments

Comments
 (0)