From 61ef7151664fe893b8ffe56945468ccd5ca8cd37 Mon Sep 17 00:00:00 2001 From: Tajmeet Singh Date: Tue, 7 Sep 2021 13:28:09 +0100 Subject: [PATCH 1/4] doc: fix: Added documentations that works with the autogenerated documentation --- {ciphers => cipher}/caesar/caesar.go | 0 {ciphers => cipher}/caesar/caesar_test.go | 0 .../diffiehellman/diffiehellmankeyexchange.go | 0 .../diffiehellmankeyexchange_test.go | 0 cipher/doc.go | 2 ++ {ciphers => cipher}/polybius/polybius.go | 0 {ciphers => cipher}/polybius/polybius_test.go | 0 {ciphers => cipher}/rot13/rot13.go | 2 +- {ciphers => cipher}/rot13/rot13_test.go | 0 {ciphers => cipher}/rsa/rsa.go | 0 {ciphers => cipher}/rsa/rsa_test.go | 0 {ciphers => cipher}/xor/xor.go | 0 {ciphers => cipher}/xor/xor_test.go | 0 conversion/doc.go | 2 ++ {conversions => conversion}/romantointeger.go | 15 +++++++-------- .../romantointeger_test.go | 2 +- dynamic/doc.go | 2 ++ dynamic/matrixmultiplication.go | 1 - graph/breadthfirstsearch.go | 2 -- graph/doc.go | 3 +++ math/doc.go | 2 ++ math/max/max.go | 10 ++++++++++ other/doc.go | 3 +++ other/maxsubarraysum/maxsubarraysum.go | 15 ++++++--------- other/nested/nestedbrackets.go | 2 +- other/password/generator.go | 3 ++- search/doc.go | 2 ++ strings/doc.go | 3 +++ {genetic => strings/genetic}/genetic.go | 0 .../genetic}/geneticalgorithm_test.go | 0 structure/doc.go | 4 ++++ 31 files changed, 51 insertions(+), 24 deletions(-) rename {ciphers => cipher}/caesar/caesar.go (100%) rename {ciphers => cipher}/caesar/caesar_test.go (100%) rename {ciphers => cipher}/diffiehellman/diffiehellmankeyexchange.go (100%) rename {ciphers => cipher}/diffiehellman/diffiehellmankeyexchange_test.go (100%) create mode 100644 cipher/doc.go rename {ciphers => cipher}/polybius/polybius.go (100%) rename {ciphers => cipher}/polybius/polybius_test.go (100%) rename {ciphers => cipher}/rot13/rot13.go (88%) rename {ciphers => cipher}/rot13/rot13_test.go (100%) rename {ciphers => cipher}/rsa/rsa.go (100%) rename {ciphers => cipher}/rsa/rsa_test.go (100%) rename {ciphers => cipher}/xor/xor.go (100%) rename {ciphers => cipher}/xor/xor_test.go (100%) create mode 100644 conversion/doc.go rename {conversions => conversion}/romantointeger.go (60%) rename {conversions => conversion}/romantointeger_test.go (97%) create mode 100644 dynamic/doc.go create mode 100644 graph/doc.go create mode 100644 math/doc.go create mode 100644 math/max/max.go create mode 100644 other/doc.go create mode 100644 search/doc.go create mode 100644 strings/doc.go rename {genetic => strings/genetic}/genetic.go (100%) rename {genetic => strings/genetic}/geneticalgorithm_test.go (100%) create mode 100644 structure/doc.go diff --git a/ciphers/caesar/caesar.go b/cipher/caesar/caesar.go similarity index 100% rename from ciphers/caesar/caesar.go rename to cipher/caesar/caesar.go diff --git a/ciphers/caesar/caesar_test.go b/cipher/caesar/caesar_test.go similarity index 100% rename from ciphers/caesar/caesar_test.go rename to cipher/caesar/caesar_test.go diff --git a/ciphers/diffiehellman/diffiehellmankeyexchange.go b/cipher/diffiehellman/diffiehellmankeyexchange.go similarity index 100% rename from ciphers/diffiehellman/diffiehellmankeyexchange.go rename to cipher/diffiehellman/diffiehellmankeyexchange.go diff --git a/ciphers/diffiehellman/diffiehellmankeyexchange_test.go b/cipher/diffiehellman/diffiehellmankeyexchange_test.go similarity index 100% rename from ciphers/diffiehellman/diffiehellmankeyexchange_test.go rename to cipher/diffiehellman/diffiehellmankeyexchange_test.go diff --git a/cipher/doc.go b/cipher/doc.go new file mode 100644 index 000000000..0b1ebfb19 --- /dev/null +++ b/cipher/doc.go @@ -0,0 +1,2 @@ +// Package cipher is a package containing different implementations of certain ciphers +package cipher diff --git a/ciphers/polybius/polybius.go b/cipher/polybius/polybius.go similarity index 100% rename from ciphers/polybius/polybius.go rename to cipher/polybius/polybius.go diff --git a/ciphers/polybius/polybius_test.go b/cipher/polybius/polybius_test.go similarity index 100% rename from ciphers/polybius/polybius_test.go rename to cipher/polybius/polybius_test.go diff --git a/ciphers/rot13/rot13.go b/cipher/rot13/rot13.go similarity index 88% rename from ciphers/rot13/rot13.go rename to cipher/rot13/rot13.go index 83090129c..9b822332b 100644 --- a/ciphers/rot13/rot13.go +++ b/cipher/rot13/rot13.go @@ -3,7 +3,7 @@ package rot13 import ( - "github.com/TheAlgorithms/Go/ciphers/caesar" + "github.com/TheAlgorithms/Go/cipher/caesar" ) // rot13 is a special case, which is fixed the shift of 13, of the Caesar cipher diff --git a/ciphers/rot13/rot13_test.go b/cipher/rot13/rot13_test.go similarity index 100% rename from ciphers/rot13/rot13_test.go rename to cipher/rot13/rot13_test.go diff --git a/ciphers/rsa/rsa.go b/cipher/rsa/rsa.go similarity index 100% rename from ciphers/rsa/rsa.go rename to cipher/rsa/rsa.go diff --git a/ciphers/rsa/rsa_test.go b/cipher/rsa/rsa_test.go similarity index 100% rename from ciphers/rsa/rsa_test.go rename to cipher/rsa/rsa_test.go diff --git a/ciphers/xor/xor.go b/cipher/xor/xor.go similarity index 100% rename from ciphers/xor/xor.go rename to cipher/xor/xor.go diff --git a/ciphers/xor/xor_test.go b/cipher/xor/xor_test.go similarity index 100% rename from ciphers/xor/xor_test.go rename to cipher/xor/xor_test.go diff --git a/conversion/doc.go b/conversion/doc.go new file mode 100644 index 000000000..f5cc05779 --- /dev/null +++ b/conversion/doc.go @@ -0,0 +1,2 @@ +// Package conversion is a package of implementations which converts one data structure to another. +package conversion diff --git a/conversions/romantointeger.go b/conversion/romantointeger.go similarity index 60% rename from conversions/romantointeger.go rename to conversion/romantointeger.go index b0e250bc2..45e386a78 100644 --- a/conversions/romantointeger.go +++ b/conversion/romantointeger.go @@ -1,11 +1,10 @@ -/* -This algorithm will convert a standard roman number to an integer -https://en.wikipedia.org/wiki/Roman_numerals -Function receives a string as a roman number and outputs an integer -Maximum output will be 3999 -Only standard form is supported -*/ -package conversions +// This algorithm will convert a standard roman number to an integer +// https://en.wikipedia.org/wiki/Roman_numerals +// Function receives a string as a roman number and outputs an integer +// Maximum output will be 3999 +// Only standard form is supported + +package conversion var romans = map[string]int{"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} diff --git a/conversions/romantointeger_test.go b/conversion/romantointeger_test.go similarity index 97% rename from conversions/romantointeger_test.go rename to conversion/romantointeger_test.go index 9923e596a..59c175c19 100644 --- a/conversions/romantointeger_test.go +++ b/conversion/romantointeger_test.go @@ -1,4 +1,4 @@ -package conversions +package conversion import "testing" diff --git a/dynamic/doc.go b/dynamic/doc.go new file mode 100644 index 000000000..c6ff7e596 --- /dev/null +++ b/dynamic/doc.go @@ -0,0 +1,2 @@ +// Package dynamic is a package of certain implementations of dynamically run algorithms. +package dynamic diff --git a/dynamic/matrixmultiplication.go b/dynamic/matrixmultiplication.go index 4449c411f..dc4164f99 100644 --- a/dynamic/matrixmultiplication.go +++ b/dynamic/matrixmultiplication.go @@ -2,7 +2,6 @@ // https://en.wikipedia.org/wiki/Matrix_chain_multiplication // www.geeksforgeeks.org/dynamic_programming-set-8-matrix-chain-multiplication/ -// Package dynamic_programming Package for dynamically run algorithms package dynamic // MatrixChainRec function diff --git a/graph/breadthfirstsearch.go b/graph/breadthfirstsearch.go index 620d96a3f..93d29f9e4 100644 --- a/graph/breadthfirstsearch.go +++ b/graph/breadthfirstsearch.go @@ -1,5 +1,3 @@ -// Package graph demonstates Graph search algorithms -// reference: https://en.wikipedia.org/wiki/Tree_traversal package graph // BreadthFirstSearch is an algorithm for traversing and searching graph data structures. diff --git a/graph/doc.go b/graph/doc.go new file mode 100644 index 000000000..32a55f68b --- /dev/null +++ b/graph/doc.go @@ -0,0 +1,3 @@ +// Package graph demonstates Graph search algorithms +// reference: https://en.wikipedia.org/wiki/Tree_traversal +package graph diff --git a/math/doc.go b/math/doc.go new file mode 100644 index 000000000..d44df9e6d --- /dev/null +++ b/math/doc.go @@ -0,0 +1,2 @@ +// Package math is a package that contains mathematical algorithms and its different implementations. +package math diff --git a/math/max/max.go b/math/max/max.go new file mode 100644 index 000000000..bb070a6c8 --- /dev/null +++ b/math/max/max.go @@ -0,0 +1,10 @@ +package max + +// Int gives max of two integers +// defining it here to be used in other subpackages of the repo. +func Int(x int, y int) int { + if x < y { + return y + } + return x +} diff --git a/other/doc.go b/other/doc.go new file mode 100644 index 000000000..dbe11e339 --- /dev/null +++ b/other/doc.go @@ -0,0 +1,3 @@ +// Package other is dedicated to algorithms that +// do not quite fit into any of the other subpackages in this repository. +package other diff --git a/other/maxsubarraysum/maxsubarraysum.go b/other/maxsubarraysum/maxsubarraysum.go index e50453b3b..5b5f2a5eb 100644 --- a/other/maxsubarraysum/maxsubarraysum.go +++ b/other/maxsubarraysum/maxsubarraysum.go @@ -1,27 +1,24 @@ /* O(n) solution, for calculating maximum contiguous sum in the given array. */ +// Package maxsubarraysum is a package containing a solution to a common +// problem of finding max contigious sum within a array of ints. 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 { - if x < y { - return y - } - return x -} +import "github.com/TheAlgorithms/Go/math/max" // MaxSubarraySum returns the maximum subarray sum func MaxSubarraySum(array []int) int { var currentMax int = 0 var maxTillNow int = 0 for _, v := range array { - currentMax = Max(v, currentMax+v) - maxTillNow = Max(maxTillNow, currentMax) + currentMax = max.Int(v, currentMax+v) + maxTillNow = max.Int(maxTillNow, currentMax) } return maxTillNow } +// TODO: make a test file with this // func main() { // array := []int{-2, -5, 6, 0, -2, 0, -3, 1, 0, 5, -6} // fmt.Println("Maximum contiguous sum: ", maxSubarraySum(array)) diff --git a/other/nested/nestedbrackets.go b/other/nested/nestedbrackets.go index c0afd9571..330f78fbb 100644 --- a/other/nested/nestedbrackets.go +++ b/other/nested/nestedbrackets.go @@ -1,4 +1,4 @@ -// Package nested_brackets provides functions for testing +// Package nested provides functions for testing // strings propper brackets nesting. package nested diff --git a/other/password/generator.go b/other/password/generator.go index b25f2f48b..0a8023c26 100644 --- a/other/password/generator.go +++ b/other/password/generator.go @@ -2,7 +2,8 @@ // You must provide a minimum length and a maximum length // This length is not fixed if you generate multiple passwords for the same range -package password_generator +// Package password contains functions to help generate random passwords +package password import ( "crypto/rand" diff --git a/search/doc.go b/search/doc.go new file mode 100644 index 000000000..4c71e36b4 --- /dev/null +++ b/search/doc.go @@ -0,0 +1,2 @@ +// Package search is a subpackage dedicated to all searching algorithms related to slices/arrays. +package search diff --git a/strings/doc.go b/strings/doc.go new file mode 100644 index 000000000..786876fec --- /dev/null +++ b/strings/doc.go @@ -0,0 +1,3 @@ +// Package strings is a package that contains all algorithms +// that are used to analyse and manipulate strings. +package strings diff --git a/genetic/genetic.go b/strings/genetic/genetic.go similarity index 100% rename from genetic/genetic.go rename to strings/genetic/genetic.go diff --git a/genetic/geneticalgorithm_test.go b/strings/genetic/geneticalgorithm_test.go similarity index 100% rename from genetic/geneticalgorithm_test.go rename to strings/genetic/geneticalgorithm_test.go diff --git a/structure/doc.go b/structure/doc.go new file mode 100644 index 000000000..fc157dcfd --- /dev/null +++ b/structure/doc.go @@ -0,0 +1,4 @@ +// Package structure is a subpackage that is dedicated to +// different implementations of data structures in the +// domain of computer science. +package structure From 2d315585803e54270305f5953ffe91d92c839608 Mon Sep 17 00:00:00 2001 From: Taj Date: Tue, 7 Sep 2021 20:52:01 +0100 Subject: [PATCH 2/4] Updated go.mod to accept go version 1.17 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d2d3ee496..e7de82f0c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/TheAlgorithms/Go -go 1.15 +go 1.17 From d3524bd784459af95e64960ca189b8980abc3b87 Mon Sep 17 00:00:00 2001 From: Tajmeet Singh Date: Tue, 7 Sep 2021 22:36:48 +0100 Subject: [PATCH 3/4] test: added a few tests for one of the issues --- math/doc_test.go | 3 ++ math/max/max_test.go | 35 ++++++++++++++++++ other/maxsubarraysum/maxsubarraysum.go | 18 ++++----- other/maxsubarraysum/maxsubarraysum_test.go | 41 +++++++++++++++++++++ 4 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 math/doc_test.go create mode 100644 math/max/max_test.go create mode 100644 other/maxsubarraysum/maxsubarraysum_test.go diff --git a/math/doc_test.go b/math/doc_test.go new file mode 100644 index 000000000..943c734ca --- /dev/null +++ b/math/doc_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package math diff --git a/math/max/max_test.go b/math/max/max_test.go new file mode 100644 index 000000000..dbd1ce8dc --- /dev/null +++ b/math/max/max_test.go @@ -0,0 +1,35 @@ +package max + +import "testing" + +func TestMax(t *testing.T) { + testCases := []struct { + name string + left int + right int + max int + }{ + { + name: "Left is max", + left: 10, + right: 9, + max: 10, + }, + { + name: "right is max", + left: 1, + right: 10, + max: 10, + }, + } + + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + returnedMax := Int(test.left, test.right) + if returnedMax != test.max { + t.Errorf("Failed test %s\n\tleft: %v, right: %v, max: %v but received: %v", + test.name, test.left, test.right, test.max, returnedMax) + } + }) + } +} diff --git a/other/maxsubarraysum/maxsubarraysum.go b/other/maxsubarraysum/maxsubarraysum.go index 5b5f2a5eb..81c5042f7 100644 --- a/other/maxsubarraysum/maxsubarraysum.go +++ b/other/maxsubarraysum/maxsubarraysum.go @@ -5,21 +5,21 @@ maximum contiguous sum in the given array. */ // problem of finding max contigious sum within a array of ints. package maxsubarraysum -import "github.com/TheAlgorithms/Go/math/max" +import ( + "github.com/TheAlgorithms/Go/math/max" +) // MaxSubarraySum returns the maximum subarray sum func MaxSubarraySum(array []int) int { - var currentMax int = 0 - var maxTillNow int = 0 + var currentMax int + var maxTillNow int + if len(array) != 0 { + currentMax = array[0] + maxTillNow = array[0] + } for _, v := range array { currentMax = max.Int(v, currentMax+v) maxTillNow = max.Int(maxTillNow, currentMax) } return maxTillNow } - -// TODO: make a test file with this -// func main() { -// array := []int{-2, -5, 6, 0, -2, 0, -3, 1, 0, 5, -6} -// fmt.Println("Maximum contiguous sum: ", maxSubarraySum(array)) -// } diff --git a/other/maxsubarraysum/maxsubarraysum_test.go b/other/maxsubarraysum/maxsubarraysum_test.go new file mode 100644 index 000000000..013ba1d88 --- /dev/null +++ b/other/maxsubarraysum/maxsubarraysum_test.go @@ -0,0 +1,41 @@ +package maxsubarraysum + +import ( + "testing" +) + +func TestMaxSubarraySum(t *testing.T) { + testCases := []struct { + name string + slice []int + expected int + }{ + { + name: "Empty slice", + slice: []int{}, + expected: 0, + }, + { + name: "Max is 0", + slice: []int{0, -1, -2, -4, -5}, + expected: 0, + }, + { + name: "Max is -1", + slice: []int{-1, -3, -2, -5, -7}, + expected: -1, + }, + { + name: "Max is 7", + slice: []int{-2, -5, 6, 0, -2, 0, -3, 1, 0, 5, -6}, + expected: 7, + }, + } + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + if result := MaxSubarraySum(test.slice); result != test.expected { + t.Fatalf("%s\n\tslice: %v, expected: %v, returned: %v", test.name, test.slice, test.expected, result) + } + }) + } +} From 18295e857b60477ebfe1e0fd1f9ae682965d6f60 Mon Sep 17 00:00:00 2001 From: Tajmeet Singh Date: Wed, 8 Sep 2021 18:43:47 +0100 Subject: [PATCH 4/4] doc: fix: Added empty test files for packages that don't need test --- cipher/cipher_test.go | 3 +++ conversion/conversion_test.go | 3 +++ dynamic/dynamic_test.go | 3 +++ graph/graph_test.go | 3 +++ math/{doc_test.go => math_test.go} | 0 other/other_test.go | 3 +++ strings/strings_test.go | 3 +++ structure/structure_test.go | 3 +++ 8 files changed, 21 insertions(+) create mode 100644 cipher/cipher_test.go create mode 100644 conversion/conversion_test.go create mode 100644 dynamic/dynamic_test.go create mode 100644 graph/graph_test.go rename math/{doc_test.go => math_test.go} (100%) create mode 100644 other/other_test.go create mode 100644 strings/strings_test.go create mode 100644 structure/structure_test.go diff --git a/cipher/cipher_test.go b/cipher/cipher_test.go new file mode 100644 index 000000000..5a56f3ae3 --- /dev/null +++ b/cipher/cipher_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package cipher diff --git a/conversion/conversion_test.go b/conversion/conversion_test.go new file mode 100644 index 000000000..34eec8f96 --- /dev/null +++ b/conversion/conversion_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package conversion diff --git a/dynamic/dynamic_test.go b/dynamic/dynamic_test.go new file mode 100644 index 000000000..965718118 --- /dev/null +++ b/dynamic/dynamic_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package dynamic diff --git a/graph/graph_test.go b/graph/graph_test.go new file mode 100644 index 000000000..db9fd54ce --- /dev/null +++ b/graph/graph_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package graph diff --git a/math/doc_test.go b/math/math_test.go similarity index 100% rename from math/doc_test.go rename to math/math_test.go diff --git a/other/other_test.go b/other/other_test.go new file mode 100644 index 000000000..35aa59143 --- /dev/null +++ b/other/other_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package other diff --git a/strings/strings_test.go b/strings/strings_test.go new file mode 100644 index 000000000..aebadd648 --- /dev/null +++ b/strings/strings_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package strings diff --git a/structure/structure_test.go b/structure/structure_test.go new file mode 100644 index 000000000..29990a834 --- /dev/null +++ b/structure/structure_test.go @@ -0,0 +1,3 @@ +// Empty test file to keep track of all the tests for the algorithms. + +package structure