Skip to content

Commit 198afff

Browse files
committed
Merge branch 'master' of github.com:TheAlgorithms/Go
2 parents 9e6a93c + 3ed92dc commit 198afff

File tree

31 files changed

+914
-139
lines changed

31 files changed

+914
-139
lines changed

.github/CODEOWNERS

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This is a comment.
2+
# Each line is a file pattern followed by one or more owners.
3+
4+
# More details are here: https://help.github.com/articles/about-codeowners/
5+
6+
# The '*' pattern is global owners.
7+
8+
# Order is important. The last matching pattern has the most precedence.
9+
10+
/.* @cclauss
11+
/ciphers/ @vedantmamgain
12+
/data-structures/ @Mystigan @vedantmamgain
13+
/dynamic-programming/ @vedantmamgain
14+
/graphs/ @vedantmamgain
15+
/math/ @vedantmamgain @Mystigan
16+
/other/ @vedantmamgain
17+
/searches/ @Mystigan
18+
/sorts/ @Mystigan
19+
/strings/ @vedantmamgain

.github/workflows/golangci-lint.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@ jobs:
88
fail-fast: false
99
steps:
1010
- uses: actions/checkout@v2
11-
- name: golangci-lint
12-
uses: golangci/golangci-lint-action@v1
11+
- uses: actions/setup-go@v2
12+
- uses: golangci/golangci-lint-action@v2
1313
with:
1414
# 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
15+
version: v1.31
1716
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
17+
working-directory: sorts # Hopefully someday we can remove this... and enable only-new-issues
18+
# only-new-issues: true # Let's focus on this PR first
19+
- run: echo "/home/runner/golangci-lint-1.31.0-linux-amd64" >> $GITHUB_PATH
20+
- run: go version ; golangci-lint --version # Fix the following and remove || true
21+
- run: golangci-lint run --no-config ciphers
2322
- run: golangci-lint run --no-config data-structures/binary-tree || true
2423
- run: golangci-lint run --no-config data-structures/dynamic-array || true
25-
- run: golangci-lint run --no-config data-structures/hash-map || true
24+
- run: golangci-lint run --no-config data-structures/hash-map
2625
- run: golangci-lint run --no-config data-structures/linked-list || true
2726
- run: golangci-lint run --no-config data-structures/trie || true
2827
- run: golangci-lint run --no-config dynamic-programming || true
2928
- run: golangci-lint run --no-config other || true
3029
- run: golangci-lint run --no-config searches || true
3130
- run: golangci-lint run --no-config sorts
3231
# - 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
32+
- run: golangci-lint run --no-config "strings/multiple-string-matching" || true
33+
# - run: golangci-lint run --no-config "strings/single string matching" || true
3534
- run: golangci-lint run --no-config strings/levenshteindistance
3635
- run: golangci-lint run --no-config strings/naivesearch
3736
- run: golangci-lint run --no-config math/gcd

DIRECTORY.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11

