Skip to content

[DOC] Better README. #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
236 changes: 170 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,146 +16,250 @@
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-js&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-js)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-js&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-js)

## TL;DR

[Install and run](#install-and-run)

## What is this?

[Project Euler](https://projecteuler.net/) provide some algorithms and mathematical
problems to solve to be used as experience tests.
This repository is part of a series that share and solve the same [objectives](#objetives),
with the difference that each one is based on a different software ecosystem,
depending on the chosen programming language:

Use this answers to learn some tip and tricks for algorithms tests.
- [Modern Javascript: algorithm-exercises-js](https://github.com/sir-gon/algorithm-exercises-js)
- [Python 3.x: algorithm-exercises-py](https://github.com/sir-gon/algorithm-exercises-py)
- [Typescript: algorithm-exercises-ts](https://github.com/sir-gon/algorithm-exercises-ts)
- [Go / Golang: algorithm-exercises-go](https://github.com/sir-gon/algorithm-exercises-go)
- [Java: algorithm-exercises-java](https://github.com/sir-gon/algorithm-exercises-java)
- [.NET / C#: algorithm-exercises-csharp](https://github.com/sir-gon/algorithm-exercises-csharp)

## Why I publish solutions?
## Objetives

As Project Euler says:
### Functional

<https://projecteuler.net/about#publish>
- For academic purposes, it is an backup of some algorithm exercises
(with their solutions), proposed by various sources:
[leetcode, hackerrank, projecteuler](#algorithm-excersices-sources), ...

```text
I learned so much solving problem XXX, so is it okay to publish my solution elsewhere?
It appears that you have answered your own question. There is nothing quite like that "Aha!" moment when you finally beat a problem which you have been working on for some time. It is often through the best of intentions in wishing to share our insights so that others can enjoy that moment too. Sadly, that will rarely be the case for your readers. Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery. Please do not deny others what you have so richly valued yourself.
- The solutions must be written on "vanilla code", that is,
avoiding as much as possible the use of external libraries (in runtime).

However, the rule about sharing solutions outside of Project Euler does not apply to the first one-hundred problems, as long as any discussion clearly aims to instruct methods, not just provide answers, and does not directly threaten to undermine the enjoyment of solving later problems. Problems 1 to 100 provide a wealth of helpful introductory teaching material and if you are able to respect our requirements, then we give permission for those problems and their solutions to be discussed elsewhere.
```
- Adoption of methodology and good practices.
Each exercise is implemented as a unit test set,
using TDD (Test-driven Development) and Clean Code ideas.

If you have better answers or optimal solutions, fork and PR-me
### Technical

Enjoy 😁 !
Foundation of a project that supports:

## Using NodeJS runtime
- Explicit **typing** when the language supports it, even when it is not mandatory.
- Static Code Analysis (**Lint**) of code, scripts and documentation.
- Uniform **Code Styling**.
- **Unit Test** framework.
- **Coverge** collection. High coverage percentage. Equal or close to 100%.
- **Pipeline** (Github Actions). Each command must take care of its
return status code.
- **Docker**-based workflow to replicate behavior in any environment.
- Other tools to support the reinforcement of software development **good practices**.

### Requirements
## Install and Run

You must install dependencies:
You can run tests in the following ways:

```text
npm install
```
- [Install and run directly](#install-and-run-directly) require runtime tools
installed in your SO.
- [Install and run with make](#install-and-run-using-make) require runtime tools
and "make" installed in your SO.
- [Install and in Docker](#install-and-running-with-docker-) require Docker and
docker-compose installed.
- (⭐️)
[Install and in Docker with make](#install-and-running-with-docker--using-make)
require docker-compose and make installed.

Or using make
⭐️: Prefered way.

```text
make dependencies
```
### Install and Run directly

Using a NodeJS runtime in your SO. You must install dependencies:

### Testing silently
```bash
npm install
```

Every problem is a function with unit test.

Unit test has test cases and input data to solve the problem.

Run all tests:

```text
```bash
npm run test
```

### Testing with full logs
#### Test run with alternative behaviors

Run all tests with debug outputs:
You can change test running behaviour using some environment variables as follows:

```text
| Variable | Values | Default |
| ------ | ------ | ------ |
| LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` |
| BRUTEFORCE | `true`, `false`| `false` |

- `LOG_LEVEL`: change verbosity level in outputs.
- `BRUTEFORCE`: enable or disable running large tests.
(long time, large amount of data, high memory consumition).

#### Examples running tests with alternative behaviors

Run tests with debug outputs:

```bash
LOG_LEVEL=debug npm run test
```

Use one of following values: debug, warning, error, info.
Run brute-force tests with debug outputs:

### Testing using make

```text
make test
```bash
BRUTEFORCE=true LOG_LEVEL=debug npm run test
```

#### Enable all large BRUTEFORCE tests
### Install and Run using make

Direct in host using a make:
`make` tool is used to standardizes the commands for the same tasks
across each sibling repository.

```text
make test -e BRUTEFORCE=true
Run tests (libraries are installed as dependency task in make):

```bash
make test
```

#### Enable all DEBUG outputs
Run tests with debug outputs:

```text
```bash
make test -e LOG_LEVEL=debug
```

#### Enable all large BRUTEFORCE tests and all DEBUG outputs
Run brute-force tests with debug outputs:

```text
make test -e LOG_LEVEL=debug -e BRUTEFORCE=true
```bash
make test -e BRUTEFORCE=true -e LOG_LEVEL=debug
```

## Running with Docker 🐳
### Install and Running with Docker 🐳

## Build a complete image with and run all tests
Build an image of the test stage.
Then creates and ephemeral container an run tests.

Running container with testing (final) target.
BRUTEFORCE and LOG_LEVEL environment variables are passing from current
environment using docker-compose.

Designed to store all application files and dependencies as a complete runnable image.
Coverage results will be stored in host **/coverage** directory (mounted as volume).
```bash
docker-compose --profile testing run --rm algorithm-exercises-js-test
```

```text
# Build a complete image
docker-compose build algorithm-exercises-js
docker-compose run --rm algorithm-exercises-js npm run test
To change behavior using environment variables, you can pass to containers
in the following ways:

From host using docker-compose (compose.yaml) mechanism:

```bash
BRUTEFORCE=true LOG_LEVEL=debug docker-compose --profile testing run --rm algorithm-exercises-js-test
```

### Enable BRUTEFORCE tests with full DEBUG output
Overriding docker CMD, as parameter of make "-e":

With docker-compose:
```bash
docker-compose --profile testing run --rm algorithm-exercises-js-test make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
```

```text
docker-compose --profile testing run --rm algorithm-exercises-js make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
### Install and Running with Docker 🐳 using make

```bash
make compose/build
make compose/test
```

Using make:
To pass environment variables you can use docker-compose
or overriding CMD and passing to make as "-e" argument.

```text
make docker/compose-run -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
Passing environment variables using docker-compose (compose.yaml mechanism):

```bash
BRUTEFORCE=true LOG_LEVEL=debug make compose/test
```

### Build and run a development image
## Development workflow using Docker / docker-compose

Running container with development target.
Designed to develop on top of this image. All source application is mounted as
a volume in **/app** directory.
Dependencies should be installed to run (not present in this target) so, you
must install dependencies before run (or after a dependency add/change).
Designed for development workflow on top of this image.
All source application is mounted as a volume in **/app** directory.
Dependencies should be installed to run so, you must
install dependencies before run (or after a dependency add/change).

```text
# install node_modules dependencies using docker runtime and store them in host directory
```bash
# Build development target image
docker-compose build --compress algorithm-exercises-js-dev
# run ephemeral container to install dependencies using docker runtime
# and store them in host directory (by bind-mount volume)
docker-compose run --rm algorithm-exercises-js-dev npm install --verbose
# Run ephemeral container and override command to run test
docker-compose run --rm algorithm-exercises-js-dev npm run test
```

## Run complete workflow (Docker + make)

Following command simulates a standarized pipeline across environments,
using docker-compose and make.

```bash
make compose/build && make compose/lint && make compose/test && make compose/run
```

- Build all Docker stages and tag relevant images.
- Run static analysis (lint) checks
- Run unit tests
- Run a "final" production ready image as a final container.
Final "production" image just shows a minimal "production ready"
build (with no tests).

## About development

Developed with runtime:

```text
node --version
v22.1.0
v22.2.0
```

## Algorithm excersices sources

- [Leetcode](https://leetcode.com/) online platform for
coding interview preparation.
- [HackerRank](https://www.hackerrank.com/) competitive programming challenges
for both consumers and businesses.
- [Project Euler](https://projecteuler.net/) a series of computational problems
intended to be solved with computer programs.

Use these answers to learn some tip and tricks for algorithms tests.

### Disclaimer. Why I publish solutions?

As Project Euler says:

<https://projecteuler.net/about#publish>

```text
I learned so much solving problem XXX, so is it okay to publish my solution elsewhere?
It appears that you have answered your own question. There is nothing quite like that "Aha!" moment when you finally beat a problem which you have been working on for some time. It is often through the best of intentions in wishing to share our insights so that others can enjoy that moment too. Sadly, that will rarely be the case for your readers. Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery. Please do not deny others what you have so richly valued yourself.

However, the rule about sharing solutions outside of Project Euler does not apply to the first one-hundred problems, as long as any discussion clearly aims to instruct methods, not just provide answers, and does not directly threaten to undermine the enjoyment of solving later problems. Problems 1 to 100 provide a wealth of helpful introductory teaching material and if you are able to respect our requirements, then we give permission for those problems and their solutions to be discussed elsewhere.
```

If you have better answers or optimal solutions, fork and PR-me

Enjoy 😁 !

### License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bi.8713187.xyz%2Fsir-gon%2Fprojecteuler-js.svg?type=large)](https://app.fossa.com/projects/git%2Bi.8713187.xyz%2Fsir-gon%2Fprojecteuler-js?ref=badge_large)
Expand Down