diff --git a/README.md b/README.md index cc96f6fe3..aad7edd56 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. 1. [`IsPalindrome`](./strings/palindrome/ispalindrome.go#L26): No description provided. +--- +
+ pangram + +--- + +##### Functions: + +1. [`IsPangram`](./strings/pangram/ispangram.go#L21): No description provided. + ---
pascal diff --git a/strings/pangram/ispangram.go b/strings/pangram/ispangram.go new file mode 100644 index 000000000..18d9f90b4 --- /dev/null +++ b/strings/pangram/ispangram.go @@ -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 +} diff --git a/strings/pangram/ispangram_test.go b/strings/pangram/ispangram_test.go new file mode 100644 index 000000000..29e48de21 --- /dev/null +++ b/strings/pangram/ispangram_test.go @@ -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) + } + }) + } +}