Skip to content

Commit 5265f02

Browse files
authored
Merge pull request #187 from sir-gon/develop
Develop
2 parents 8219b5a + 32d9ffb commit 5265f02

File tree

3 files changed

+204
-61
lines changed

3 files changed

+204
-61
lines changed

.github/workflows/gitleaks.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ on: # yamllint disable-line rule:truthy
77
push:
88
workflow_dispatch:
99
schedule:
10+
# ┌───────────── minute (0 - 59)
11+
# │ ┌───────────── hour (0 - 23)
12+
# │ │ ┌───────────── day of the month (1 - 31)
13+
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
14+
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
15+
# │ │ │ │ │
16+
# │ │ │ │ │
17+
# │ │ │ │ │
18+
# * * * * *
1019
- cron: "0 4 * * *" # run once a day at 4 AM
1120
jobs:
1221
scan:

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ gradle-app.setting
2020
.project
2121
# JDT-specific (Eclipse Java Development Tools)
2222
.classpath
23+
24+
# Third party libraries
25+
**/lib/*.jar
26+
27+
# Static Analysis Results Interchange Format
28+
*.sarif
29+
.dccache

README.md

Lines changed: 188 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,134 +3,231 @@
33
[![Java with Gradle](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/java-gradle.yml/badge.svg)](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/java-gradle.yml)
44
[![codecov](https://codecov.io/gh/sir-gon/algorithm-exercises-java/graph/badge.svg?token=7QAS85D3HB)](https://codecov.io/gh/sir-gon/algorithm-exercises-java)
55
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsir-gon%2Fprojecteuler-java.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsir-gon%2Fprojecteuler-java?ref=badge_shield)
6-
[![Markdown Lint](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/markdown-lint.yml/badge.svg)](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/markdown-lint.yml)
76

87
![GitHub](https://img.shields.io/github/license/sir-gon/algorithm-exercises-java)
98
![GitHub language count](https://img.shields.io/github/languages/count/sir-gon/algorithm-exercises-java)
109
![GitHub top language](https://img.shields.io/github/languages/top/sir-gon/algorithm-exercises-java)
1110

11+
[![Markdown Lint](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/markdown-lint.yml/badge.svg)](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/markdown-lint.yml)
12+
[![YAML lint](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/yamllint.yml/badge.svg)](https://github.com/sir-gon/algorithm-exercises-java/actions/workflows/yamllint.yml)
13+
1214
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-java&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-java)
1315
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-java&metric=coverage)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-java)
1416
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-java&metric=bugs)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-java)
1517
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-java&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-java)
1618
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=sir-gon_algorithm-exercises-java&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=sir-gon_algorithm-exercises-java)
1719

20+
## TL;DR
21+
22+
[Install and run](#install-and-run)
23+
1824
## What is this?
1925

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

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

25-
## Why I publish solutions?
37+
## Objetives
2638

27-
As Project Euler says:
39+
### Functional
2840

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

31-
```text
32-
I learned so much solving problem XXX, so is it okay to publish my solution elsewhere?
33-
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.
45+
- The solutions must be written on "vanilla code", that is,
46+
avoiding as much as possible the use of external libraries (in runtime).
3447

35-
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.
36-
```
48+
- Adoption of methodology and good practices.
49+
Each exercise is implemented as a unit test set,
50+
using TDD (Test-driven Development) and Clean Code ideas.
3751

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

40-
Enjoy 😁 !
54+
Foundation of a project that supports:
55+
56+
- Explicit **typing** when the language supports it, even when it is not mandatory.
57+
- Static Code Analysis (**Lint**) of code, scripts and documentation.
58+
- Uniform **Code Styling**.
59+
- **Unit Test** framework.
60+
- **Coverge** collection. High coverage percentage. Equal or close to 100%.
61+
- **Pipeline** (Github Actions). Each command must take care of its
62+
return status code.
63+
- **Docker**-based workflow to replicate behavior in any environment.
64+
- Other tools to support the reinforcement of software development **good practices**.
65+
66+
## Install and Run
67+
68+
You can run tests in the following ways:
69+
70+
- [Install and run directly](#install-and-run-directly) require runtime tools
71+
installed in your SO.
72+
- [Install and run with make](#install-and-run-using-make) require runtime tools
73+
and "make" installed in your SO.
74+
- [Install and in Docker](#install-and-running-with-docker-) require Docker and
75+
docker-compose installed.
76+
- (⭐️)
77+
[Install and in Docker with make](#install-and-running-with-docker--using-make)
78+
require docker-compose and make installed.
79+
80+
⭐️: Prefered way.
4181

42-
## Using Java runtime
82+
### Install and Run directly
4383

44-
### Testing silently
84+
Running over a JVM with gradle. You must install dependencies:
85+
86+
```bash
87+
gradle --console=verbose dependencies
88+
```
4589

4690
Every problem is a function with unit test.
91+
4792
Unit test has test cases and input data to solve the problem.
4893

49-
Run all tests:
94+
Run all tests (skips static analysis, and "clean" test cache before running):
5095

51-
```text
52-
gradle clean test
96+
```bash
97+
gradle --console=verbose clean test -x checkstyleMain checkstyleTest
5398
```
5499

55-
### Testing with full logs
100+
#### Test run with alternative behaviors
56101

57-
Run all tests with debug outputs:
102+
You can change test running behaviour using some environment variables as follows:
58103

59-
```text
60-
LOG_LEVEL=debug gradle clean test
61-
```
104+
| Variable | Values | Default |
105+
| ------ | ------ | ------ |
106+
| LOG_LEVEL | `debug`, `warning`, `error`, `info` | `info` |
107+
| BRUTEFORCE | `true`, `false`| `false` |
62108

63-
Use one of following values: debug, warning, error, info.
109+
- `LOG_LEVEL`: change verbosity level in outputs.
110+
- `BRUTEFORCE`: enable or disable running large tests.
111+
(long time, large amount of data, high memory consumition).
64112

65-
### Testing using make
113+
#### Examples running tests with alternative behaviors
66114

67-
```text
68-
make test
115+
Run tests with debug outputs:
116+
117+
```bash
118+
LOG_LEVEL=debug gradle --console=verbose clean test -x checkstyleMain checkstyleTest
69119
```
70120

71-
### Enable all large BRUTEFORCE tests
121+
Run brute-force tests with debug outputs:
72122

73-
Direct in host using a make:
123+
```bash
124+
BRUTEFORCE=true LOG_LEVEL=debug gradle --console=verbose clean test -x checkstyleMain checkstyleTest
125+
```
74126

75-
```text
76-
make test -e BRUTEFORCE=true
127+
### Install and Run using make
128+
129+
`make` tool is used to standardizes the commands for the same tasks
130+
across each sibling repository.
131+
132+
Run tests (libraries are installed as dependency task in make):
133+
134+
```bash
135+
make test
77136
```
78137

79-
### Enable all DEBUG outputs
138+
Run tests with debug outputs:
80139

81-
```text
140+
```bash
82141
make test -e LOG_LEVEL=debug
83142
```
84143

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

87-
```text
88-
make test -e LOG_LEVEL=debug -e BRUTEFORCE=true
146+
```bash
147+
make test -e BRUTEFORCE=true -e LOG_LEVEL=debug
89148
```
90149

91-
## Running with Docker 🐳
150+
Alternative way, use environment variables as prefix:
92151

93-
### Build a complete image with and run all tests
152+
```bash
153+
BRUTEFORCE=true LOG_LEVEL=debug make test
154+
```
94155

95-
Running container with testing (final) target.
156+
### Install and Running with Docker 🐳
96157

97-
Designed to store all application files and dependencies as a complete runnable image.
98-
Coverage results will be stored in host **/coverage** directory (mounted as volume).
158+
Build an image of the test stage.
159+
Then creates and ephemeral container an run tests.
99160

