Skip to content

Commit 4ee4e70

Browse files
authored
Merge branch 'master' into master
2 parents e9a2d3d + f417633 commit 4ee4e70

File tree

14 files changed

+462
-28
lines changed

14 files changed

+462
-28
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,31 @@ on: [push, pull_request]
44
jobs:
55
golangci-lint:
66
runs-on: ubuntu-latest
7+
strategy:
8+
fail-fast: false
79
steps:
8-
- uses: actions/setup-go@v1
9-
with:
10-
go-version: 1.13
11-
- uses: actions/checkout@v1
12-
- run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.21.0
13-
- run: |
14-
go version ; ./bin/golangci-lint --version
15-
./bin/golangci-lint run --no-config || true
16-
- run: ./bin/golangci-lint run --no-config ciphers strings
17-
- run: ./bin/golangci-lint run --no-config data-structures || true
18-
- run: ./bin/golangci-lint run --no-config dynamic-programming || true
19-
- run: ./bin/golangci-lint run --no-config other || true
20-
- run: ./bin/golangci-lint run --no-config searches || true
21-
- run: ./bin/golangci-lint run --no-config sorts
10+
- uses: actions/checkout@v2
11+
- name: golangci-lint
12+
uses: golangci/golangci-lint-action@v1
13+
with:
14+
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
15+
version: v1.27
16+
working-directory: sorts
17+
args: --issues-exit-code=0 # Hopefully someday we can remove this...
18+
# Only show new issues if this is a pull request...
19+
# only-new-issues: true
20+
- run: echo "::set-env name=PATH::/home/runner/golangci-lint-1.27.0-linux-amd64:$PATH"
21+
- run: go version ; golangci-lint --version # Fix the following and remove || true
22+
- run: golangci-lint run --no-config ciphers || true
23+
- run: golangci-lint run --no-config data-structures/binary-tree || true
24+
- run: golangci-lint run --no-config data-structures/dynamic-array || true
25+
- run: golangci-lint run --no-config data-structures/hash-map || true
26+
- run: golangci-lint run --no-config data-structures/linked-list || true
27+
- run: golangci-lint run --no-config data-structures/trie || true
28+
- run: golangci-lint run --no-config dynamic-programming || true
29+
- run: golangci-lint run --no-config other || true
30+
- run: golangci-lint run --no-config searches || true
31+
- run: golangci-lint run --no-config sorts
32+
- run: golangci-lint run --no-config strings
33+
- run: golangci-lint run --no-config "strings/multiple string matching" || true
34+
# - run: golangci-lint run --no-config "strings/single string matching" || true

