Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1a51021
Added README for Challenge 1 - Write your own WC tool
jainmohit2001 Aug 29, 2023
a94c3ba
Added Notion link to README for challenge 1
jainmohit2001 Aug 29, 2023
c30e330
Created README for challenge 2 - Write your own JSON parser
jainmohit2001 Aug 29, 2023
0a0bbe1
Fixed typo
jainmohit2001 Aug 29, 2023
50cbed7
Added README for challenge 3 - Write Your Own Compression Tool
jainmohit2001 Aug 29, 2023
7262c50
Added README for challenge 4 - Write Your Own cut Tool
jainmohit2001 Aug 29, 2023
fab4e40
Updated `src/4/README.md` examples
jainmohit2001 Aug 29, 2023
90d8915
Added README for challenge 4 - Write You Own Load Balancer
jainmohit2001 Aug 29, 2023
a173093
Added README for challenge 6 - Write Your Own Sort Tool
jainmohit2001 Aug 30, 2023
b679037
Added README for challenge 7 - Write Your Own Calculator
jainmohit2001 Aug 30, 2023
adb2cab
Added README for challenge 8 - Write Your Own Redis Server
jainmohit2001 Aug 30, 2023
9805173
typo fix
jainmohit2001 Aug 30, 2023
9dc8753
Created README for challenge 9 - Write your own grep
jainmohit2001 Aug 31, 2023
73ce0cf
typo fix for challenge 9 README
jainmohit2001 Aug 31, 2023
15e95b2
Completed README for challenge 10 -Write your own uniq tool
jainmohit2001 Aug 31, 2023
b724012
Added README for challenge 11 - Write Your Own Web Server
jainmohit2001 Aug 31, 2023
59173c0
Added README for challenge 13 - Write Your Own diff Tool
jainmohit2001 Aug 31, 2023
17d55b0
Completed README for challenge 14 - Write Your Own Shell
jainmohit2001 Aug 31, 2023
546ce3a
typo fix in README for challenge 14
jainmohit2001 Aug 31, 2023
dfd2523
Added README for challenge 15 - Write Your Own cat Tool
jainmohit2001 Aug 31, 2023
9f12ab2
Added README for challenge 16 - Write Your Own IRC Client
jainmohit2001 Aug 31, 2023
ad86a93
Added README for challenge 17 - Write Your Own Memcached Server
jainmohit2001 Aug 31, 2023
e38c6d2
Added README for challenge 19 - Write Your Own Discord Bot
jainmohit2001 Aug 31, 2023
8fec034
Updated global README and added links to all the challenges.
jainmohit2001 Aug 31, 2023
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,35 @@ Just trying to learn Typescript and improve my problem solving skills.

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

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

## Structure

- `src` - Contains all the source code
- `tests` - Contains all the test files

## Challenges