100-
```text
101-
# Build a complete image
102-
docker-compose build algorithm-exercises-java
103-
docker-compose run --rm algorithm-exercises-java gradle clean test
161+
BRUTEFORCE and LOG_LEVEL environment variables are passing from current
162+
environment using docker-compose.
163+
164+
```bash
165+
docker-compose --profile testing run --rm algorithm-exercises-java-test
104166
```
105167

106-
### Enable BRUTEFORCE tests with full DEBUG output
168+
To change behavior using environment variables, you can pass to containers
169+
in the following ways:
107170

108-
With docker-compose:
171+
From host using docker-compose (compose.yaml) mechanism:
109172

110-
```text
111-
docker-compose --profile testing run --rm algorithm-exercises-java make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
173+
```bash
174+
BRUTEFORCE=true LOG_LEVEL=debug docker-compose --profile testing run --rm algorithm-exercises-java-test
112175
```
113176

114-
Using make:
177+
Overriding docker CMD, as parameter of make "-e":
115178

116-
```text
117-
make docker/compose-run -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
179+
```bash
180+
docker-compose --profile testing run --rm algorithm-exercises-java-test make test -e LOG_LEVEL=DEBUG -e BRUTEFORCE=true
181+
```
182+
183+
### Install and Running with Docker 🐳 using make
184+
185+
```bash
186+
make compose/build
187+
make compose/test
188+
```
189+
190+
To pass environment variables you can use docker-compose
191+
or overriding CMD and passing to make as "-e" argument.
192+
193+
Passing environment variables using docker-compose (compose.yaml mechanism):
194+
195+
```bash
196+
BRUTEFORCE=true LOG_LEVEL=debug make compose/test
118197
```
119198

120-
### Build and run a development image
199+
## Development workflow using Docker / docker-compose
121200

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

128-
```text
129-
# install node_modules dependencies using docker runtime and store them in host directory
207+
```bash
208+
# Build development target image
130209
docker-compose build --compress algorithm-exercises-java-dev
131-
docker-compose run --rm algorithm-exercises-java-dev gradle clean test
210+
211+
# Run ephemeral container and override command to run test
212+
docker-compose run --rm algorithm-exercises-java-dev gradle --console=verbose clean test -x checkstyleMain checkstyleTest
132213
```
133214

