Skip to content

Commit 367cf94

Browse files
sir-gonGonzalo Diaz
authored and
Gonzalo Diaz
committed
Initial commit
1 parent ebf6e60 commit 367cf94

File tree

11 files changed

+285
-0
lines changed

11 files changed

+285
-0
lines changed

.github/workflows/go.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.18
20+
21+
- name: Build
22+
run: go build -v ./...
23+
24+
- name: Test
25+
run: go test -v ./...

.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# General
2+
3+
# https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
4+
5+
.DS_Store
6+
.AppleDouble
7+
.LSOverride
8+
9+
# Icon must end with two \r
10+
Icon
11+
12+
# Thumbnails
13+
._*
14+
15+
16+
# https://github.com/github/gitignore/blob/main/Go.gitignore
17+
118
# Binaries for programs and plugins
219
*.exe
320
*.exe~
@@ -13,3 +30,21 @@
1330

1431
# Dependency directories (remove the comment below to include it)
1532
# vendor/
33+
34+
# Go workspace file
35+
go.work
36+
37+
# https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
38+
39+
.vscode/*
40+
!.vscode/settings.json
41+
!.vscode/tasks.json
42+
!.vscode/launch.json
43+
!.vscode/extensions.json
44+
!.vscode/*.code-snippets
45+
46+
# Local History for Visual Studio Code
47+
.history/
48+
49+
# Built Visual Studio Code Extensions
50+
*.vsix

Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM golang:1.18.3-alpine3.16 as base
2+
ENV WORKDIR=/app
3+
WORKDIR ${WORKDIR}
4+
5+
FROM base as development
6+
7+
FROM development as builder
8+
RUN apk add --update --no-cache make
9+
10+
COPY ./src ${WORKDIR}/src
11+
COPY ./go.mod ${WORKDIR}/
12+
COPY ./Makefile ${WORKDIR}/
13+
14+
RUN make dependencies
15+
16+
FROM builder as testing
17+
18+
WORKDIR /app
19+
20+
RUN ls -alh
21+
22+
CMD ["make", "all"]

Makefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
GO=go
2+
GOTEST=$(GO) test
3+
GOCOVER=$(GO) tool cover
4+
PKG_LIST=$(go list ./... | grep -v /vendor/ | tr '\n' ' '| xargs echo -n)
5+
6+
.MAIN: test/coverage
7+
.PHONY: all clean coverage dependencies help list test
8+
9+
help: list
10+
11+
list:
12+
@LC_ALL=C $(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
13+
14+
dependencies:
15+
$(GO) mod download
16+
17+
coverage/c.out: dependencies
18+
$(GOTEST) -v -coverprofile=coverage/c.out ./...
19+
20+
test: coverage/c.out
21+
22+
coverage: coverage/c.out
23+
$(GOCOVER) -func=coverage/c.out
24+
$(GOCOVER) -html=coverage/c.out
25+
26+
clean:
27+
$(GO) clean -testcache
28+
29+
all: test coverage

README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
[![Go CI](https://github.com/sir-gon/projecteuler-go/actions/workflows/go.yml/badge.svg)](https://github.com/sir-gon/projecteuler-go/actions/workflows/go.yml)
2+
3+
# What is this?
4+
5+
[Project Euler](https://projecteuler.net/) provide some algorithms and mathematical problems to solve to be used as experience tests.
6+
7+
Use this answers to learn some tip and tricks for algorithms tests.
8+
9+
# Using Go native runtime
10+
11+
## Requirements
12+
13+
You must install dependencies:
14+
15+
```
16+
go mod download
17+
```
18+
19+
Or using make
20+
21+
```
22+
make dependencies
23+
```
24+
25+
## Testing silently
26+
27+
Every problem is a function with unit test.
28+
Unit test has test cases and input data to solve the problem.
29+
30+
Run all tests:
31+
32+
```
33+
go test -v -coverprofile=coverage/c.out ./...
34+
go tool -func=coverage/c.out
35+
go tool -html=coverage/c.out
36+
```
37+
38+
Or using make:
39+
40+
```
41+
make test coverage
42+
```
43+
44+
# Running with Docker 🐳
45+
46+
## Build a complete image with and run all tests
47+
Running container with testing (final) target.
48+
49+
Designed to store all application files and dependencies as a complete runnable image.
50+
Coverage results will be stored in host **/coverage** directory (mounted as volume).
51+
52+
```
53+
# Build a complete image
54+
docker-compose build projecteuler-go
55+
docker-compose run --rm projecteuler-go make test coverage
56+
```
57+
58+
59+
## Build and run a development image
60+
61+
Running container with development target.
62+
Designed to develop on top of this image. All source application is mounted as a volume in **/app** directory.
63+
Dependencies should be installed to run (not present in this target) so, you must install dependencies before run (or after a dependency add/change).
64+
65+
```
66+
# install dependencies using docker runtime and store them in host directory
67+
docker-compose build projecteuler-go-dev
68+
docker-compose run --rm projecteuler-go-dev make dependencies
69+
```
70+
71+
# About development
72+
73+
Developed with runtime:
74+
75+
```
76+
go version
77+
go version go1.18.3 darwin/amd64
78+
```
79+
80+
# Why I publish solutions?
81+
82+
As Project Euler says:
83+
84+
https://projecteuler.net/about#publish
85+
86+
87+
```
88+
I learned so much solving problem XXX, so is it okay to publish my solution elsewhere?
89+
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.
90+
91+
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.
92+
```
93+
94+
95+
If you have better answers or optimal solutions, fork and PR-me
96+
97+
Enjoy 😁 !

compose.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
projecteuler-go:
3+
image: projecteuler-go:latest
4+
build:
5+
context: .
6+
target: testing
7+
environment:
8+
_DEBUG: ${_DEBUG}
9+
volumes:
10+
- ./coverage:/app/coverage
11+
profiles: ["testing"]
12+
13+
projecteuler-go-dev:
14+
image: projecteuler-go:dev
15+
build:
16+
context: .
17+
target: development
18+
environment:
19+
_DEBUG: ${_DEBUG}
20+
volumes:
21+
- ./:/app
22+
profiles: ["development"]

coverage/.gitkeep

Whitespace-only changes.

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module gon.cl/projecteuler.net
2+
3+
go 1.18

go.sum

Whitespace-only changes.

src/helpers/minmax.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package projecteuler
2+
3+
func IntMin(a, b int) int {
4+
if a < b {
5+
return a
6+
}
7+
return b
8+
}

src/helpers/minmax_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package projecteuler
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestIntMinBasic(t *testing.T) {
9+
ans := IntMin(2, -2)
10+
if ans != -2 {
11+
12+
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
13+
}
14+
}
15+
16+
func TestIntMinTableDriven(t *testing.T) {
17+
var tests = []struct {
18+
a, b int
19+
want int
20+
}{
21+
{0, 1, 0},
22+
{1, 0, 0},
23+
{2, -2, -2},
24+
{0, -1, -1},
25+
{-1, 0, -1},
26+
}
27+
28+
for _, tt := range tests {
29+
30+
testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
31+
t.Run(testname, func(t *testing.T) {
32+
ans := IntMin(tt.a, tt.b)
33+
if ans != tt.want {
34+
t.Errorf("got %d, want %d", ans, tt.want)
35+
}
36+
})
37+
}
38+
}
39+
40+
func BenchmarkIntMin(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
IntMin(1, 2)
43+
}
44+
}

0 commit comments

Comments
 (0)