1. [Write your own wc tool](src/1/)
2. [Write your own JSON parser](src/2/)
3. [Write Your Own Compression Tool](src/3/)
4. [Write Your Own cut Tool](src/4/)
5. [Write You Own Load Balancer](src/5/)
6. [Write Your Own Sort Tool](src/6/)
7. [Write Your Own Calculator](src/7/)
8. [Write Your Own Redis Server](src/8/)
9. [Write your own grep](src/9/)
10. [Write Your Own uniq Tool](src/10/)
11. [Write Your Own Web Server](src/11/)
12. [Write Your Own URL Shortener](https://github.com/jainmohit2001/short-url)
13. [Write Your Own diff Tool](src/13/)
14. [Write Your Own Shell](src/14/)
15. [Write Your Own cat Tool](src/15/)
16. [Write Your Own IRC Client](src/16/)
17. [Write Your Own Memcached Server](src/17/)
18. [Write Your Own Spotify Client](https://github.com/jainmohit2001/spotify-client)
19. [Write Your Own Discord Bot](src/19/)

## Installation

The following command will build all the .ts files present in `src` folder into a new `build` folder.
Expand Down
40 changes: 40 additions & 0 deletions src/1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Challenge 1 - Write your own wc tool

This challenge corresponds to the first part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-wc.

## Description

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.

Check out [this](https://www.notion.so/mohitjain/1-Write-Your-Own-wc-Tool-b289bb2362c14778880029633b76033b) Notion page to understand how I approached this challenge.

## Usage

You can use `ts-node` to run the tool as follows:

```bash
npx ts-node index.ts [option] filename
```

The following options are supported:

- `-w`: prints the number of words in the file
- `-l`: prints the number of lines in the file
- `-c`: prints the number of bytes in the file
- `-m`: prints the number of characters in the file

The tool can also be used in stdin mode as follows:

```bash
cat filename | npx ts-node index.ts [option]
```

## Run tests

To run the tests for the WC tool, go to the root directory of this repository and run the following command:

```bash
npm run test tests/1/
```

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).
42 changes: 42 additions & 0 deletions src/10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Challenge 10 - Write Your Own uniq Tool

This challenge corresponds to the tenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-uniq.

## Description

The uniq tool is written in `uniq.ts` file and the `uniq.index.ts` is the command line version of the tool.

## Usage

You can use `ts-node` to run the tool as follows:

```bash
# Using input file
npx ts-node uniq.index.ts [-option] <path/to/input-file>

# Using standard input
cat filename | npx ts-node uniq.index.ts [-option] -

# Using output with input file
npx ts-node uniq.index.ts [-option] <path/to/input-file> <path/to/output-file>

# Using output with standard input
cat filename | npx ts-node uniq.index.ts [-option] - <path/to/output-file>
```

The following options are supported:

- `-c` or `--count`: prefix lines by the number of occurrences
- `-d` or `--repeated`: only print duplicate lines
- `-u`: only print unique lines
- `-`: read from standard input

## Run tests

To run the tests for the uniq tool, go to the root directory of this repository and run the following command:

```bash
npm run test tests/10/
```

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).
52 changes: 52 additions & 0 deletions src/11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Challenge 11 - Write Your Own Web Server

This challenge corresponds to the eleventh part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-webserver.

## Description

The web server API implementation is inspired from (express)[https://www.npmjs.com/package/express].
The webserver is a basic implementation and a subpart of the express framework.
It current handles only GET requests and supports responses and file responses.
The webserver is able to handle the case when the callback to a GET function throws an error, by sending a 500 response.

- `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.
- `index.ts`: A simple example of how to use the web server and serve a HTML file.
- `request.ts`: A simple implementation of the request object that is passed to the callback of the `get` method.
- `status_codes.ts`: Contains the supported status codes and their corresponding messages that are used in the response.

## Usage

You can directly import the HttpServer class from the `webserver.ts` file and use it as follows:

```typescript
import { HttpServer } from './webserver';

// Create a new instance of the HttpServer class with
const HOST = '127.0.0.1';
const PORT = 8000;
const debug = false;
const webServer = new HttpServer(HOST, PORT, debug);

// Handle GET requests
webServer.get('/', (req) => {
res.send('Hello World!');
});

// Serve a HTML file
webServer.get('/index.html', (req) => {
req.sendFile('path/to/index.html');
});

// Start the server
webServer.startServer();
```

## Run tests

To run the tests for the webserver, go to the root directory of this repository and run the following command:

```bash
npm run test tests/11/
```

The tests are located in the `tests/11/` directory.
28 changes: 28 additions & 0 deletions src/13/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Challenge 13 - Write Your Own diff Tool

This challenge corresponds to the thirteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-diff.

## Description

The diff tool is written in `diff.ts` file and the `diff.index.ts` is the command line version of the tool.
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.
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.

## Usage

You can use `ts-node` to run the tool as follows:

```bash
# Using input file
npx ts-node diff.index.ts <file1> <file2>
```

## Run tests

To run the tests for the diff tool, go to the root directory of this repository and run the following command:

```bash
npm run test tests/13/
```

The tests are located in the `tests/13/` directory.
34 changes: 34 additions & 0 deletions src/14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Challenge 14 - Write Your Own Shell

This challenge corresponds to the fourteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-shell.

## Description

As the name suggests, here we try to build a simple shell using the `child_process` module of Node.js.
The shell supports all the commands (including piped commands) that are available in any standard LINUX shell.

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.

## Usage

You can use `ts-node` to run the tool as follows:

```bash
npx ts-node shell.ts
```

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.

To exit the shell, use the `exit` command.

The `pwd` and `cd` command support is implemented using the inbuilt `cwd()` and `chdir()` function exposed by the process module.

## Run tests

To run the tests for the shell tool, go to the root directory of this repository and run the following command:

```bash
npm run test tests/14/
```

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).
35 changes: 35 additions & 0 deletions src/15/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Challenge 15 - Write Your Own cat Tool

This challenge corresponds to the fifteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-cat.

## Description

The cat utility reads files sequentially, writing them to the standard output.
The file operands are processed in command-line order. If file is a single dash (`-`) or absent, cat reads from the standard input.

## Usage

You can use `ts-node` to run the tool as follows:

```bash
# Using file
npx ts-node cat.ts [-option] [filename]

# Using stdin
cat test.txt | npx ts-node cat.ts [-option]
```

The following options are supported:

- `-n`: number the lines are they are printed out
- `-b`: number the lines excluding blank lines

## Run tests

To run the tests for the cat tool, go to the root directory of this repository and run the following command:

```bash
npm run test tests/15/
```

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).
119 changes: 119 additions & 0 deletions src/16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Challenge 16 - Write Your Own IRC Client

This challenge corresponds to the sixteenth part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-irc.

## Table of contents

- [Description](#description)
- [Usage](#usage)
- [Run tests](#run-tests)
- [TODOs](#todos)

## Description

The IRC client is written using the `net` module of Node.js.
You can find more about the IRC protocol from https://datatracker.ietf.org/doc/html/rfc2812.

- `command-types.ts`: Command types returned by server and supported by client
- `parser.ts`: A parser class that parses the data sent by server into a more usable message interface
- `types.ts`: All the interface definitions are present in this file
- `utils.ts`: Utility functions used by IRC client
- `irc-client.ts`: The main IRC Client login is written here.
- `irc-client.index.ts`: A command line interface for IRC Client to interact with the server

The IRC Client supports file based logging via [winston](https://www.npmjs.com/package/winston)

The following commands are supported by the IRC Client:

- JOIN
- PART
- NICK
- PRIVMSG
- QUIT

## Usage

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.

```typescript
const host = 'irc.freenode.net';
const port = 6667;
const nickName = 'MJ'; // Use you nickname
const fullName = 'Mohit Jain'; // Use your full name
const debug = true; // Enable winston logging
```

```bash
npx ts-node irc-client.index.ts
```

The following commands are supported by the IRC Client:

```bash
# Connect to server
client>connect

# Join a channel
client>/join <channel-name>

# Leave a channel
client>/part <channel-name>

# Change your nickname
client>/nick <new-nickname>

# Send a message to a channel
client>/privmsg <channel-name> <message>

# Quit the IRC Client
client>/quit
```

You can also use the IRC Client Class in your own code by importing it from `irc-client.ts`.

```typescript
import IRCClient from './irc-client';

// logger is an instance of winston.Logger otherwise undefined
const client = new IRCClient(host, port, nickName, fullName, debug, logger);

// Connect the client to the server
await client.connect();

// Join a channel
await client.join([{ channel: '#cc' }]);

// Send message to a channel
client.privateMessage('#cc', 'Hello World!');

// Part a channel
await client.part({ channels: ['#cc'], partMessage: 'Bye Bye' });

// Update your nickname
await client.nick('MJ');

// Quit the server
await client.quit('Bye Bye');

// Get details about a channel
const channelDetails = client.getChannelDetails('#cc');
```

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).

## Run tests

To run the tests for the IRC Client, go to the root directory of this repository and run the following command:

```bash
npm run test tests/16/
```

The tests are located in the `tests/16/` directory.

## TODOs

- [ ] Add support for multiple channels for JOIN and PART commands.
- [ ] Add command line option support for `irc-client.index.ts`.
- [ ] Separate the command handling from the main IRC client code into more a structured and scalable format.
- [ ] Improve tests.
Loading