Skip to content

Commit

Permalink
fix: docs (TheAlgorithms#445)
Browse files Browse the repository at this point in the history
Co-authored-by: Rak Laptudirm <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 22, 2021
1 parent 6fd8a07 commit 385fe84
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 47 deletions.
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
1. [`BitCounter`](./math/binary/bitcounter.go#L11): BitCounter - The function returns the number of set bits for an unsigned integer number
2. [`IsPowerOfTwo`](./math/binary/checkisnumberpoweroftwo.go#L19): IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function: 10...0 & 01...1 00...0 -> 0 This is also true for 0, which is not a power of 2, for which we have to add and extra condition.
3. [`IsPowerOfTwoLeftShift`](./math/binary/checkisnumberpoweroftwo.go#L26): IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2
4. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L11): No description provided.
5. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L15): No description provided.
4. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L12): MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operations
5. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L17): MeanUsingRightShift This function finds arithmetic mean using right shift
6. [`ReverseBits`](./math/binary/reversebits.go#L14): ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result and finally move on to the next bit in the input number. Repeat this till all its bits are processed.
7. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L10): No description provided.
7. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L11): XorSearchMissingNumber This function finds a missing number in a sequence

---
</details><details>
Expand Down Expand Up @@ -146,7 +146,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

##### Functions:

