Skip to content

Commit

Permalink
Add Solution for Go programming Language.
Browse files Browse the repository at this point in the history
  • Loading branch information
drmacsika committed Jan 29, 2022
1 parent fdc8895 commit b238b3d
Show file tree
Hide file tree
Showing 952 changed files with 18,666 additions and 0 deletions.
1 change: 1 addition & 0 deletions Easy/BinarySearch/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BinarySearch/BinarySearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions Easy/BinarySearch/BinarySearch.go
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
}
1 change: 1 addition & 0 deletions Easy/BinarySearch/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BinarySearch/BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BinarySearch/BinarySearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BranchSums/BranchSums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BranchSums/BranchSums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

60 changes: 60 additions & 0 deletions Easy/BranchSums/BranchSums.go
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)
}
}
}
1 change: 1 addition & 0 deletions Easy/BranchSums/BranchSums.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BranchSums/BranchSums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BranchSums/BranchSums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BubbleSort/BubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BubbleSort/BubbleSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

34 changes: 34 additions & 0 deletions Easy/BubbleSort/BubbleSort.go
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
}
1 change: 1 addition & 0 deletions Easy/BubbleSort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BubbleSort/BubbleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/BubbleSort/BubbleSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

32 changes: 32 additions & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.go
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)
}
1 change: 1 addition & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/CaesarCypherEncryptor/CaesarCypherEncryptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/ClassPhotos/ClassPhotos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/ClassPhotos/ClassPhotos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions Easy/ClassPhotos/ClassPhotos.go
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
}
1 change: 1 addition & 0 deletions Easy/ClassPhotos/ClassPhotos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/ClassPhotos/ClassPhotos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/ClassPhotos/ClassPhotos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

29 changes: 29 additions & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.go
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
}
1 change: 1 addition & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/Depth-FirstSearch/Depth-FirstSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/FindClosestValueInBST/FindClosestValueInBST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Easy/FindClosestValueInBST/FindClosestValueInBST.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit b238b3d

Please sign in to comment.