22
## Ciphers
3-
* [Caesarcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/CaesarCipher.go)
3+
* Caesar
4+
* [Caesar Test](https://github.com/TheAlgorithms/Go/blob/master/ciphers/caesar/caesar_test.go)
5+
* [Caesarcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/caesar/CaesarCipher.go)
46
* [Diffiehellmankeyexchange](https://github.com/TheAlgorithms/Go/blob/master/ciphers/diffieHellmanKeyExchange.go)
5-
* [Rot13](https://github.com/TheAlgorithms/Go/blob/master/ciphers/Rot13.go)
6-
* [Rsacipher(Big)](https://github.com/TheAlgorithms/Go/blob/master/ciphers/RSAcipher(Big).go)
7-
* [Rsacipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/RSAcipher.go)
8-
* [Xorcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/xorCipher.go)
7+
* Rot13
8+
* [Rot13](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rot13/rot13.go)
9+
* [Rot13 Test](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rot13/rot13_test.go)
10+
* Rsa
11+
* [Rsa Cipher Test](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rsa/rsa_cipher_test.go)
12+
* [Rsacipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rsa/RSAcipher.go)
13+
* Rsabig
14+
* [Rsa Cipher Big Test](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rsaBig/rsa_cipher_big_test.go)
15+
* [Rsacipher(Big)](https://github.com/TheAlgorithms/Go/blob/master/ciphers/rsaBig/RSAcipher(Big).go)
16+
* Xor
17+
* [Xor Cipher Test](https://github.com/TheAlgorithms/Go/blob/master/ciphers/xor/xor_cipher_test.go)
18+
* [Xorcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/xor/xorCipher.go)
919

1020
## Data-Structures
1121
* Binary-Tree
@@ -38,12 +48,20 @@
3848
* [Floyd Warshall](https://github.com/TheAlgorithms/Go/blob/master/graphs/floyd_warshall.go)
3949

4050
## Math
41-
* [Fastexponent](https://github.com/TheAlgorithms/Go/blob/master/math/fastExponent.go)
42-
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Go/blob/master/math/greatest_common_divisor.go)
43-
* Prime Check
44-
* [Prime Check](https://github.com/TheAlgorithms/Go/blob/master/math/prime_check/prime_check.go)
45-
* [Prime Check Test](https://github.com/TheAlgorithms/Go/blob/master/math/prime_check/prime_check_test.go)
46-
* [Sieve](https://github.com/TheAlgorithms/Go/blob/master/math/Sieve.go)
51+
* Gcd
52+
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Go/blob/master/math/gcd/greatest_common_divisor.go)
53+
* [Greatest Common Divisor Test](https://github.com/TheAlgorithms/Go/blob/master/math/gcd/greatest_common_divisor_test.go)
54+
* Power
55+
* [Fastexponent](https://github.com/TheAlgorithms/Go/blob/master/math/power/fastExponent.go)
56+
* [Fastexponent Test](https://github.com/TheAlgorithms/Go/blob/master/math/power/fastExponent_test.go)
57+
* Primecheck
58+
* [Prime Check](https://github.com/TheAlgorithms/Go/blob/master/math/primecheck/prime_check.go)
59+
* [Prime Check Test](https://github.com/TheAlgorithms/Go/blob/master/math/primecheck/prime_check_test.go)
60+
* Pythagoras
61+
* [Pythagoras](https://github.com/TheAlgorithms/Go/blob/master/math/pythagoras/pythagoras.go)
62+
* [Pythagoras Test](https://github.com/TheAlgorithms/Go/blob/master/math/pythagoras/pythagoras_test.go)
63+
* Sieve
64+
* [Sieve](https://github.com/TheAlgorithms/Go/blob/master/math/sieve/Sieve.go)
4765

4866
## Other
4967
* [Maxsubarraysum](https://github.com/TheAlgorithms/Go/blob/master/other/maxSubarraySum.go)
@@ -80,14 +98,22 @@
8098
* Levenshteindistance
8199
* [Levenshteindistance](https://github.com/TheAlgorithms/Go/blob/master/strings/levenshteindistance/levenshteinDistance.go)
82100
* [Levenshteindistance Test](https://github.com/TheAlgorithms/Go/blob/master/strings/levenshteindistance/levenshteinDistance_test.go)
83-
* Multiple String Matching
84-
* [Ac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/ac.go)
85-
* [Adac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/adac.go)
86-
* [Sbom](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/sbom.go)
101+
* Multiple-String-Matching
102+
* Advanced-Aho-Corasick
103+
* [Adac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/advanced-aho-corasick/adac.go)
104+
* [Adac Test](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/advanced-aho-corasick/adac_test.go)
105+
* Aho-Corasick
106+
* [Ac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/aho-corasick/ac.go)
107+
* [Ac Test](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/aho-corasick/ac_test.go)
108+
* Sbom
109+
* [Sbom](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/sbom/sbom.go)
110+
* [Sbom Test](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple-string-matching/sbom/sbom_test.go)
87111
* Naivesearch
88112
* [Naivestringsearch](https://github.com/TheAlgorithms/Go/blob/master/strings/naivesearch/naiveStringSearch.go)
89113
* [Naivestringsearch Test](https://github.com/TheAlgorithms/Go/blob/master/strings/naivesearch/naiveStringSearch_test.go)
90-
* Single String Matching
91-
* [Bom](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/bom.go)
92-
* [Horspool](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/horspool.go)
93-
* [Kmp](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/kmp.go)
114+
* Single-String-Matching
115+
* [Bom](https://github.com/TheAlgorithms/Go/blob/master/strings/single-string-matching/bom.go)
116+
* [Horspool](https://github.com/TheAlgorithms/Go/blob/master/strings/single-string-matching/horspool.go)
117+
* Kmp
118+
* [Kmp](https://github.com/TheAlgorithms/Go/blob/master/strings/single-string-matching/kmp/kmp.go)
119+
* [Kmp Test](https://github.com/TheAlgorithms/Go/blob/master/strings/single-string-matching/kmp/kmp_test.go)
File renamed without changes.

ciphers/caesar/caesar_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var caesarTestData = []struct {
8+
description string
9+
input string
10+
key int
11+
expected string
12+
}{
13+
{
14+
"Basic caesar encryption with letter 'a'",
15+
"a",
16+
3,
17+
"d",
18+
},
19+
{
20+
"Basic caesar encryption wrap around alphabet on letter 'z'",
21+
"z",
22+
3,
23+
"c",
24+
},
25+
{
26+
"Encrypt a simple string with ceasar encryiption",
27+
"hello",
28+
3,
29+
"khoor",
30+
},
31+
{
32+
"Encrypt a simple string with key 13",
33+
"hello",
34+
13,
35+
"uryyb",
36+
},
37+
{
38+
"Encrypt a simple string with key -13",
39+
"hello",
40+
-13,
41+
"uryyb",
42+
},
43+
{
44+
"With key of 26 output should be the same as the input",
45+
"no change",
46+
26,
47+
"no change",
48+
},
49+
{
50+
"Encrpyt sentence with key 10",
51+
"the quick brown fox jumps over the lazy dog.",
52+
10,
53+
"dro aesmu lbygx pyh tewzc yfob dro vkji nyq.",
54+
},
55+
}
56+
57+
func TestCeasarCipher(t *testing.T) {
58+
for _, test := range caesarTestData {
59+
t.Run(test.description, func(t *testing.T) {
60+
actual := caesarCipher(test.input, test.key)
61+
if actual != test.expected {
62+
t.Logf("FAIL: %s", test.description)
63+
t.Fatalf("With input string '%s' and key '%d' was expecting '%s' but actual was '%s'",
64+
test.input, test.key, test.expected, actual)
65+
}
66+
})
67+
}
68+
}

ciphers/Rot13.go renamed to ciphers/rot13/rot13.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package main
1+
package rot13
22

33
import (
44
"bytes"
5-
"fmt"
65
"strings"
76
)
87

@@ -24,11 +23,4 @@ func rot13(input string) string {
2423
outputBuffer.WriteString(string(newByte))
2524
}
2625
return outputBuffer.String()
27-
28-
}
29-
func main() {
30-
cleartext := "We'll just make him an offer he can't refuse... tell me you get the pop culture reference"
31-
encrypted := rot13(cleartext)
32-
decrypted := rot13(encrypted)
33-
fmt.Printf("Cleartext: %v\nencrypted: %v\ndecrypted: %v\n", cleartext, encrypted, decrypted)
3426
}

ciphers/rot13/rot13_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package rot13
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var rot13TestData = []struct {
8+
description string
9+
input string
10+
expected string
11+
}{
12+
{
13+
"Basic rotation with letter 'a' gives 'n",
14+
"a",
15+
"n",
16+
},
17+
{
18+
"Rotation with wrapping around alphabet on letter 'z' gives 'm'",
19+
"z",
20+
"m",
21+
},
22+
{
23+
"Rotation on 'hello world'",
24+
"hello world",
25+
"uryyb jbeyq",
26+
},
27+
{
28+
"Rotation on the rotation of 'hello world' gives 'hello world' back",
29+
"uryyb jbeyq",
30+
"hello world",
31+
},
32+
{
33+
"Full sentence rotation",
34+
"the quick brown fox jumps over the lazy dog.",
35+
"gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
36+
},
37+
{
38+
"Sentence from Rot13.go main function",
39+
"we'll just make him an offer he can't refuse... tell me you get the pop culture reference",
40+
"jr'yy whfg znxr uvz na bssre ur pna'g ershfr... gryy zr lbh trg gur cbc phygher ersrerapr",
41+
},
42+
}
43+
44+
func TestRot13Encrypt(t *testing.T) {
45+
for _, test := range rot13TestData {
46+
t.Run(test.description, func(t *testing.T) {
47+
input := test.input
48+
expected := test.expected
49+
assertRot13Output(t, input, expected)
50+
})
51+
}
52+
}
53+
54+
func TestRot13Decrypt(t *testing.T) {
55+
for _, test := range rot13TestData {
56+
t.Run(test.description, func(t *testing.T) {
57+
input := test.expected
58+
expected := test.input
59+
assertRot13Output(t, input, expected)
60+
})
61+
}
62+
}
63+
64+
func assertRot13Output(t *testing.T, input, expected string) {
65+
actual := rot13(input)
66+
if actual != expected {
67+
t.Fatalf("With input string '%s' was expecting '%s' but actual was '%s'",
68+
input, expected, actual)
69+
}
70+
}
File renamed without changes.

ciphers/rsa/rsa_cipher_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"reflect"
6+
"testing"
7+
"time"
8+
)
9+
10+
var rsaTestData = []struct {
11+
description string
12+
input string
13+
}{
14+
{
15+
"Encrypt letter 'a'",
16+
"a",
17+
},
18+
{
19+
"Encrypt 'hello world'",
20+
"hello world",
21+
},
22+
{
23+
"Encrypt full sentence",
24+
"the quick brown fox jumps over the lazy dog.",
25+
},
26+
{
27+
"Encrypt full sentence from RSAcipher.go main function",
28+
"I think RSA is really great",
29+
},
30+
}
31+
32+
func TestRSACipher(t *testing.T) {
33+
rand.Seed(time.Now().UTC().UnixNano())
34+
bits := 17
35+
36+
p := generatePrimes(1 << bits)
37+
q := generatePrimes(1 << bits)
38+
for p == q {
39+
q = generatePrimes(1 << bits)
40+
}
41+
n := p * q
42+
43+
delta := lcm(p-1, q-1)
44+
45+
e := generatePrimes(delta)
46+
d := modularMultiplicativeInverse(e, delta)
47+
48+
for _, test := range rsaTestData {
49+
t.Run(test.description, func(t *testing.T) {
50+
51+
message := []rune(test.input)
52+
expected := toASCII(message)
53+
54+
encrypted := encryptRSA(expected, e, n)
55+
decrypted := decryptRSA(encrypted, d, n)
56+
57+
if actual := decrypted; !reflect.DeepEqual(actual, expected) {
58+
t.Logf("FAIL: %s", test.description)
59+
t.Fatalf("Expecting %v, actual %v", expected, actual)
60+
}
61+
})
62+
}
63+
}
File renamed without changes.

0 commit comments

Comments
 (0)