Skip to content

Commit

Permalink
merge: Add Pangram program (TheAlgorithms#441)
Browse files Browse the repository at this point in the history
* Add Pangram program

* Updated Documentation in README.md

Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
kavithajm and github-action authored Nov 17, 2021
1 parent 28b4df5 commit e9ba9c4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

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

---
</details><details>
<summary> <strong> pangram </strong> </summary>

---

##### Functions:

1. [`IsPangram`](./strings/pangram/ispangram.go#L21): No description provided.

---
</details><details>
<summary> <strong> pascal </strong> </summary>
Expand Down
31 changes: 31 additions & 0 deletions strings/pangram/ispangram.go
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
}
53 changes: 53 additions & 0 deletions strings/pangram/ispangram_test.go
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)
}
})
}
}

0 comments on commit e9ba9c4

Please sign in to comment.