Skip to content

Commit 4047440

Browse files
authored
Merge branch 'master' into base64
2 parents 312acb9 + 9fec515 commit 4047440

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
7979
---
8080
##### Functions:
8181

82-
1. [`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.
83-
2. [`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
84-
3. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L11): No description provided.
85-
4. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L15): No description provided.
86-
5. [`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.
87-
6. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L10): No description provided.
82+
1. [`BitCounter`](./math/binary/bitcounter.go#L11): BitCounter - The function returns the number of set bits for an unsigned integer number
83+
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.
84+
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
85+
4. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L11): No description provided.
86+
5. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L15): No description provided.
87+
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.
88+
7. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L10): No description provided.
8889

8990
---
9091
</details><details>
@@ -566,6 +567,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
566567

567568
1. [`IsPalindrome`](./strings/palindrome/ispalindrome.go#L26): No description provided.
568569

570+
---
571+
</details><details>
572+
<summary> <strong> pangram </strong> </summary>
573+
574+
---
575+
576+
##### Functions:
577+
578+
1. [`IsPangram`](./strings/pangram/ispangram.go#L21): No description provided.
579+
569580
---
570581
</details><details>
571582
<summary> <strong> pascal </strong> </summary>

math/binary/bitcounter.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// bitcounter.go
2+
// description: Counts the number of set bits in a number
3+
// details:
4+
// For unsigned integer number N, return the number of bits set to 1 - [Bit numbering](https://en.wikipedia.org/wiki/Bit_numbering)
5+
// author(s) [red_byte](https://github.com/i-redbyte)
6+
// see bitcounter_test.go
7+
8+
package binary
9+
10+
// BitCounter - The function returns the number of set bits for an unsigned integer number
11+
func BitCounter(n uint) int {
12+
counter := 0
13+
for n != 0 {
14+
if n&1 == 1 {
15+
counter++
16+
}
17+
n >>= 1
18+
}
19+
return counter
20+
}

math/binary/bitcounter_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// bitcounter_test.go
2+
// description: Test for counts the number of set bits in a number
3+
// author(s) [red_byte](https://github.com/i-redbyte)
4+
// see bitcounter.go
5+
6+
package binary
7+
8+
import "testing"
9+
10+
func TestBitCounter(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
number uint
14+
want int
15+
}{
16+
{"Number of bits in a number 0", 0, 0},
17+
{"Number of bits in a number 1", 1, 1},
18+
{"Number of bits in a number 255", 255, 8},
19+
{"Number of bits in a number 7", 7, 3},
20+
{"Number of bits in a number 8", 8, 1},
21+
{"Number of bits in a number 9223372036854775807", 9223372036854775807, 63},
22+
{"Number of bits in a number 2147483647", 2147483647, 31},
23+
{"Number of bits in a number 15", 15, 4},
24+
{"Number of bits in a number 16", 16, 1},
25+
}
26+
for _, test := range tests {
27+
t.Run(test.name, func(t *testing.T) {
28+
if got := BitCounter(test.number); got != test.want {
29+
t.Errorf("BitCounter() = %v, want %v", got, test.want)
30+
}
31+
})
32+
}
33+
}

strings/pangram/ispangram.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ispangram.go
2+
// description: Checks if a given string is pangram or not
3+
// details: A pangram is a sentence or expression that uses all the letters of the alphabet.
4+
// Reference: https://www.geeksforgeeks.org/pangram-checking/
5+
// Author : Kavitha J
6+
7+
package pangram
8+
9+
import (
10+
"regexp"
11+
"strings"
12+
)
13+
14+
func cleanString(text string) string {
15+
cleanText := strings.ToLower(text) // Convert to lowercase
16+
cleanText = strings.Join(strings.Fields(cleanText), "") // Remove spaces
17+
regex, _ := regexp.Compile(`[^\p{L}\p{N} ]+`) // Regular expression for alphanumeric only characters
18+
return regex.ReplaceAllString(cleanText, "")
19+
}
20+
21+
func IsPangram(text string) bool {
22+
cleanText := cleanString(text)
23+
if len(cleanText) < 26 {
24+
return false
25+
}
26+
var data = make(map[rune]bool)
27+
for _, i := range cleanText {
28+
data[i] = true
29+
}
30+
return len(data) == 26
31+
}

strings/pangram/ispangram_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package pangram
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var testCases = []struct {
8+
name string // test description
9+
input string // user input
10+
expected bool // expected return
11+
}{
12+
{
13+
"empty string",
14+
"",
15+
false,
16+
},
17+
{
18+
"non pangram string without spaces",
19+
"abc",
20+
false,
21+
},
22+
{
23+
"non pangram string with spaces",
24+
"Hello World",
25+
false,
26+
},
27+
{
28+
"Pangram string 1",
29+
" Abcdefghijklmnopqrstuvwxyz",
30+
true,
31+
},
32+
{
33+
"pangram string 2",
34+
"cdefghijklmnopqrstuvwxABC zyb",
35+
true,
36+
},
37+
{
38+
"pangram string 3",
39+
"The Quick Brown Fox Jumps Over the Lazy Dog",
40+
true,
41+
},
42+
}
43+
44+
func TestIsPangram(t *testing.T) {
45+
for _, test := range testCases {
46+
t.Run(test.name, func(t *testing.T) {
47+
func_result := IsPangram(test.input)
48+
if test.expected != func_result {
49+
t.Errorf("Expected answer '%t' for string '%s' but answer given was %t", test.expected, test.input, func_result)
50+
}
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)