|
16 | 16 | [](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-js)
|
17 | 17 | [](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-js)
|
18 | 18 |
|
| 19 | +## TL;DR |
| 20 | + |
| 21 | +[Install and run](#install-and-run) |
| 22 | + |
19 | 23 | ## What is this?
|
20 | 24 |
|
21 |
| -[Project Euler](https://projecteuler.net/) provide some algorithms and mathematical |
22 |
| - problems to solve to be used as experience tests. |
| 25 | +This repository is part of a series that share and solve the same [objectives](#objetives), |
| 26 | +with the difference that each one is based on a different software ecosystem, |
| 27 | +depending on the chosen programming language: |
23 | 28 |
|
24 |
| -Use this answers to learn some tip and tricks for algorithms tests. |
| 29 | +- [Modern Javascript: algorithm-exercises-js](https://github.com/sir-gon/algorithm-exercises-js) |
| 30 | +- [Python 3.x: algorithm-exercises-py](https://github.com/sir-gon/algorithm-exercises-py) |
| 31 | +- [Typescript: algorithm-exercises-ts](https://github.com/sir-gon/algorithm-exercises-ts) |
| 32 | +- [Go / Golang: algorithm-exercises-go](https://github.com/sir-gon/algorithm-exercises-go) |
| 33 | +- [Java: algorithm-exercises-java](https://github.com/sir-gon/algorithm-exercises-java) |
| 34 | +- [.NET / C#: algorithm-exercises-csharp](https://github.com/sir-gon/algorithm-exercises-csharp) |
25 | 35 |
|
26 |
| -## Why I publish solutions? |
| 36 | +## Objetives |
27 | 37 |
|
28 |
| -As Project Euler says: |
| 38 | +### Functional |
29 | 39 |
|
30 |
| -<https://projecteuler.net/about#publish> |
| 40 | +- For academic purposes, it is an backup of some algorithm exercises |
| 41 | +(with their solutions), proposed by various sources: |
| 42 | +[leetcode, hackerrank, projecteuler](#algorithm-excersices-sources), ... |
31 | 43 |
|
32 |
| -```text |
33 |
| -I learned so much solving problem XXX, so is it okay to publish my solution elsewhere? |
34 |
| -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. |
| 44 | +- The solutions must be written on "vanilla code", that is, |
| 45 | +avoiding as much as possible the use of external libraries (in runtime). |
35 | 46 |
|
36 |
| -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. |
37 |
| -``` |
| 47 | +- Adoption of methodology and good practices. |
| 48 | +Each exercise is implemented as a unit test set, |
| 49 | +using TDD (Test-driven Development) and Clean Code ideas. |
38 | 50 |
|
39 |
| -If you have better answers or optimal solutions, fork and PR-me |
| 51 | +### Technical |
40 | 52 |
|
41 |
| -Enjoy 😁 ! |
| 53 | +Foundation of a project that supports: |
42 | 54 |
|
43 |
| -## Using NodeJS runtime |
| 55 | +- Explicit **typing** when the language supports it, even when it is not mandatory. |
| 56 | +- Static Code Analysis (**Lint**) of code, scripts and documentation. |
| 57 | +- Uniform **Code Styling**. |
| 58 | +- **Unit Test** framework. |
| 59 | +- **Coverge** collection. High coverage percentage. Equal or close to 100%. |
| 60 | +- **Pipeline** (Github Actions). Each command must take care of its |
| 61 | +return status code. |
| 62 | +- **Docker**-based workflow to replicate behavior in any environment. |
| 63 | +- Other tools to support the reinforcement of software development **good practices**. |
44 | 64 |
|
45 |
| -### Requirements |
| 65 | +## Install and Run |
46 | 66 |
|
47 |
| -You must install dependencies: |
| 67 | +You can run tests in the following ways: |
48 | 68 |
|
49 |
| -```text |
50 |
| -npm install |
51 |
| -``` |
| 69 | +- [Install and run directly](#install-and-run-directly) require runtime tools |
| 70 | +installed in your SO. |
| 71 | +- [Install and run with make](#install-and-run-using-make) require runtime tools |
| 72 | +and "make" installed in your SO. |
| 73 | +- [Install and in Docker](#install-and-running-with-docker-) require Docker and |
| 74 | +docker-compose installed. |
| 75 | +- (⭐️) |
| 76 | +[Install and in Docker with make](#install-and-running-with-docker--using-make) |
| 77 | +require docker-compose and make installed. |
52 | 78 |
|
53 |
| -Or using make |
| 79 | +⭐️: Prefered way. |
54 | 80 |
|
55 |
| -```text |
56 |
| -make dependencies |
57 |
| -``` |
| 81 | +### Install and Run directly |
58 | 82 |
|
59 |
| -### Testing silently |
| 83 | +Using a NodeJS runtime in your SO. You must install dependencies: |
| 84 | + |
| 85 | +```bash |
| 86 | +npm install |
| 87 | +``` |
60 | 88 |
|
61 | 89 | Every problem is a function with unit test.
|
| 90 | + |
62 | 91 | Unit test has test cases and input data to solve the problem.
|
63 | 92 |
|
64 | 93 | Run all tests:
|
65 | 94 |
|
66 |
| -```text |
| 95 | +```bash |
67 | 96 | npm run test
|
68 | 97 | ```
|
69 | 98 |
|
70 |
| -### Testing with full logs |
| 99 | +#### Test run with alternative behaviors |
71 | 100 |
|
72 |
| -Run all tests with debug outputs: |
| 101 | +You can change test running behaviour using some environment variables as follows: |
73 | 102 |
|
74 |
| -```text |
| 103 | +| Variable | Values | Default | |
| 104 | +| ------ | ------ | ------ | |
| 105 | +| LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` | |
| 106 | +| BRUTEFORCE | `true`, `false`| `false` | |
| 107 | + |
| 108 | +- `LOG_LEVEL`: change verbosity level in outputs. |
| 109 | +- `BRUTEFORCE`: enable or disable running large tests. |
| 110 | +(long time, large amount of data, high memory consumition). |
| 111 | + |
| 112 | +#### Examples running tests with alternative behaviors |
| 113 | + |
| 114 | +Run tests with debug outputs: |
| 115 | + |
| 116 | +```bash |
75 | 117 | LOG_LEVEL=debug npm run test
|
76 | 118 | ```
|
77 | 119 |
|
78 |
| -Use one of following values: debug, warning, error, info. |
79 |
| - |
80 |
| -### Testing using make |
| 120 | +Run brute-force tests with debug outputs: |
81 | 121 |
|
82 |
| -```text |
83 |
| -make test |
| 122 | +```bash |
| 123 | +BRUTEFORCE=true LOG_LEVEL=debug npm run test |
84 | 124 | ```
|
85 | 125 |
|
86 |
| -#### Enable all large BRUTEFORCE tests |
| 126 | +### Install and Run using make |
87 | 127 |
|
88 |
| -Direct in host using a make: |
| 128 | +`make` tool is used to standardizes the commands for the same tasks |
| 129 | +across each sibling repository. |
89 | 130 |
|
90 |
| -```text |
91 |
| -make test -e BRUTEFORCE=true |
| 131 | +Run tests (libraries are installed as dependency task in make): |
| 132 | + |
| 133 | +```bash |
| 134 | +make test |
92 | 135 | ```
|
93 | 136 |
|
94 |
| -#### Enable all DEBUG outputs |
| 137 | +Run tests with debug outputs: |
95 | 138 |
|
96 |
| -```text |
| 139 | +```bash |
97 | 140 | make test -e LOG_LEVEL=debug
|
98 | 141 | ```
|
99 | 142 |
|
100 |
| -#### Enable all large BRUTEFORCE tests and all DEBUG outputs |
| 143 | +Run brute-force tests with debug outputs: |
101 | 144 |
|
102 |
| -```text |
103 |
| -make test -e LOG_LEVEL=debug -e BRUTEFORCE=true |
| 145 | +```bash |
| 146 | +make test -e BRUTEFORCE=true -e LOG_LEVEL=debug |
104 | 147 | ```
|
105 | 148 |
|
106 |
| -## Running with Docker 🐳 |
| 149 | +Alternative way, use environment variables as prefix: |
107 | 150 |
|
108 |
| -## Build a complete image with and run all tests |
| 151 | +```bash |
| 152 | +BRUTEFORCE=true LOG_LEVEL=debug make test |
| 153 | +``` |
109 | 154 |
|
110 |
| -Running container with testing (final) target. |
| 155 | +### Install and Running with Docker 🐳 |
111 | 156 |
|
112 |
| -Designed to store all application files and dependencies as a complete runnable image. |
113 |
| -Coverage results will be stored in host **/coverage** directory (mounted as volume). |
| 157 | +Build an image of the test stage. |
| 158 | +Then creates and ephemeral container an run tests. |
114 | 159 |
|
115 |
| -```text |
116 |
| -# Build a complete image |
117 |
| -docker-compose build algorithm-exercises-js |
118 |
| -docker-compose run --rm algorithm-exercises-js npm run test |
| 160 | +BRUTEFORCE and LOG_LEVEL environment variables are passing from current |
| 161 | +environment using docker-compose. |
| 162 | + |
| 163 | +```bash |
| 164 | +docker-compose --profile testing run --rm algorithm-exercises-js-test |
119 | 165 | ```
|
120 | 166 |
|
121 |
| -### Enable BRUTEFORCE tests with full DEBUG output |
| 167 | +To change behavior using environment variables, you can pass to containers |
| 168 | +in the following ways: |
122 | 169 |
|
123 |
| -With docker-compose: |
| 170 | +From host using docker-compose (compose.yaml) mechanism: |
124 | 171 |
|
125 |
| -```text |
126 |
| -docker-compose --profile testing run --rm algorithm-exercises-js make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true |
| 172 | +```bash |
| 173 | +BRUTEFORCE=true LOG_LEVEL=debug docker-compose --profile testing run --rm algorithm-exercises-js-test |
127 | 174 | ```
|
128 | 175 |
|
129 |
| -Using make: |
| 176 | +Overriding docker CMD, as parameter of make "-e": |
130 | 177 |
|
131 |
| -```text |
132 |
| -make docker/compose-run -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true |
| 178 | +```bash |
| 179 | +docker-compose --profile testing run --rm algorithm-exercises-js-test make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true |
| 180 | +``` |
| 181 | + |
| 182 | +### Install and Running with Docker 🐳 using make |
| 183 | + |
| 184 | +```bash |
| 185 | +make compose/build |
| 186 | +make compose/test |
| 187 | +``` |
| 188 | + |
| 189 | +To pass environment variables you can use docker-compose |
| 190 | +or overriding CMD and passing to make as "-e" argument. |
| 191 | + |
| 192 | +Passing environment variables using docker-compose (compose.yaml mechanism): |
| 193 | + |
| 194 | +```bash |
| 195 | +BRUTEFORCE=true LOG_LEVEL=debug make compose/test |
133 | 196 | ```
|
134 | 197 |
|
135 |
| -### Build and run a development image |
| 198 | +## Development workflow using Docker / docker-compose |
136 | 199 |
|
137 | 200 | Running container with development target.
|
138 |
| -Designed to develop on top of this image. All source application is mounted as |
139 |
| - a volume in **/app** directory. |
140 |
| -Dependencies should be installed to run (not present in this target) so, you |
141 |
| - must install dependencies before run (or after a dependency add/change). |
| 201 | +Designed for development workflow on top of this image. |
| 202 | +All source application is mounted as a volume in **/app** directory. |
| 203 | +Dependencies should be installed to run so, you must |
| 204 | +install dependencies before run (or after a dependency add/change). |
142 | 205 |
|
143 |
| -```text |
144 |
| -# install node_modules dependencies using docker runtime and store them in host directory |
| 206 | +```bash |
| 207 | +# Build development target image |
145 | 208 | docker-compose build --compress algorithm-exercises-js-dev
|
| 209 | +# run ephemeral container to install dependencies using docker runtime |
| 210 | +# and store them in host directory (by bind-mount volume) |
146 | 211 | docker-compose run --rm algorithm-exercises-js-dev npm install --verbose
|
| 212 | +# Run ephemeral container and override command to run test |
147 | 213 | docker-compose run --rm algorithm-exercises-js-dev npm run test
|
148 | 214 | ```
|
149 | 215 |
|
| 216 | +## Run complete workflow (Docker + make) |
| 217 | + |
| 218 | +Following command simulates a standarized pipeline across environments, |
| 219 | +using docker-compose and make. |
| 220 | + |
| 221 | +```bash |
| 222 | +make compose/build && make compose/lint && make compose/test && make compose/run |
| 223 | +``` |
| 224 | + |
| 225 | +- Build all Docker stages and tag relevant images. |
| 226 | +- Run static analysis (lint) checks |
| 227 | +- Run unit tests |
| 228 | +- Run a "final" production ready image as a final container. |
| 229 | +Final "production" image just shows a minimal "production ready" |
| 230 | +build (with no tests). |
| 231 | + |
150 | 232 | ## About development
|
151 | 233 |
|
152 | 234 | Developed with runtime:
|
153 | 235 |
|
154 | 236 | ```text
|
155 | 237 | node --version
|
156 |
| -v22.1.0 |
| 238 | +v22.2.0 |
| 239 | +``` |
| 240 | + |
| 241 | +## Algorithm excersices sources |
| 242 | + |
| 243 | +- [Leetcode](https://leetcode.com/) online platform for |
| 244 | +coding interview preparation. |
| 245 | +- [HackerRank](https://www.hackerrank.com/) competitive programming challenges |
| 246 | +for both consumers and businesses. |
| 247 | +- [Project Euler](https://projecteuler.net/) a series of computational problems |
| 248 | +intended to be solved with computer programs. |
| 249 | + |
| 250 | +Use these answers to learn some tip and tricks for algorithms tests. |
| 251 | + |
| 252 | +### Disclaimer. Why I publish solutions? |
| 253 | + |
| 254 | +As Project Euler says: |
| 255 | + |
| 256 | +<https://projecteuler.net/about#publish> |
| 257 | + |
| 258 | +```text |
| 259 | +I learned so much solving problem XXX, so is it okay to publish my solution elsewhere? |
| 260 | +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. |
| 261 | +
|
| 262 | +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. |
157 | 263 | ```
|
158 | 264 |
|
| 265 | +If you have better answers or optimal solutions, fork and PR-me |
| 266 | + |
| 267 | +Enjoy 😁 ! |
| 268 | + |
| 269 | +## Status |
| 270 | + |
159 | 271 | ### License
|
160 | 272 |
|
161 | 273 | [](https://app.fossa.com/projects/git%2Bgithub.com%2Fsir-gon%2Fprojecteuler-js?ref=badge_large)
|
|
0 commit comments