forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: Add Pangram program (TheAlgorithms#441)
* Add Pangram program * Updated Documentation in README.md Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// ispangram.go | ||
// description: Checks if a given string is pangram or not | ||
// details: A pangram is a sentence or expression that uses all the letters of the alphabet. | ||
// Reference: https://www.geeksforgeeks.org/pangram-checking/ | ||
// Author : Kavitha J | ||
|
||
package pangram | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func cleanString(text string) string { | ||
cleanText := strings.ToLower(text) // Convert to lowercase | ||
cleanText = strings.Join(strings.Fields(cleanText), "") // Remove spaces | ||
regex, _ := regexp.Compile(`[^\p{L}\p{N} ]+`) // Regular expression for alphanumeric only characters | ||
return regex.ReplaceAllString(cleanText, "") | ||
} | ||
|
||
func IsPangram(text string) bool { | ||
cleanText := cleanString(text) | ||
if len(cleanText) < 26 { | ||
return false | ||
} | ||
var data = make(map[rune]bool) | ||
for _, i := range cleanText { | ||
data[i] = true | ||
} | ||
return len(data) == 26 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pangram | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var testCases = []struct { | ||
name string // test description | ||
input string // user input | ||
expected bool // expected return | ||
}{ | ||
{ | ||
"empty string", | ||
"", | ||
false, | ||
}, | ||
{ | ||
"non pangram string without spaces", | ||
"abc", | ||
false, | ||
}, | ||
{ | ||
"non pangram string with spaces", | ||
"Hello World", | ||
false, | ||
}, | ||
{ | ||
"Pangram string 1", | ||
" Abcdefghijklmnopqrstuvwxyz", | ||
true, | ||
}, | ||
{ | ||
"pangram string 2", | ||
"cdefghijklmnopqrstuvwxABC zyb", | ||
true, | ||
}, | ||
{ | ||
"pangram string 3", | ||
"The Quick Brown Fox Jumps Over the Lazy Dog", | ||
true, | ||
}, | ||
} | ||
|
||
func TestIsPangram(t *testing.T) { | ||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
func_result := IsPangram(test.input) | ||
if test.expected != func_result { | ||
t.Errorf("Expected answer '%t' for string '%s' but answer given was %t", test.expected, test.input, func_result) | ||
} | ||
}) | ||
} | ||
} |