Skip to content

Commit 5e1d6d6

Browse files
Documentation (#27)
* Added README for Challenge 1 - Write your own WC tool * Added Notion link to README for challenge 1 * Created README for challenge 2 - Write your own JSON parser * Fixed typo * Added README for challenge 3 - Write Your Own Compression Tool * Added README for challenge 4 - Write Your Own cut Tool * Updated `src/4/README.md` examples * Added README for challenge 4 - Write You Own Load Balancer * Added README for challenge 6 - Write Your Own Sort Tool * Added README for challenge 7 - Write Your Own Calculator * Added README for challenge 8 - Write Your Own Redis Server * typo fix * Created README for challenge 9 - Write your own grep * typo fix for challenge 9 README * Completed README for challenge 10 -Write your own uniq tool * Added README for challenge 11 - Write Your Own Web Server * Added README for challenge 13 - Write Your Own diff Tool * Completed README for challenge 14 - Write Your Own Shell * typo fix in README for challenge 14 * Added README for challenge 15 - Write Your Own cat Tool * Added README for challenge 16 - Write Your Own IRC Client * Added README for challenge 17 - Write Your Own Memcached Server * Added README for challenge 19 - Write Your Own Discord Bot * Updated global README and added links to all the challenges.
1 parent 416a47f commit 5e1d6d6

File tree

18 files changed

+812
-0
lines changed

18 files changed

+812
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@ Just trying to learn Typescript and improve my problem solving skills.
1010

1111
I am also trying to incorporate testing, documentation and a better GIT control.
1212

13+
Checkout my [Notion](https://mohitjain.notion.site/Coding-Challenges-af9b8197a438447e9b455ab9e010f9a2?pvs=4) where I share how I tackled these challenges, along with my learnings.
14+
1315
## Structure
1416

1517
- `src` - Contains all the source code
1618
- `tests` - Contains all the test files
1719

20+
## Challenges
21+
22+
1. [Write your own wc tool](src/1/)
23+
2. [Write your own JSON parser](src/2/)
24+
3. [Write Your Own Compression Tool](src/3/)
25+
4. [Write Your Own cut Tool](src/4/)
26+
5. [Write You Own Load Balancer](src/5/)
27+
6. [Write Your Own Sort Tool](src/6/)
28+
7. [Write Your Own Calculator](src/7/)
29+
8. [Write Your Own Redis Server](src/8/)
30+
9. [Write your own grep](src/9/)
31+
10. [Write Your Own uniq Tool](src/10/)
32+
11. [Write Your Own Web Server](src/11/)
33+
12. [Write Your Own URL Shortener](https://github.com/jainmohit2001/short-url)
34+
13. [Write Your Own diff Tool](src/13/)
35+
14. [Write Your Own Shell](src/14/)
36+
15. [Write Your Own cat Tool](src/15/)
37+
16. [Write Your Own IRC Client](src/16/)
38+
17. [Write Your Own Memcached Server](src/17/)
39+
18. [Write Your Own Spotify Client](https://github.com/jainmohit2001/spotify-client)
40+
19. [Write Your Own Discord Bot](src/19/)
41+
1842
## Installation
1943

2044
The following command will build all the .ts files present in `src` folder into a new `build` folder.

src/1/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Challenge 1 - Write your own wc tool
2+
3+
This challenge corresponds to the first part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-wc.
4+
5+
## Description
6+
7+
The WC tool is written in `wc.ts` file and the `index.ts` is the command line version of the tool. The tool is used to count the number of words, lines, bytes and characters in a file/stdin.
8+
9+
Check out [this](https://www.notion.so/mohitjain/1-Write-Your-Own-wc-Tool-b289bb2362c14778880029633b76033b) Notion page to understand how I approached this challenge.
10+
11+
## Usage
12+
13+
You can use `ts-node` to run the tool as follows:
14+
15+
```bash
16+
npx ts-node index.ts [option] filename
17+
```
18+
19+
The following options are supported:
20+
21+
- `-w`: prints the number of words in the file
22+
- `-l`: prints the number of lines in the file
23+
- `-c`: prints the number of bytes in the file
24+
- `-m`: prints the number of characters in the file
25+
26+
The tool can also be used in stdin mode as follows:
27+
28+
```bash
29+
cat filename | npx ts-node index.ts [option]
30+
```
31+
32+
## Run tests
33+
34+
To run the tests for the WC tool, go to the root directory of this repository and run the following command:
35+
36+
```bash
37+
npm run test tests/1/
38+
```
39+
40+
The tests are located in the `tests/1/` directory. All the tests are made for **LINUX** environment only. If you want to run the tests in Windows environment, you can use the Git Bash terminal or Windows Subsystem for Linux (WSL).

src/10/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Challenge 10 - Write Your Own uniq Tool
2+
3+
This challenge corresponds to the tenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-uniq.
4+
5+
## Description
6+
7+
The uniq tool is written in `uniq.ts` file and the `uniq.index.ts` is the command line version of the tool.
8+
9+
## Usage
10+
11+
You can use `ts-node` to run the tool as follows:
12+
13+
```bash
14+
# Using input file
15+
npx ts-node uniq.index.ts [-option] <path/to/input-file>
16+
17+
# Using standard input
18+
cat filename | npx ts-node uniq.index.ts [-option] -
19+
20+
# Using output with input file
21+
npx ts-node uniq.index.ts [-option] <path/to/input-file> <path/to/output-file>
22+
23+
# Using output with standard input
24+
cat filename | npx ts-node uniq.index.ts [-option] - <path/to/output-file>
25+
```
26+
27+
The following options are supported:
28+
29+
- `-c` or `--count`: prefix lines by the number of occurrences
30+
- `-d` or `--repeated`: only print duplicate lines
31+
- `-u`: only print unique lines
32+
- `-`: read from standard input
33+
34+
## Run tests
35+
36+
To run the tests for the uniq tool, go to the root directory of this repository and run the following command:
37+
38+
```bash
39+
npm run test tests/10/
40+
```
41+
42+
The tests are located in the `tests/10/` directory. All the tests are made for **LINUX** environment only. If you want to run the tests in Windows environment, you can use the Git Bash terminal or Windows Subsystem for Linux (WSL).

src/11/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Challenge 11 - Write Your Own Web Server
2+
3+
This challenge corresponds to the eleventh part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-webserver.
4+
5+
## Description
6+
7+
The web server API implementation is inspired from (express)[https://www.npmjs.com/package/express].
8+
The webserver is a basic implementation and a subpart of the express framework.
9+
It current handles only GET requests and supports responses and file responses.
10+
The webserver is able to handle the case when the callback to a GET function throws an error, by sending a 500 response.
11+
12+
- `webserver.ts`: Contains the implementation of the web server, implemented using the `net` module of Node.js. It exposes `startServer`, `stopServer` and `get` methods to start, stop and handle GET requests respectively.
13+
- `index.ts`: A simple example of how to use the web server and serve a HTML file.
14+
- `request.ts`: A simple implementation of the request object that is passed to the callback of the `get` method.
15+
- `status_codes.ts`: Contains the supported status codes and their corresponding messages that are used in the response.
16+
17+
## Usage
18+
19+
You can directly import the HttpServer class from the `webserver.ts` file and use it as follows:
20+
21+
```typescript
22+
import { HttpServer } from './webserver';
23+
24+
// Create a new instance of the HttpServer class with
25+
const HOST = '127.0.0.1';
26+
const PORT = 8000;
27+
const debug = false;
28+
const webServer = new HttpServer(HOST, PORT, debug);
29+
30+
// Handle GET requests
31+
webServer.get('/', (req) => {
32+
res.send('Hello World!');
33+
});
34+
35+
// Serve a HTML file
36+
webServer.get('/index.html', (req) => {
37+
req.sendFile('path/to/index.html');
38+
});
39+
40+
// Start the server
41+
webServer.startServer();
42+
```
43+
44+
## Run tests
45+
46+
To run the tests for the webserver, go to the root directory of this repository and run the following command:
47+
48+
```bash
49+
npm run test tests/11/
50+
```
51+
52+
The tests are located in the `tests/11/` directory.

src/13/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Challenge 13 - Write Your Own diff Tool
2+
3+
This challenge corresponds to the thirteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-diff.
4+
5+
## Description
6+
7+
The diff tool is written in `diff.ts` file and the `diff.index.ts` is the command line version of the tool.
8+
The diff tool is build using the Longest common subsequence (LCS) problem. We first find the LCS of a pair of strings and extend that to a pair of array of strings.
9+
While finding LCS we also store information about the insertions and deletions required to convert one string to another and use that information to print the differences between the two strings.
10+
11+
## Usage
12+
13+
You can use `ts-node` to run the tool as follows:
14+
15+
```bash
16+
# Using input file
17+
npx ts-node diff.index.ts <file1> <file2>
18+
```
19+
20+
## Run tests
21+
22+
To run the tests for the diff tool, go to the root directory of this repository and run the following command:
23+
24+
```bash
25+
npm run test tests/13/
26+
```
27+
28+
The tests are located in the `tests/13/` directory.

src/14/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Challenge 14 - Write Your Own Shell
2+
3+
This challenge corresponds to the fourteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-shell.
4+
5+
## Description
6+
7+
As the name suggests, here we try to build a simple shell using the `child_process` module of Node.js.
8+
The shell supports all the commands (including piped commands) that are available in any standard LINUX shell.
9+
10+
Apart from the builtin commands - `cd`, `pwd`, `exit`, `history` are executed by spawning a new process and passing all the relevant arguments provided by the user.
11+
12+
## Usage
13+
14+
You can use `ts-node` to run the tool as follows:
15+
16+
```bash
17+
npx ts-node shell.ts
18+
```
19+
20+
All the executed commands are stored in a file in the home directory of the user with the name `.ccsh_history`. You can then use the `history` command to see the previously executed commands.
21+
22+
To exit the shell, use the `exit` command.
23+
24+
The `pwd` and `cd` command support is implemented using the inbuilt `cwd()` and `chdir()` function exposed by the process module.
25+
26+
## Run tests
27+
28+
To run the tests for the shell tool, go to the root directory of this repository and run the following command:
29+
30+
```bash
31+
npm run test tests/14/
32+
```
33+
34+
The tests are located in the `tests/14/` directory. All the tests are made for **LINUX** environment only. If you want to run the tests in Windows environment, you can use the Git Bash terminal or Windows Subsystem for Linux (WSL).

src/15/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Challenge 15 - Write Your Own cat Tool
2+
3+
This challenge corresponds to the fifteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-cat.
4+
5+
## Description
6+
7+
The cat utility reads files sequentially, writing them to the standard output.
8+
The file operands are processed in command-line order. If file is a single dash (`-`) or absent, cat reads from the standard input.
9+
10+
## Usage
11+
12+
You can use `ts-node` to run the tool as follows:
13+
14+
```bash
15+
# Using file
16+
npx ts-node cat.ts [-option] [filename]
17+
18+
# Using stdin
19+
cat test.txt | npx ts-node cat.ts [-option]
20+
```
21+
22+
The following options are supported:
23+
24+
- `-n`: number the lines are they are printed out
25+
- `-b`: number the lines excluding blank lines
26+
27+
## Run tests
28+
29+
To run the tests for the cat tool, go to the root directory of this repository and run the following command:
30+
31+
```bash
32+
npm run test tests/15/
33+
```
34+
35+
The tests are located in the `tests/15/` directory. All the tests are made for **LINUX** environment only. If you want to run the tests in Windows environment, you can use the Git Bash terminal or Windows Subsystem for Linux (WSL).

src/16/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Challenge 16 - Write Your Own IRC Client
2+
3+
This challenge corresponds to the sixteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-irc.
4+
5+
## Table of contents
6+
7+
- [Description](#description)
8+
- [Usage](#usage)
9+
- [Run tests](#run-tests)
10+
- [TODOs](#todos)
11+
12+
## Description
13+
14+
The IRC client is written using the `net` module of Node.js.
15+
You can find more about the IRC protocol from https://datatracker.ietf.org/doc/html/rfc2812.
16+
17+
- `command-types.ts`: Command types returned by server and supported by client
18+
- `parser.ts`: A parser class that parses the data sent by server into a more usable message interface
19+
- `types.ts`: All the interface definitions are present in this file
20+
- `utils.ts`: Utility functions used by IRC client
21+
- `irc-client.ts`: The main IRC Client login is written here.
22+
- `irc-client.index.ts`: A command line interface for IRC Client to interact with the server
23+
24+
The IRC Client supports file based logging via [winston](https://www.npmjs.com/package/winston)
25+
26+
The following commands are supported by the IRC Client:
27+
28+
- JOIN
29+
- PART
30+
- NICK
31+
- PRIVMSG
32+
- QUIT
33+
34+
## Usage
35+
36+
To use the IRC Client command line interface, you can update the following variables present in `irc-client.index.ts` and use the `ts-node` command to start the client.
37+
38+
```typescript
39+
const host = 'irc.freenode.net';
40+
const port = 6667;
41+
const nickName = 'MJ'; // Use you nickname
42+
const fullName = 'Mohit Jain'; // Use your full name
43+
const debug = true; // Enable winston logging
44+
```
45+
46+
```bash
47+
npx ts-node irc-client.index.ts
48+
```
49+
50+
The following commands are supported by the IRC Client:
51+
52+
```bash
53+
# Connect to server
54+
client>connect
55+
56+
# Join a channel
57+
client>/join <channel-name>
58+
59+
# Leave a channel
60+
client>/part <channel-name>
61+
62+
# Change your nickname
63+
client>/nick <new-nickname>
64+
65+
# Send a message to a channel
66+
client>/privmsg <channel-name> <message>
67+
68+
# Quit the IRC Client
69+
client>/quit
70+
```
71+
72+
You can also use the IRC Client Class in your own code by importing it from `irc-client.ts`.
73+
74+
```typescript
75+
import IRCClient from './irc-client';
76+
77+
// logger is an instance of winston.Logger otherwise undefined
78+
const client = new IRCClient(host, port, nickName, fullName, debug, logger);
79+
80+
// Connect the client to the server
81+
await client.connect();
82+
83+
// Join a channel
84+
await client.join([{ channel: '#cc' }]);
85+
86+
// Send message to a channel
87+
client.privateMessage('#cc', 'Hello World!');
88+
89+
// Part a channel
90+
await client.part({ channels: ['#cc'], partMessage: 'Bye Bye' });
91+
92+
// Update your nickname
93+
await client.nick('MJ');
94+
95+
// Quit the server
96+
await client.quit('Bye Bye');
97+
98+
// Get details about a channel
99+
const channelDetails = client.getChannelDetails('#cc');
100+
```
101+
102+
You can add listeners to different command types supported by the IRC Client. The client exposes the `on()` method as mentioned in [types.ts](https://github.com/jainmohit2001/coding-challenges/blob/416a47f715fff82964bd8def81c26ab72cfe8978/src/16/types.ts#L189).
103+
104+
## Run tests
105+
106+
To run the tests for the IRC Client, go to the root directory of this repository and run the following command:
107+
108+
```bash
109+
npm run test tests/16/
110+
```
111+
112+
The tests are located in the `tests/16/` directory.
113+
114+
## TODOs
115+
116+
- [ ] Add support for multiple channels for JOIN and PART commands.
117+
- [ ] Add command line option support for `irc-client.index.ts`.
118+
- [ ] Separate the command handling from the main IRC client code into more a structured and scalable format.
119+
- [ ] Improve tests.

0 commit comments

Comments
 (0)