Skip to content

Commit 6adcb36

Browse files
authored
Co-authored-by: Caroline Borg <[email protected]>
1 parent e685f8d commit 6adcb36

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ If you want to uninstall algorithms, it is as simple as:
348348
- [atbash_cipher](algorithms/strings/atbash_cipher.py)
349349
- [longest_palindromic_substring](algorithms/strings/longest_palindromic_substring.py)
350350
- [knuth_morris_pratt](algorithms/strings/knuth_morris_pratt.py)
351+
- [panagram](algorithms/strings/panagram.py)
351352
- [tree](algorithms/tree)
352353
- [bst](algorithms/tree/bst)
353354
- [array_to_bst](algorithms/tree/bst/array_to_bst.py)

algorithms/strings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
from .atbash_cipher import *
3939
from .longest_palindromic_substring import *
4040
from .knuth_morris_pratt import *
41+
from .panagram import *

algorithms/strings/panagram.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Given a string, check whether it is a panagram or not.
3+
4+
A panagram is a sentence that uses every letter at least once.
5+
6+
The most famous example is: "he quick brown fox jumps over the lazy dog.
7+
8+
Note:
9+
A panagram in one language isn't necessarily a panagram in another. This
10+
module assumes the english language. Hence, the Finnish panagram
11+
'Törkylempijävongahdus' won't pass for a panagram despite being considered
12+
a perfect panagram in its language. However, the Swedish panagram
13+
'Yxmördaren Julia Blomqvist på fäktning i Schweiz' will pass despite
14+
including letters not used in the english alphabet. This is because the
15+
Swedish alphabet only extends the Latin one.
16+
"""
17+
18+
from string import ascii_lowercase
19+
20+
def panagram(string):
21+
"""
22+
Returns whether the input string is an English panagram or not.
23+
24+
Parameters:
25+
string (str): A sentence in the form of a string.
26+
27+
Returns:
28+
A boolean with the result.
29+
"""
30+
letters = set(ascii_lowercase)
31+
for c in string:
32+
try:
33+
letters.remove(c.lower())
34+
except:
35+
pass
36+
return len(letters) == 0

tests/test_strings.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
repeat_substring,
4141
atbash,
4242
longest_palindrome,
43-
knuth_morris_pratt
43+
knuth_morris_pratt,
44+
panagram
4445
)
4546

4647
import unittest
@@ -572,6 +573,84 @@ def test_knuth_morris_pratt(self):
572573
self.assertEqual([0, 4], knuth_morris_pratt("abcdabc", "abc"))
573574
self.assertEqual([], knuth_morris_pratt("aabcdaab", "aba"))
574575

576+
class TestPanagram(unittest.TestCase):
577+
"""[summary]
578+
Test for the file panagram.py
579+
580+
Arguments:
581+
unittest {[type]} -- [description]
582+
"""
583+
584+
def test_empty_string(self):
585+
# Arrange
586+
string = ""
587+
588+
# Act
589+
res = panagram(string)
590+
591+
# Assert
592+
self.assertEqual(False, res)
593+
594+
def test_single_word_non_panagram(self):
595+
# Arrange
596+
string = "sentence"
597+
598+
# Act
599+
res = panagram(string)
600+
601+
# Assert
602+
self.assertEqual(False, res)
603+
604+
def test_fox_panagram_no_spaces(self):
605+
# Arrange
606+
string = "thequickbrownfoxjumpsoverthelazydog"
607+
608+
# Act
609+
res = panagram(string)
610+
611+
# Assert
612+
self.assertEqual(True, res)
613+
614+
def test_fox_panagram_mixed_case(self):
615+
# Arrange
616+
string = "theqUiCkbrOwnfOxjUMPSOVErThELAzYDog"
617+
618+
# Act
619+
res = panagram(string)
620+
621+
# Assert
622+
self.assertEqual(True, res)
623+
624+
def test_whitespace_punctuation(self):
625+
# Arrange
626+
string = "\n\t\r,.-_!?"
627+
628+
# Act
629+
res = panagram(string)
630+
631+
# Assert
632+
self.assertEqual(False, res)
633+
634+
def test_fox_panagram(self):
635+
# Arrange
636+
string = "the quick brown fox jumps over the lazy dog"
637+
638+
# Act
639+
res = panagram(string)
640+
641+
# Assert
642+
self.assertEqual(True, res)
643+
644+
def test_swedish_panagram(self):
645+
# Arrange
646+
string = "Yxmördaren Julia Blomqvist på fäktning i Schweiz"
647+
648+
# Act
649+
res = panagram(string)
650+
651+
# Assert
652+
self.assertEqual(True, res)
653+
575654

576655
if __name__ == "__main__":
577656
unittest.main()

0 commit comments

Comments
 (0)