1. [`CatalanNumber`](./math/catalan/catalannumber.go#L15): No description provided.
1. [`CatalanNumber`](./math/catalan/catalannumber.go#L16): CatalanNumber This function returns the `nth` Catalan number

---
</details><details>
Expand Down Expand Up @@ -197,13 +197,15 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
---
##### Functions:

1. [`BinaryToDecimal`](./conversion/binarytodecimal.go#L25): BinaryToDecimal() function that will take Binary number as string, and return it's Decimal equivalent as integer.
2. [`DecimalToBinary`](./conversion/decimaltobinary.go#L32): DecimalToBinary() function that will take Decimal number as int, and return it's Binary equivalent as string.
3. [`HEXToRGB`](./conversion/rgbhex.go#L10): HEXToRGB splits an RGB input (e.g. a color in hex format; 0x<color-code>) into the individual components: red, green and blue
4. [`IntToRoman`](./conversion/integertoroman.go#L17): IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.
5. [`RGBToHEX`](./conversion/rgbhex.go#L41): RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
6. [`Reverse`](./conversion/decimaltobinary.go#L22): Reverse() function that will take string, and returns the reverse of that string.
7. [`RomanToInteger`](./conversion/romantointeger.go#L40): RomanToInteger converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.
1. [`Base64Decode`](./conversion/base64.go#L57): Base64Decode decodes the received input base64 string into a byte slice. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4
2. [`Base64Encode`](./conversion/base64.go#L19): Base64Encode encodes the received input bytes slice into a base64 string. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4
3. [`BinaryToDecimal`](./conversion/binarytodecimal.go#L25): BinaryToDecimal() function that will take Binary number as string, and return it's Decimal equivalent as integer.
4. [`DecimalToBinary`](./conversion/decimaltobinary.go#L32): DecimalToBinary() function that will take Decimal number as int, and return it's Binary equivalent as string.
5. [`HEXToRGB`](./conversion/rgbhex.go#L10): HEXToRGB splits an RGB input (e.g. a color in hex format; 0x<color-code>) into the individual components: red, green and blue
6. [`IntToRoman`](./conversion/integertoroman.go#L17): IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.
7. [`RGBToHEX`](./conversion/rgbhex.go#L41): RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
8. [`Reverse`](./conversion/decimaltobinary.go#L22): Reverse() function that will take string, and returns the reverse of that string.
9. [`RomanToInteger`](./conversion/romantointeger.go#L40): RomanToInteger converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.

---
</details><details>
Expand Down Expand Up @@ -272,9 +274,9 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
---
##### Functions:

1. [`BruteForceFactorial`](./math/factorial/factorial.go#L11): No description provided.
2. [`CalculateFactorialUseTree`](./math/factorial/factorial.go#L27): No description provided.
3. [`RecursiveFactorial`](./math/factorial/factorial.go#L19): No description provided.
1. [`Iterative`](./math/factorial/factorial.go#L12): Iterative returns the iteratively brute forced factorial of n
2. [`Recursive`](./math/factorial/factorial.go#L21): Recursive This function recursively computes the factorial of a number
3. [`UsingTree`](./math/factorial/factorial.go#L30): UsingTree This function finds the factorial of a number using a binary tree

---
</details><details>
Expand Down Expand Up @@ -505,7 +507,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

##### Functions:

1. [`BitwiseMax`](./math/max/bitwisemax.go#L10): No description provided.
1. [`Bitwise`](./math/max/bitwisemax.go#L11): Bitwise computes using bitwise operator the maximum of all the integer input and returns it
2. [`Int`](./math/max/max.go#L4): Int is a function which returns the maximum of all the integers provided as arguments.

---
Expand All @@ -529,7 +531,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

##### Functions:

1. [`Bitwise`](./math/min/bitwisemin.go#L10): No description provided.
1. [`Bitwise`](./math/min/bitwisemin.go#L11): Bitwise This function returns the minimum integer using bit operations
2. [`Int`](./math/min/min.go#L4): Int is a function which returns the minimum of all the integers provided as arguments.

---
Expand Down
2 changes: 2 additions & 0 deletions math/binary/arithmeticmean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
// Package binary describes algorithms that use binary operations for different calculations.
package binary

// MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operations
func MeanUsingAndXor(a int, b int) int {
return ((a ^ b) >> 1) + (a & b)
}

// MeanUsingRightShift This function finds arithmetic mean using right shift
func MeanUsingRightShift(a int, b int) int {
return (a + b) >> 1
}
1 change: 1 addition & 0 deletions math/binary/xorsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package binary

// XorSearchMissingNumber This function finds a missing number in a sequence
func XorSearchMissingNumber(a []int) int {
n := len(a)
result := len(a)
Expand Down
3 changes: 2 additions & 1 deletion math/catalan/catalannumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
f "github.com/TheAlgorithms/Go/math/factorial"
)

// CatalanNumber This function returns the `nth` Catalan number
func CatalanNumber(n int) int {
return f.BruteForceFactorial(n*2) / (f.BruteForceFactorial(n) * f.BruteForceFactorial(n+1))
return f.Iterative(n*2) / (f.Iterative(n) * f.Iterative(n+1))
}
11 changes: 7 additions & 4 deletions math/factorial/factorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@
// Package factorial describes algorithms Factorials calculations.
package factorial

func BruteForceFactorial(n int) int {
// Iterative returns the iteratively brute forced factorial of n
func Iterative(n int) int {
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result
}

func RecursiveFactorial(n int) int {
// Recursive This function recursively computes the factorial of a number
func Recursive(n int) int {
if n == 1 {
return 1
} else {
return n * RecursiveFactorial(n-1)
return n * Recursive(n-1)
}
}

func CalculateFactorialUseTree(n int) int {
// UsingTree This function finds the factorial of a number using a binary tree
func UsingTree(n int) int {
if n < 0 {
return 0
}
Expand Down
12 changes: 6 additions & 6 deletions math/factorial/factorial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBruteForceFactorial(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := BruteForceFactorial(tv.n)
result := Iterative(tv.n)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
Expand All @@ -42,7 +42,7 @@ func TestRecursiveFactorial(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := RecursiveFactorial(tv.n)
result := Recursive(tv.n)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
Expand All @@ -54,7 +54,7 @@ func TestCalculateFactorialUseTree(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := CalculateFactorialUseTree(tv.n)
result := UsingTree(tv.n)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
Expand All @@ -64,18 +64,18 @@ func TestCalculateFactorialUseTree(t *testing.T) {

func BenchmarkBruteForceFactorial(b *testing.B) {
for i := 0; i < b.N; i++ {
BruteForceFactorial(10)
Iterative(10)
}
}

func BenchmarkRecursiveFactorial(b *testing.B) {
for i := 0; i < b.N; i++ {
RecursiveFactorial(10)
Recursive(10)
}
}

func BenchmarkCalculateFactorialUseTree(b *testing.B) {
for i := 0; i < b.N; i++ {
RecursiveFactorial(10)
Recursive(10)
}
}
3 changes: 2 additions & 1 deletion math/max/bitwisemax.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

package max

func BitwiseMax(a int, b int, base int) int {
// Bitwise computes using bitwise operator the maximum of all the integer input and returns it
func Bitwise(a int, b int, base int) int {
z := a - b
i := (z >> base) & 1
return a - (i * z)
Expand Down
38 changes: 19 additions & 19 deletions math/max/bitwisemax_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// bitwiseMax_test.go
// description: Test for BitwiseMax
// description: Test for Bitwise
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitwiseMax.go

Expand All @@ -11,67 +11,67 @@ func TestBitwiseMax(t *testing.T) {
base32 := 31

t.Run("Testing(32bit) a = 32 and m = 64: ", func(t *testing.T) {
max := BitwiseMax(32, 64, base32)
max := Bitwise(32, 64, base32)
if max != 64 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(32bit) a = 1024 and m = -9: ", func(t *testing.T) {
max := BitwiseMax(1024, -9, base32)
max := Bitwise(1024, -9, base32)
if max != 1024 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(32bit) a = -6 and m = -6: ", func(t *testing.T) {
max := BitwiseMax(-6, -6, base32)
max := Bitwise(-6, -6, base32)
if max != -6 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(32bit) a = -72 and m = -73: ", func(t *testing.T) {
max := BitwiseMax(-72, -73, base32)
max := Bitwise(-72, -73, base32)
if max != -72 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

base64 := 63
t.Run("Testing(64bit) a = 32 and m = 9223372036854775807: ", func(t *testing.T) {
max := BitwiseMax(32, 9223372036854775807, base64)
max := Bitwise(32, 9223372036854775807, base64)
if max != 9223372036854775807 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(64bit) a = 1024 and m = -9223372036854770001: ", func(t *testing.T) {
max := BitwiseMax(1024, -9223372036854770001, base64)
max := Bitwise(1024, -9223372036854770001, base64)
if max != 1024 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(64bit) a = -6 and m = -6: ", func(t *testing.T) {
max := BitwiseMax(-6, -6, base64)
max := Bitwise(-6, -6, base64)
if max != -6 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

t.Run("Testing(64bit) a = -4223372036854775809 and m = -4223372036854775808: ", func(t *testing.T) {
max := BitwiseMax(-4223372036854775809, -4223372036854775808, base64)
max := Bitwise(-4223372036854775809, -4223372036854775808, base64)
if max != -4223372036854775808 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})

base8 := 7
t.Run("Testing(8bit) a = 257 and m = 256: ", func(t *testing.T) {
max := BitwiseMax(8, 16, base8)
max := Bitwise(8, 16, base8)
if max != 16 {
t.Fatalf("Error: BitwiseMax returned bad value")
t.Fatalf("Error: Bitwise returned bad value")
}
})
}
1 change: 1 addition & 0 deletions math/min/bitwisemin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package min

// Bitwise This function returns the minimum integer using bit operations
func Bitwise(base int, value int, values ...int) int {
min := value
for _, val := range values {
Expand Down

0 comments on commit 385fe84

Please sign in to comment.