Skip to content

Commit

Permalink
Merge branch 'TheAlgorithms-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgurwara99 committed Sep 5, 2021
2 parents 38ea851 + 41dddda commit 3930c92
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 66 deletions.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
package dynamic_programming
package dynamic

// func main() {
// myArrayOfK := [4]int{5, 6, 7, 8}
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/fibonacci.go → dynamic/fibonacci.go
@@ -1,4 +1,4 @@
package dynamic_programming
package dynamic

// https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/

Expand Down
@@ -1,4 +1,4 @@
package dynamic_programming
package dynamic

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/knapsack.go → dynamic/knapsack.go
@@ -1,4 +1,4 @@
package dynamic_programming
package dynamic

// Knapsack Problem
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
Expand Down
Expand Up @@ -2,7 +2,7 @@
// DP - 4
// https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/

package dynamic_programming
package dynamic

// LongestCommonSubsequence function
func LongestCommonSubsequence(a string, b string, m int, n int) int {
Expand Down
@@ -1,7 +1,7 @@
// longest palindromic subsequence
// http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/

package dynamic_programming
package dynamic

// LpsRec function
func LpsRec(word string, i, j int) int {
Expand Down
Expand Up @@ -3,7 +3,7 @@
// www.geeksforgeeks.org/dynamic_programming-set-8-matrix-chain-multiplication/

// Package dynamic_programming Package for dynamically run algorithms
package dynamic_programming
package dynamic

// MatrixChainRec function
func MatrixChainRec(D []int, i, j int) int {
Expand Down
Expand Up @@ -2,7 +2,7 @@
// https://en.wikipedia.org/wiki/Cutting_stock_problem
// http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/

package dynamic_programming
package dynamic

// CutRodRec solve the problem recursively: initial approach
func CutRodRec(price []int, length int) int {
Expand Down
16 changes: 8 additions & 8 deletions genetic_algorithm/geneticalgorithm.go → genetic/genetic.go
Expand Up @@ -9,7 +9,7 @@ https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_stri
Author: D4rkia
*/

package main
package genetic

import (
"errors"
Expand Down Expand Up @@ -148,10 +148,10 @@ func geneticString(target string, charmap []rune) (int, int, string) {
return gen, generatedPop, pop[0].Key
}

func main() {
// Define parameters
target := string("This is a genetic algorithm to evaluate, combine, evolve and mutate a string!")
charmap := []rune(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\")
gen, generatedPop, best := geneticString(target, charmap)
fmt.Println("Generation:", strconv.Itoa(gen), "Analyzed:", generatedPop, "Best:", best)
}
// func main() {
// // Define parameters
// target := string("This is a genetic algorithm to evaluate, combine, evolve and mutate a string!")
// charmap := []rune(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\")
// gen, generatedPop, best := geneticString(target, charmap)
// fmt.Println("Generation:", strconv.Itoa(gen), "Analyzed:", generatedPop, "Best:", best)
// }
File renamed without changes.
@@ -1,4 +1,4 @@
package breadth_first_search
package graph

import (
"testing"
Expand Down
@@ -1,4 +1,4 @@
package depth_first_search
package graph

func GetIdx(target int, nodes []int) int {
for i := 0; i < len(nodes); i++ {
Expand All @@ -18,7 +18,7 @@ func NotExist(target int, slice []int) bool {
return true
}

func Dfs(start, end int, nodes []int, edges [][]bool) ([]int, bool) {
func DepthFirstSearch(start, end int, nodes []int, edges [][]bool) ([]int, bool) {
var route []int
var stack []int
startIdx := GetIdx(start, nodes)
Expand Down
@@ -1,7 +1,7 @@
// Floyd-Warshall algorithm
// https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

package floyd_warshall
package graph

// Matrix Defining matrix to use 2d array easier
type Matrix [][]float64
Expand Down
@@ -1,7 +1,7 @@
/* O(n) solution, for calculating
maximum contiguous sum in the given array. */

package max_subarray_sum
package maxsubarraysum

// Max - already defined somewhere in this repository TODO: remove this definition and add the import path
func Max(x int, y int) int {
Expand Down
@@ -1,6 +1,6 @@
// Package nested_brackets provides functions for testing
// strings propper brackets nesting.
package nested_brackets
package nested

// IsBalanced returns true if provided input string is properly nested.
//
Expand Down
@@ -1,4 +1,4 @@
package nested_brackets
package nested

import (
"testing"
Expand Down
Expand Up @@ -10,8 +10,8 @@ import (
"math/big"
)

// GeneratePassword returns a newly generated password
func GeneratePassword(minLength int, maxLength int) string {
// Generate returns a newly generated password
func Generate(minLength int, maxLength int) string {
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~")

length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength-minLength)))
Expand Down Expand Up @@ -43,17 +43,3 @@ func GeneratePassword(minLength int, maxLength int) string {
}
}
}

// func main() {
// rand.Seed(time.Now().Unix())

// fmt.Print("Please specify a minimum length: ")
// var minLength int
// fmt.Scanf("%d", &minLength)

// fmt.Print("Please specify a maximum length: ")
// var maxLength int
// fmt.Scanf("%d", &maxLength)

// fmt.Printf("Your generated password is %v\n", generatePassword(minLength, maxLength))
// }
53 changes: 26 additions & 27 deletions structure/binarytree/binarysearchtree.go
Expand Up @@ -8,31 +8,30 @@ package binarytree
// )

// func main() {
// t := &BTree{nil}
// InOrder(t.root)
// t.root = Insert(t.root, 30)

// t.root = Insert(t.root, 20)
// t.root = Insert(t.root, 15)
// t.root = Insert(t.root, 10)
// t.root = Insert(t.root, 12)
// t.root = Insert(t.root, 9)
// t.root = Insert(t.root, 11)
// t.root = Insert(t.root, 17)
// fmt.Print(t.depth(), "\n")
// InOrder(t.root)
// fmt.Print("\n")
// t.root = BstDelete(t.root, 10)
// InOrder(t.root)
// fmt.Print("\n")
// t.root = BstDelete(t.root, 30)
// InOrder(t.root)
// fmt.Print("\n")
// t.root = BstDelete(t.root, 15)
// InOrder(t.root)
// fmt.Print("\n")
// t.root = BstDelete(t.root, 20)
// InOrder(t.root)
// fmt.Print("\n")
// fmt.Print(t.depth(), "\n")
//t := &BTree{nil}
//InOrder(t.Root)
//t.Root = Insert(t.Root, 30)
//t.Root = Insert(t.Root, 20)
//t.Root = Insert(t.Root, 15)
//t.Root = Insert(t.Root, 10)
//t.Root = Insert(t.Root, 12)
//t.Root = Insert(t.Root, 9)
//t.Root = Insert(t.Root, 11)
//t.Root = Insert(t.Root, 17)
//fmt.Print(t.Depth(), "\n")
//InOrder(t.Root)
//fmt.Print("\n")
//t.Root = BstDelete(t.Root, 10)
//InOrder(t.Root)
//fmt.Print("\n")
//t.Root = BstDelete(t.Root, 30)
//InOrder(t.Root)
//fmt.Print("\n")
//t.Root = BstDelete(t.Root, 15)
//InOrder(t.Root)
//fmt.Print("\n")
//t.Root = BstDelete(t.Root, 20)
//InOrder(t.Root)
//fmt.Print("\n")
//fmt.Print(t.Depth(), "\n")
// }
29 changes: 29 additions & 0 deletions structure/binarytree/btree.go
Expand Up @@ -116,6 +116,35 @@ func LevelOrder(root *Node) {
}
}

// AccessNodesByLayer Function that access nodes layer by layer instead of printing the results as one line.
func AccessNodesByLayer(root *Node) [][]int {
var res [][]int
if root == nil {
return res
}
var q []*Node
var n *Node
var idx = 0
q = append(q, root)

for len(q) != 0 {
res = append(res, []int{})
qLen := len(q)
for i := 0; i < qLen; i++ {
n, q = q[0], q[1:]
res[idx] = append(res[idx], n.val)
if n.left != nil {
q = append(q, n.left)
}
if n.right != nil {
q = append(q, n.right)
}
}
idx++
}
return res
}

// Max Function that returns max of two numbers - possibly already declared.
func Max(a, b int) int {
if a > b {
Expand Down

0 comments on commit 3930c92

Please sign in to comment.