Skip to content

Commit d93f532

Browse files
committed
Merge branch 'master' of https://github.com/brayo-pip/Go
2 parents 86024c5 + e8b4837 commit d93f532

File tree

16 files changed

+1608
-90
lines changed

16 files changed

+1608
-90
lines changed

DIRECTORY.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
## Ciphers
33
* [Caesarcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/CaesarCipher.go)
4+
* [Diffiehellmankeyexchange](https://github.com/TheAlgorithms/Go/blob/master/ciphers/diffieHellmanKeyExchange.go)
5+
* [Rsacipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/RSAcipher.go)
6+
* [Xorcipher](https://github.com/TheAlgorithms/Go/blob/master/ciphers/xorCipher.go)
47

58
## Data-Structures
69
* Binary-Tree
@@ -20,6 +23,7 @@
2023

2124
## Dynamic-Programming
2225
* [Longest-Palindromic-Subsequence](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/longest-palindromic-subsequence.go)
26+
* [Longestcommonsubsequence](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/longestCommonSubsequence.go)
2327
* [Matrix-Multiplication](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/matrix-multiplication.go)
2428
* [Rod-Cutting](https://github.com/TheAlgorithms/Go/blob/master/dynamic-programming/rod-cutting.go)
2529

@@ -48,4 +52,12 @@
4852
* [Topological Sort](https://github.com/TheAlgorithms/Go/blob/master/sorts/topological_sort.go)
4953

5054
## Strings
55+
* Multiple String Matching
56+
* [Ac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/ac.go)
57+
* [Adac](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/adac.go)
58+
* [Sbom](https://github.com/TheAlgorithms/Go/blob/master/strings/multiple%20string%20matching/sbom.go)
5159
* [Naivestringsearch](https://github.com/TheAlgorithms/Go/blob/master/strings/naiveStringSearch.go)
60+
* Single String Matching
61+
* [Bom](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/bom.go)
62+
* [Horspool](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/horspool.go)
63+
* [Kmp](https://github.com/TheAlgorithms/Go/blob/master/strings/single%20string%20matching/kmp.go)

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Also see: https://algorithmswithgo.com
1616
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
1717

1818
__Properties__
19-
* Worst case performance O(n^2)
20-
* Best case performance O(n)
21-
* Average case performance O(n^2)
19+
* Worst case performance O(n^2)
20+
* Best case performance O(n)
21+
* Average case performance O(n^2)
2222

2323
###### View the algorithm in [action][bubble-toptal]
2424

@@ -30,9 +30,9 @@ __Properties__
3030
From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
3131

3232
__Properties__
33-
* Worst case performance O(n^2)
34-
* Best case performance O(n)
35-
* Average case performance O(n^2)
33+
* Worst case performance O(n^2)
34+
* Best case performance O(n)
35+
* Average case performance O(n^2)
3636

3737
###### View the algorithm in [action][insertion-toptal]
3838

@@ -43,9 +43,9 @@ __Properties__
4343
From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
4444

4545
__Properties__
46-
* Worst case performance O(n log n)
47-
* Best case performance O(n)
48-
* Average case performance O(n)
46+
* Worst case performance O(n log n)
47+
* Best case performance O(n)
48+
* Average case performance O(n)
4949

5050

5151
###### View the algorithm in [action][merge-toptal]
@@ -56,9 +56,9 @@ __Properties__
5656
From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
5757

5858
__Properties__
59-
* Worst case performance O(n^2)
60-
* Best case performance O(n log n) or O(n) with three-way partition
61-
* Average case performance O(n^2)
59+
* Worst case performance O(n^2)
60+
* Best case performance O(n log n) or O(n) with three-way partition
61+
* Average case performance O(n^2)
6262

6363
###### View the algorithm in [action][quick-toptal]
6464

@@ -68,9 +68,9 @@ __Properties__
6868
From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
6969

7070
__Properties__
71-
* Worst case performance O(n^2)
72-
* Best case performance O(n^2)
73-
* Average case performance O(n^2)
71+
* Worst case performance O(n^2)
72+
* Best case performance O(n^2)
73+
* Average case performance O(n^2)
7474

7575
###### View the algorithm in [action][selection-toptal]
7676

@@ -86,7 +86,7 @@ __Properties__
8686

8787
###### View the algorithm in [action][shell-toptal]
8888

89-
### Time-Compexity Graphs
89+
### Time-Complexity Graphs
9090

9191
Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort)
9292

@@ -100,24 +100,24 @@ Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Sel
100100
![alt text][linear-image]
101101

102102
From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
103-
Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.
103+
Linear search runs in at the worst linear time and makes at most n comparisons, where n is the length of the list.
104104

105105
__Properties__
106-
* Worst case performance O(n)
107-
* Best case performance O(1)
108-
* Average case performance O(n)
109-
* Worst case space complexity O(1) iterative
106+
* Worst case performance O(n)
107+
* Best case performance O(1)
108+
* Average case performance O(n)
109+
* Worst case space complexity O(1) iterative
110110

111111
### Binary
112112
![alt text][binary-image]
113113

114114
From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
115115

116116
__Properties__
117-
* Worst case performance O(log n)
118-
* Best case performance O(1)
119-
* Average case performance O(log n)
120-
* Worst case space complexity O(1)
117+
* Worst case performance O(log n)
118+
* Best case performance O(1)
119+
* Average case performance O(log n)
120+
* Worst case space complexity O(1)
121121

122122
----------------------------------------------------------------------------------------------------------------------
123123

@@ -128,7 +128,7 @@ __Properties__
128128
In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.<br>
129129
It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. <br>
130130
The method is named after **Julius Caesar**, who used it in his private correspondence.<br>
131-
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.
131+
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern applications in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.
132132
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher)
133133

134134
### Transposition

ciphers/RSAcipher(Big).go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
"math/big"
5+
crypto"crypto/rand"
6+
"strconv"
7+
"fmt"
8+
)
9+
/*
10+
Care has been taken to uses cryptographic secure functions
11+
The primes numbers are 1024 bits which is as secure as u can get really
12+
crypto/rand library has been imported as crypto and not rand
13+
This import style will make it easier to spot all the cryptographic secure functions
14+
*/
15+
func main(){
16+
p,_:= crypto.Prime(crypto.Reader,1024)
17+
q,_:= crypto.Prime(crypto.Reader,1024)
18+
if !(primeCheck(p)||primeCheck(q)){
19+
//they are always prime, no worries
20+
fmt.Println("These numbers ain't prime")
21+
}
22+
n := new(big.Int).Mul(p,q)
23+
24+
one := big.NewInt(1)
25+
26+
delta := lcmBig(p.Sub(p,one),q.Sub(q,one))
27+
28+
e,_:= crypto.Prime(crypto.Reader,delta.BitLen())
29+
d := big.NewInt(0)
30+
d.ModInverse(e,delta)
31+
32+
cleartext := "Black Lives Matter, all lives can't matter until Black lives matter"
33+
runes :=[]rune(cleartext)
34+
ASCIIs :=toASCII(runes)
35+
stringEncoded := stringEncode(ASCIIs)
36+
bigNum, _ := new(big.Int).SetString(stringEncoded,0)
37+
/*
38+
TODO: check that bigNum is not larger than N if larger break
39+
into two or more strings and encrypt separately
40+
*/
41+
fmt.Printf("Message to be encrypted: %v \n",cleartext)
42+
fmt.Printf("ASCII encoded: %v\n",bigNum)
43+
encrypted := encryptBig(bigNum,e,n)
44+
fmt.Printf("ciphertext: %v \n",encrypted)
45+
decrypted := decryptBig(encrypted,d,n)
46+
fmt.Printf("Decrypted but still ASCII encoded: %v \n",decrypted)
47+
decryptASCIIs := stringDecode(decrypted)
48+
fmt.Printf("Plaintext (original message) :%v",toRune(decryptASCIIs))
49+
}
50+
51+
func encryptBig(num *big.Int, privateExponent *big.Int, modulus *big.Int)*big.Int{
52+
//encrypts by modular exponentiation
53+
encrypted := new(big.Int).Exp(num,privateExponent,modulus)
54+
return encrypted
55+
}
56+
57+
func decryptBig(num *big.Int, publicExponent *big.Int, modulus *big.Int)*big.Int{
58+
//decrypts by modular exponentiation
59+
decrypted := new(big.Int).Exp(num,publicExponent,modulus)
60+
return decrypted
61+
}
62+
63+
func lcmBig(x *big.Int, y *big.Int)*big.Int{
64+
//an lcm implementation for big.Int numbers
65+
gcd := new(big.Int).GCD(nil,nil,x,y)
66+
temp := new(big.Int).Mul(x,y)
67+
lcm := new(big.Int).Div(temp,gcd)
68+
return lcm
69+
}
70+
71+
func primeCheck(prime *big.Int)bool{
72+
//primality test
73+
return prime.ProbablyPrime(256)
74+
}
75+
76+
func toASCII(slice []rune)[]int{
77+
//runs in O(n) where n = len(slice)
78+
var converted []int
79+
for _,v:= range slice{
80+
converted = append(converted, int(v))
81+
}
82+
return converted
83+
}
84+
85+
func toRune(slice []int)string{
86+
//runs in O(n) where n = len(slice)
87+
var str string
88+
for _,v:= range slice{
89+
str += string(v)
90+
}
91+
return str
92+
}
93+
94+
func stringEncode(slice []int)string{
95+
//encodes the ASCII to a string
96+
var out []string
97+
for _,v := range slice{
98+
if v<100 {
99+
out =append(out,"0"+strconv.Itoa(v))
100+
continue
101+
}
102+
out = append(out, strconv.Itoa(v))
103+
}
104+
var str string
105+
for _,v := range out{
106+
str += v
107+
}
108+
/*strips leading 0 if present to avoid conversion errors
109+
*/
110+
if str[0]== '0'{
111+
str = str[1:]
112+
}
113+
return str
114+
}
115+
116+
func stringDecode(decryptedBig *big.Int)[]int{
117+
//decodes the number to string then ASCII values
118+
str := decryptedBig.String()
119+
if len(str)%3!=0{
120+
str ="0"+str
121+
}
122+
var ASCII []int
123+
for i:=0; i<len(str); i+=3{
124+
temp,_:= strconv.Atoi(str[i:i+3])
125+
ASCII = append(ASCII,temp)
126+
}
127+
return ASCII
128+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// LONGEST COMMON SUBSEQUENCE
2+
// DP - 4
3+
// https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
4+
5+
package longestcommonsubsequence
6+
// package main
7+
8+
// import "fmt"
9+
10+
func max(a,b int) int{
11+
if a>b{
12+
return a
13+
}
14+
return b
15+
}
16+
17+
func longestCommonSubsequence(a string, b string, m int, n int) int{
18+
// m is the length of string a and n is the length of string b
19+
20+
// here we are making a 2d slice of size (m+1)*(n+1)
21+
lcs:= make([][] int, m+1)
22+
for i:=0;i<=m;i++{
23+
lcs[i] = make([]int, n+1)
24+
}
25+
26+
// block that implements LCS
27+
for i:=0;i<=m;i++{
28+
for j:=0;j<=n;j++{
29+
if i==0 || j==0{
30+
lcs[i][j] = 0;
31+
}else if a[i-1] == b[j-1]{
32+
lcs[i][j] = lcs[i-1][j-1]+1
33+
}else{
34+
lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1])
35+
}
36+
}
37+
}
38+
// returning the length of longest common subsequence
39+
return lcs[m][n]
40+
}
41+
42+
// func main(){
43+
// // declaring two strings and asking for input
44+
45+
// var a,b string
46+
// fmt.Scan(&a, &b)
47+
// // calling the LCS function
48+
// fmt.Println("The length of longest common subsequence is:", longestCommonSubsequence(a,b, len(a), len(b)))
49+
// }

searches/binary_search.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +0,0 @@
1-
/*
2-
Binary search implementation in Go
3-
*/
4-
package main
5-
6-
func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
7-
if highIndex < lowIndex {
8-
return -1
9-
}
10-
mid := int((lowIndex + (highIndex-lowIndex)/2))
11-
if array[mid] > target {
12-
return binarySearch(array, target, lowIndex, mid)
13-
} else if array[mid] < target {
14-
return binarySearch(array, target, mid+1, highIndex)
15-
} else {
16-
return mid
17-
}
18-
}
19-
20-
func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int {
21-
startIndex := lowIndex
22-
endIndex := highIndex
23-
var mid int
24-
for startIndex < endIndex {
25-
mid = int((startIndex + (endIndex-startIndex)/2))
26-
if array[mid] > target {
27-
endIndex = mid
28-
} else if array[mid] < target {
29-
startIndex = mid
30-
} else {
31-
return mid
32-
}
33-
}
34-
return -1
35-
}

0 commit comments

Comments
 (0)