Reputation: 21
I have created a function that returns a boolean if a argument contains every letter in the ascii.lowercase
string (panagram).
within the code, I am using a for loop to find membership of whitespace and punctuation with the string
module methods string.whitespace
and string.punctuation
.
When testing the for loop, the special characters in string.punctuation
portion seems to not be matching the special characters provide from the for loop.
Please provide the reasoning to string.punctuation
not working as planned.
import string
def ispanagram(text, alphabet = string.ascii_lowercase):
"""Return boolean if argument is contains every letter in the ascii alphabet"""
alphabet_list = list(alphabet)
letter_set = sorted(set(text.lower()))
for char in letter_set:
if char in string.whitespace or char in string.punctuation:
letter_set.remove(char)
return letter_set == alphabet_list
ispanagram("The quick brown !fox jumps over the lazy dog")
Upvotes: 1
Views: 320
Reputation: 577
Let me know if this help.
import string
import re
def ispanagram(text, alphabet = string.ascii_lowercase):
"""Return boolean if argument is contains every letter in the ascii alphabet"""
alphabet_list = list(alphabet)
# just remove all the special characters including space
text_only_chars = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?, ]", "", text)
letter_set = sorted(set(text_only_chars.lower()))
return letter_set == alphabet_list
print(ispanagram("The quick brown !fox jumps over the lazy dog"))
#### Output ####
True
Upvotes: 0
Reputation: 500963
The main issue is that you're modifying letter_set
while iterating over it. This does not work as expected (explanation).
To fix, iterate over a copy:
for char in letter_set[:]:
Upvotes: 4