-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Solution for Go programming Language.
- Loading branch information
Showing
952 changed files
with
18,666 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package binary_search | ||
// important question | ||
|
||
func BinarySearch(array []int, target int) int { | ||
return binarySearch(array, target, 0, len(array)-1) | ||
} | ||
|
||
func binarySearch(array []int, target int, left int, right int) int { | ||
for left <= right { | ||
middle := (left + right) / 2 | ||
if target == array[middle] { | ||
return middle | ||
} else if target > array[middle] { | ||
left = middle + 1 | ||
} else { | ||
right = middle - 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func binarySearch1(array []int, target int, left int, right int) int { | ||
middle := (left + right) / 2 | ||
if target == array[middle] { | ||
return middle | ||
} else if middle < right && target > array[middle] { | ||
return binarySearch1(array, target, middle+1, right) | ||
} else if middle > left && target < array[middle] { | ||
return binarySearch1(array, target, left, middle-1) | ||
} | ||
|
||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package branch_sums | ||
// important question | ||
|
||
type BinaryTree struct { | ||
Value int | ||
Left *BinaryTree | ||
Right *BinaryTree | ||
} | ||
|
||
func BranchSums(root *BinaryTree) []int { | ||
var sums []int | ||
root.traverse(&sums, 0) | ||
return sums | ||
} | ||
|
||
func NewBinaryTree(root int, values ...int) *BinaryTree { | ||
tree := &BinaryTree{Value: root} | ||
tree.Insert(values, 0) | ||
return tree | ||
} | ||
|
||
func (tree *BinaryTree) Insert(values []int, i int) *BinaryTree { | ||
if i >= len(values) { | ||
return tree | ||
} | ||
val := values[i] | ||
|
||
queue := []*BinaryTree{tree} | ||
for len(queue) > 0 { | ||
var current *BinaryTree | ||
current, queue = queue[0], queue[1:] | ||
if current.Left == nil { | ||
current.Left = &BinaryTree{Value: val} | ||
break | ||
} | ||
queue = append(queue, current.Left) | ||
|
||
if current.Right == nil { | ||
current.Right = &BinaryTree{Value: val} | ||
break | ||
} | ||
queue = append(queue, current.Right) | ||
} | ||
|
||
tree.Insert(values, i+1) | ||
return tree | ||
} | ||
|
||
// https://www.sohamkamani.com/golang/arrays-vs-slices/ | ||
func (tree *BinaryTree) traverse(sums *[]int, sum int) { | ||
if tree != nil { | ||
sum += tree.Value | ||
tree.Left.traverse(sums, sum) | ||
tree.Right.traverse(sums, sum) | ||
|
||
if tree.Left == nil && tree.Right == nil { | ||
*sums = append(*sums, sum) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package bubble_sort | ||
|
||
func BubbleSort(array []int) []int { | ||
isSorted := false | ||
counter := 0 | ||
|
||
for !isSorted { | ||
isSorted = true | ||
for i := 0; i < len(array)-1-counter; i++ { | ||
if array[i] > array[i+1] { | ||
array[i], array[i+1] = array[i+1], array[i] | ||
isSorted = false | ||
} | ||
} | ||
counter++ | ||
} | ||
return array | ||
} | ||
|
||
func bubbleSort(array []int) []int { | ||
isSorted := false | ||
|
||
for !isSorted { | ||
isSorted = true | ||
for i := 0; i < len(array)-1; i++ { | ||
if array[i] > array[i+1] { | ||
array[i], array[i+1] = array[i+1], array[i] | ||
isSorted = false | ||
} | ||
} | ||
|
||
} | ||
return array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package caesar_cipher_encryptor | ||
|
||
func CaesarCipherEncryptor(str string, key int) string { | ||
key = key % 26 | ||
var buff []rune | ||
for i := range str { | ||
charCode := int(str[i]) + key | ||
if charCode > 122 { | ||
charCode = 96 + charCode%122 | ||
} | ||
buff = append(buff, rune(charCode)) | ||
} | ||
|
||
return string(buff) | ||
} | ||
|
||
// caesarCipherEncryptor | ||
// My solution | ||
func caesarCipherEncryptor(str string, key int) string { | ||
key = key % 26 | ||
var buff []byte | ||
for i := range str { | ||
char := int(str[i]) + key | ||
diff := char - 122 | ||
if diff > 0 { | ||
char = 96 + diff | ||
} | ||
buff = append(buff, byte(char)) | ||
} | ||
|
||
return string(buff) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package class_photos | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
func ClassPhotos(redShirtHeights []int, blueShirtHeights []int) bool { | ||
sort.Sort(sort.Reverse(sort.IntSlice(blueShirtHeights))) | ||
sort.Sort(sort.Reverse(sort.IntSlice(redShirtHeights))) | ||
|
||
var backRow string | ||
if redShirtHeights[0] == blueShirtHeights[0] { | ||
return false | ||
} else if redShirtHeights[0] > blueShirtHeights[0] { | ||
backRow = "Red" | ||
} else { | ||
backRow = "Blue" | ||
} | ||
|
||
for i, redShirtHeight := range redShirtHeights { | ||
if backRow == "Red" { | ||
if blueShirtHeights[i] >= redShirtHeight { | ||
return false | ||
} | ||
} else { | ||
if blueShirtHeights[i] <= redShirtHeight { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package depth_first_search | ||
|
||
type Node struct { | ||
Name string | ||
Children []*Node | ||
} | ||
|
||
func NewNode(name string) *Node { | ||
return &Node{ | ||
Name: name, | ||
Children: []*Node{}, | ||
} | ||
} | ||
|
||
func (n *Node) AddChildren(names ...string) *Node { | ||
for _, name := range names { | ||
child := Node{Name: name} | ||
n.Children = append(n.Children, &child) | ||
} | ||
return n | ||
} | ||
|
||
func (n *Node) DepthFirstSearch(array []string) []string { | ||
array = append(array, n.Name) | ||
for _, chidNode := range n.Children { | ||
array = chidNode.DepthFirstSearch(array) | ||
} | ||
return array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Oops, something went wrong.