DIRECTORY.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## Ciphers
33
* [Caesarcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/CaesarCipher.go)
44
* [Diffiehellmankeyexchange](https://github.com/TheAlgorithms/Go/blob/master/ciphers/diffieHellmanKeyExchange.go)
5+
* [Rsacipher(Big)](https://github.com/TheAlgorithms/Go/blob/master/ciphers/RSAcipher(Big).go)
56
* [Rsacipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/RSAcipher.go)
67
* [Xorcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/xorCipher.go)
78

@@ -22,13 +23,21 @@
2223
* [Trie Test](https://github.com/TheAlgorithms/Go/blob/master/data-structures/trie/trie_test.go)
2324

2425
## Dynamic-Programming
26+
* [Binomial-Coeffecient](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/binomial-coeffecient.go)
27+
* [Knapsack](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/knapsack.go)
2528
* [Longest-Palindromic-Subsequence](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/longest-palindromic-subsequence.go)
2629
* [Longestcommonsubsequence](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/longestCommonSubsequence.go)
2730
* [Matrix-Multiplication](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/matrix-multiplication.go)
2831
* [Rod-Cutting](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/rod-cutting.go)
2932

33+
## Graphs
34+
* [Floyd Warshall](https://github.com/TheAlgorithms/Go/blob/master/graphs/floyd_warshall.go)
35+
3036
## Other
3137
* [Maxsubarraysum](https://github.com/TheAlgorithms/Go/blob/master/other/maxSubarraySum.go)
38+
* Monte Carlo Pi
39+
* [Monte Carlo Pi](https://github.com/TheAlgorithms/Go/blob/master/other/monte_carlo_pi/monte_carlo_pi.go)
40+
* [Monte Carlo Pi Test](https://github.com/TheAlgorithms/Go/blob/master/other/monte_carlo_pi/monte_carlo_pi_test.go)
3241
* [Nestedbrackets](https://github.com/TheAlgorithms/Go/blob/master/other/NestedBrackets.go)
3342
* [Passwordgenerator](https://github.com/TheAlgorithms/Go/blob/master/other/PasswordGenerator.go)
3443
* [Prime Numbers](https://github.com/TheAlgorithms/Go/blob/master/other/prime_numbers.go)
@@ -37,10 +46,14 @@
3746

3847
## Searches
3948
* [Binary Search](https://github.com/TheAlgorithms/Go/blob/master/searches/binary_search.go)
49+
* [Breadth First Search](https://github.com/TheAlgorithms/Go/blob/master/searches/breadth_first_search.go)
50+
* [Depth First Search](https://github.com/TheAlgorithms/Go/blob/master/searches/depth_first_search.go)
4051
* [Linear Search](https://github.com/TheAlgorithms/Go/blob/master/searches/linear_search.go)
52+
* [Search Test](https://github.com/TheAlgorithms/Go/blob/master/searches/search_test.go)
4153

4254
## Sorts
4355
* [Bubble Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/bubble_sort.go)
56+
* [Counting Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/counting_sort.go)
4457
* [Heap Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/heap_sort.go)
4558
* [Insertion Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/insertion_sort.go)
4659
* [Merge Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/merge_sort.go)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# The Algorithms - Go
22

3+
![golangci-lint](https://github.com/TheAlgorithms/Go/workflows/golangci-lint/badge.svg)
4+
![Go](https://github.com/TheAlgorithms/Go/workflows/Go/badge.svg)
5+
![update_directory_md](https://github.com/TheAlgorithms/Go/workflows/update_directory_md/badge.svg)
6+
37
### Algorithms implemented in Go (for education)
48

59
These algorithms are for demonstration purposes only. There are many sort implementations in the Go standard library that may have better performance.

data-structures/binary-tree/binary-tree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ func preorder(n *node) {
4242
return
4343
}
4444
fmt.Print(n.val, " ")
45-
inorder(n.left)
46-
inorder(n.right)
45+
preorder(n.left)
46+
preorder(n.right)
4747
}
4848

4949
func postorder(n *node) {
5050
if n == nil {
5151
return
5252
}
53-
inorder(n.left)
54-
inorder(n.right)
53+
postorder(n.left)
54+
postorder(n.right)
5555
fmt.Print(n.val, " ")
5656
}
5757

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
myArrayOfK := [4]int{5, 6, 7, 8}
10+
var x int
11+
12+
fmt.Println("\nBinomial Coefficient Using Dynamic Programming:", bin2(50, 5))
13+
for _, element := range myArrayOfK {
14+
start := time.Now()
15+
x = bin2(50, element)
16+
elapsed := time.Since(start)
17+
fmt.Println("bin2 (50,", element, ") = ", x, " took ", elapsed)
18+
19+
}
20+
21+
}
22+
23+
func bin2(n int, k int) int {
24+
var i, j int
25+
B := make([][]int, (n + 1))
26+
for i := range B {
27+
B[i] = make([]int, k+1)
28+
}
29+
30+
for i = 0; i <= n; i++ {
31+
for j = 0; j <= min(i, k); j++ {
32+
if j == 0 || j == i {
33+
B[i][j] = 1
34+
} else {
35+
B[i][j] = B[i-1][j-1] + B[i-1][j]
36+
}
37+
}
38+
}
39+
return B[n][k]
40+
}
41+
42+
func min(a, b int) int {
43+
if a < b {
44+
return a
45+
}
46+
return b
47+
}

dynamic-programming/knapsack.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Knapsack Problem
2+
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
3+
package Knapsack
4+
5+
// package main
6+
7+
import (
8+
"math"
9+
)
10+
11+
func max(a, b int) int {
12+
return int(math.Max(float64(a), float64(b)))
13+
}
14+
15+
// solve knapsack problem
16+
// return maxProfit
17+
func solve(maxWeight int, weights, values []int) int {
18+
n := len(weights)
19+
m := maxWeight
20+
// create dp data structure
21+
dp := make([][]int, n+1)
22+
for i := range dp {
23+
dp[i] = make([]int, m+1)
24+
}
25+
for i := 0; i < len(weights); i++ {
26+
for j := 0; j <= maxWeight; j++ {
27+
if weights[i] > j {
28+
dp[i+1][j] = dp[i][j]
29+
} else {
30+
dp[i+1][j] = max(dp[i][j-weights[i]]+values[i], dp[i][j])
31+
}
32+
}
33+
}
34+
return dp[n][m]
35+
}
36+
37+
/*
38+
func main() {
39+
maxWeight := 50
40+
values := []int{
41+
60, 100, 120,
42+
}
43+
weights := []int{
44+
10, 20, 30,
45+
}
46+
maxProfit := solve(maxWeight, weights, values)
47+
fmt.Println(maxProfit)
48+
}
49+
*/

graphs/floyd_warshall.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Floyd-Warshall algorithm
2+
// https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
3+
4+
package graph
5+
6+
import (
7+
"math"
8+
)
9+
10+
// Defining matrix to use 2d array easier
11+
type Matrix [][]float64
12+
13+
// Defining maximum value. If two vertices share this value, it means they are not connected
14+
var maxValue = math.Inf(1)
15+
16+
// Returns all pair's shortest path using Floyd Warshall algorithm
17+
func FloydWarshall(graph Matrix) Matrix {
18+
// If graph is empty or width != height, returns nil
19+
if len(graph) == 0 || len(graph) != len(graph[0]) {
20+
return nil
21+
}
22+
23+
numVertecies := len(graph)
24+
25+
// Initializing result matrix and filling it up with same values as given graph
26+
result := make(Matrix, numVertecies)
27+
28+
for i := 0; i < numVertecies; i++ {
29+
result[i] = make([]float64, numVertecies)
30+
for j := 0; j < numVertecies; j++ {
31+
result[i][j] = graph[i][j]
32+
}
33+
}
34+
35+
// Running over the result matrix and following the algorithm
36+
for k := 0; k < numVertecies; k++ {
37+
for i := 0; i < numVertecies; i++ {
38+
for j := 0; j < numVertecies; j++ {
39+
// If there is a less costly path from i to j node, remembering it
40+
if result[i][j] > result[i][k]+result[k][j] {
41+
result[i][j] = result[i][k] + result[k][j]
42+
}
43+
}
44+
}
45+
}
46+
47+
return result
48+
}
49+
50+
// func main() {
51+
// var graph Matrix
52+
// graph = Matrix{{0, maxValue, -2, maxValue},
53+
// {4, 0, 3, maxValue},
54+
// {maxValue, maxValue, 0, 2},
55+
// {maxValue, -1, maxValue, 0}}
56+
57+
// result := FloydWarshall(graph)
58+
59+
// // Print result
60+
// for i := 0; i < len(result); i++ {
61+
// fmt.Printf("%4g\n", result[i])
62+
// }
63+
// }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Monte Carlo Pi
2+
// [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method) to calculate Pi
3+
//
4+
// The basic idea of Monte Carlo is:
5+
// 1. Define a domain of possible inputs
6+
// 2. Generate inputs randomly from a probability distribution over the domain
7+
// 3. Perform a deterministic computation on the inputs
8+
// 4. Aggregate the results
9+
//
10+
// Thus the algorithm to calculate Pi is:
11+
//
12+
// 1. Given a circle inscribed in a square with side length 1.
13+
// 2. randomly draw n points (x, y) | x,y in [0.0,1.0)
14+
// 3. Count the amount of points inside the circle
15+
// 4. pi = 4 * (points inside the circle) / n
16+
//
17+
// A point (x, y) is inside the circle if:
18+
//
19+
// x*x + y*y <= 1
20+
//
21+
22+
package main
23+
24+
import (
25+
"math"
26+
"math/rand"
27+
)
28+
29+
// MonteCarloPi calculates Pi using the Monte Carlo method
30+
func MonteCarloPi(iterations uint) float64 {
31+
var inside, total uint
32+
33+
for total = uint(0); total < iterations; total++ {
34+
if in := math.Hypot(rand.Float64(), rand.Float64()) <= 1; in {
35+
inside++
36+
}
37+
}
38+
39+
return float64(4) * (float64(inside) / float64(total))
40+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestMonteCarloPi(t *testing.T) {
9+
delta := 0.0001
10+
11+
pi := MonteCarloPi(100000000)
12+
13+
if math.Abs(pi-math.Pi) > delta {
14+
t.Errorf("Given: %.4f, expected: %.4f", pi, math.Pi)
15+
}
16+
}

0 commit comments

Comments
 (0)