-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsamples.go
More file actions
154 lines (132 loc) · 2.79 KB
/
samples.go
File metadata and controls
154 lines (132 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Most of the algorithm examples are taken from https://github.com/TheAlgorithms/Go
package simple
import (
"errors"
"math"
)
// DivOrPanic divides x by y or panics if y is 0
func DivOrPanic(x int, y int) int {
if y == 0 {
panic("div by 0")
}
return x / y
}
// Extended simple extended gcd
func Extended(a, b int64) (int64, int64, int64) {
if a == 0 {
return b, 0, 1
}
gcd, xPrime, yPrime := Extended(b%a, a)
return gcd, yPrime - (b/a)*xPrime, xPrime
}
func ArraySum(array [5]int) int {
sum := 0
for _, elem := range array {
sum += elem
}
return sum
}
func GenerateArrayOfIntegers(num int) [10]int {
result := [10]int{}
for i := range result {
result[i] = num
}
return result
}
type Point struct {
x, y float64
}
func DistanceBetweenTwoPoints(a, b Point) float64 {
return math.Sqrt(math.Pow(a.x-b.x, 2) + math.Pow(a.y-b.y, 2))
}
func GetCoordinatesOfMiddleBetweenTwoPoints(a, b Point) (float64, float64) {
return (a.x + b.x) / 2, (a.y + b.y) / 2
}
func GetCoordinateSumOfPoints(points []Point) (float64, float64) {
sumX := 0.0
sumY := 0.0
for _, point := range points {
sumX += point.x
sumY += point.y
}
return sumX, sumY
}
type Circle struct {
Center Point
Radius float64
}
func GetAreaOfCircle(circle Circle) float64 {
return math.Pi * math.Pow(circle.Radius, 2)
}
func IsIdentity(matrix [3][3]int) bool {
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == j && matrix[i][j] != 1 {
return false
}
if i != j && matrix[i][j] != 0 {
return false
}
}
}
return true
}
var ErrNotFound = errors.New("target not found in array")
// Binary search for target within a sorted array by repeatedly dividing the array in half and comparing the midpoint with the target.
// This function uses recursive call to itself.
// If a target is found, the index of the target is returned. Else the function return -1 and ErrNotFound.
func Binary(array []int, target int, lowIndex int, highIndex int) (int, error) {
if highIndex < lowIndex {
return -1, ErrNotFound
}
mid := lowIndex + (highIndex-lowIndex)/2
if array[mid] > target {
return Binary(array, target, lowIndex, mid-1)
} else if array[mid] < target {
return Binary(array, target, mid+1, highIndex)
} else {
return mid, nil
}
}
func StringSearch(str string) bool {
if len(str) != 3 {
return false
}
if str[0] == 'A' {
if str[1] == 'B' {
if str[2] == 'C' {
return true
}
}
}
return false
}
func SumOfChanElements(c <-chan int) int {
sum := 0
for val := range c {
sum += val
}
return sum
}
type List struct {
tail *List
val int
}
func LenOfList(l *List) int {
if l == nil {
return 0
}
length := 1
for ; l.tail != nil; l = l.tail {
length++
}
return length
}
func GetLastNode(n *Node) *Node {
if n == nil {
return nil
}
for ; n.next != nil; n = n.next {
}
return n
}