215+
## Run complete workflow (Docker + make)
216+
217+
Following command simulates a standarized pipeline across environments,
218+
using docker-compose and make.
219+
220+
```bash
221+
make compose/build && make compose/lint && make compose/test && make compose/run
222+
```
223+
224+
- Build all Docker stages and tag relevant images.
225+
- Run static analysis (lint) checks
226+
- Run unit tests
227+
- Run a "final" production ready image as a final container.
228+
Final "production" image just shows a minimal "production ready"
229+
build (with no tests).
230+
134231
## About development
135232

136233
Developed with runtime:
@@ -142,6 +239,36 @@ Java(TM) SE Runtime Environment (build 20.0.2+9-78)
142239
Java HotSpot(TM) 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)
143240
```
144241

242+
## Algorithm excersices sources
243+
244+
- [Leetcode](https://leetcode.com/) online platform for
245+
coding interview preparation.
246+
- [HackerRank](https://www.hackerrank.com/) competitive programming challenges
247+
for both consumers and businesses.
248+
- [Project Euler](https://projecteuler.net/) a series of computational problems
249+
intended to be solved with computer programs.
250+
251+
Use these answers to learn some tip and tricks for algorithms tests.
252+
253+
### Disclaimer. Why I publish solutions?
254+
255+
As Project Euler says:
256+
257+
<https://projecteuler.net/about#publish>
258+
259+
```text
260+
I learned so much solving problem XXX, so is it okay to publish my solution elsewhere?
261+
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.
262+
263+
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.
264+
```
265+
266+
If you have better answers or optimal solutions, fork and PR-me
267+
268+
Enjoy 😁 !
269+
270+
## Status
271+
145272
### License
146273

147274
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsir-gon%2Fprojecteuler-java.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsir-gon%2Fprojecteuler-java?ref=badge_large)

0 commit comments

Comments
 (0)