Skip to content

Commit e9a2d3d

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 7a06c93 + d93f532 commit e9a2d3d

File tree

7 files changed

+79
-39
lines changed

7 files changed

+79
-39
lines changed

ciphers/diffieHellmanKeyExchange.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,40 @@ func main() {
2424

2525
AliceSecret := 1 + rand.Intn(1<<bit)
2626
BobSecret := 1 + rand.Intn(1<<bit)
27-
28-
fmt.Printf("Alice's secret key is: %v", AliceSecret)
29-
fmt.Printf("Bob's secret key is: %v", BobSecret)
30-
31-
//Both parties send ((g^secret_key)%p)
27+
28+
fmt.Printf("Alice's secret key is: %v",AliceSecret)
29+
fmt.Printf("Bob's secret key is: %v",BobSecret)
30+
31+
//Both parties send ((g^secret_key)%p)
3232
//It's not possible to determine the secretkey from the value sent
3333

3434
AliceSends := modularExponentiation(g, AliceSecret, p)
3535
BobSends := modularExponentiation(g, BobSecret, p)
3636

37-
fmt.Printf("Alice sends to Bob: %v", AliceSends)
38-
fmt.Printf("Bob sends to Alice: %v", BobSends)
39-
4037
//Both parties calculate the shared secret key from the value send
38+
<<<<<<< HEAD
39+
40+
=======
4141
//(value_sent^secret_key)%p
4242
//Both calculations end up with same value despite the different inputs
43-
AliceComputes := modularExponentiation(BobSends, AliceSecret, p)
44-
BobComputes := modularExponentiation(AliceSends, BobSecret, p)
45-
46-
fmt.Printf("Alice Computes the shared secret key as: %v", AliceComputes)
47-
fmt.Printf("Bob Computes the shared secret key as: %v", BobComputes)
43+
AliceComputes :=modularExponentiation(BobSends,AliceSecret,p)
44+
BobComputes := modularExponentiation(AliceSends,BobSecret,p)
4845

46+
fmt.Printf("Alice Computes the shared secret key as: %v",AliceComputes)
47+
fmt.Printf("Bob Computes the shared secret key as: %v",BobComputes)
48+
4949
// simply confirms that the values are equal
50-
if AliceComputes == BobComputes {
51-
sharedKey := AliceComputes
52-
fmt.Println(" Voila, shared key is", sharedKey)
50+
if AliceComputes == BobComputes{
51+
sharedKey:=AliceComputes
52+
fmt.Println(" Voila, shared key is",sharedKey)
5353
}
54+
}
55+
56+
5457

58+
5559
}
56-
func modularExponentiation(b int, e int, mod int) int {
60+
func modularExponentiation(b int, e int, mod int)int{
5761
//runs in O(log(n)) where n = e
5862
//uses exponentiation by squaring to speed up the process
5963
if mod == 1 {
@@ -65,8 +69,8 @@ func modularExponentiation(b int, e int, mod int) int {
6569
if e%2 == 1 {
6670
r = (r * b) % mod
6771
}
68-
e = e >> 1
69-
b = (b * b) % mod
72+
e =e>>1
73+
b = (b*b)%mod
7074
}
7175
return r
7276
}

ciphers/xorCipher.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func toASCII(slice []rune) []int {
3434
}
3535
func main() {
3636
str := "hello world"
37-
key := 97
38-
temp := []rune(str)
39-
message := toASCII(temp)
40-
encrypted := encrypt(key, message)
41-
decrypted := decrypt(key, encrypted)
42-
plaintext := decodeToString(decrypted)
37+
key :=97
38+
temp:= []rune(str)
39+
message:=toASCII(temp)
40+
encrypted:=encrypt(key,message)
41+
decrypted:=decrypt(key,encrypted)
42+
plaintext:=decodeToString(decrypted)
4343
fmt.Println(plaintext)
4444
}

data-structures/linked-list/Linkedlist.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "fmt"
44

55
/* v is the value of node; next is the pointer to next node */
66
type node struct {
7-
v int
7+
v int
88
next *node
99
}
1010

@@ -24,9 +24,9 @@ func (l *node) pushFront(val int) *node {
2424
nnode = head
2525
/* create a second node with new value and `next -> nnode`
2626
* is this way, nnode2 is before nnode
27-
*/
28-
nnode2 := &node {
29-
v: val,
27+
*/
28+
nnode2 := &node{
29+
v: val,
3030
next: nnode,
3131
}
3232
/* now head is equals nnode2 */
@@ -62,7 +62,7 @@ func (l *node) popFront() *node {
6262
/* create a new node equals to first node pointed by head */
6363
cpnode := new(node)
6464
cpnode = head.next
65-
65+
6666
/* now head is equals cpnode (second node) */
6767
head = cpnode
6868

@@ -76,7 +76,7 @@ func (l *node) popBack() *node {
7676
/* create a new node equals to head */
7777
cpnode := new(node)
7878
cpnode = head
79-
79+
8080
/* read list to the penultimate node */
8181
for cpnode.next.next != nil {
8282
cpnode = cpnode.next
@@ -89,14 +89,14 @@ func (l *node) popBack() *node {
8989
func main() {
9090
lista := new(node)
9191
lista.pushBack(25).pushBack(24).pushBack(32) /* lista: 25 24 32 */
92-
lista.pushBack(56) /* lista: 25 24 32 56 */
93-
lista.pushFront(36) /* lista: 36 25 24 32 56 */
94-
lista.popFront() /* lista: 25 24 32 56 */
95-
lista.popBack() /* lista: 25 24 32 */
96-
92+
lista.pushBack(56) /* lista: 25 24 32 56 */
93+
lista.pushFront(36) /* lista: 36 25 24 32 56 */
94+
lista.popFront() /* lista: 25 24 32 56 */
95+
lista.popBack() /* lista: 25 24 32 */
96+
9797
/* read the list until head is not nil */
9898
for head != nil {
99-
fmt.Printf("%d ",head.v)
99+
fmt.Printf("%d ", head.v)
100100
head = head.next /*head points to next node */
101101
}
102102
}

math/fastExponent.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
//based on square and multiply algorithm
4+
5+
func fastExponentiation(b int, e int) float64 {
6+
//runs in O(log(n)) where n = e
7+
if e == 0 {
8+
return 1.0
9+
}
10+
if e == 1 {
11+
return float64(b)
12+
}
13+
if e < 0 {
14+
e *= -1
15+
println("executed")
16+
return 1 / fastExponentiation(b, e)
17+
}
18+
r := 1
19+
for e > 0 {
20+
if e%2 == 1 {
21+
r = r * b
22+
}
23+
e = e >> 1
24+
b = b * b
25+
}
26+
return float64(r)
27+
}
28+
29+
func main() {
30+
print(fastExponentiation(2, 0))
31+
}

other/NestedBrackets.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// For example, the string "()()[()]" is properly nested but "[(()]" is not.
88
// The function called isBalanced takes as input a string which is a sequence of brackets and
99
// returns true if input is nested and false otherwise.
10+
//note that only an even number of brackets can be properly nested
1011

1112
package main
1213

@@ -18,7 +19,8 @@ import (
1819
)
1920

2021
func isBalanced(input string) string {
21-
22+
if len(input)%2 != 0{
23+
return input + "is not balanced."
2224
if len(input) > 0 {
2325
var stack []byte
2426
for i := 0; i < len(input); i++ {

searches/binary_search.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
/*
23
Binary search implementation in Go
34
*/
@@ -33,3 +34,5 @@ func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int
3334
}
3435
return -1
3536
}
37+
=======
38+
>>>>>>> master

sorts/sorts_case_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type sortTest struct {
1313
name string
1414
}
1515

16-
var arr []int = makeRandArray(500_000)
16+
var arr []= makeRandArray(500_000)
1717

1818
var sortTests = []sortTest{
1919
//Sorted slice

0 commit comments

Comments
 (0)