patternpythonModerate
Checking if a string is a pangram
Viewed 0 times
pangramcheckingstring
Problem
This checker works perfectly but I have a strong feeling that I could make this script much more compliant with the "Zen of Python", especially having read a book about it recently. There is no doubt that I made this script much longer than needed, so if you see something I can shorten, please say so.
#!/usr/bin/env python
myPhrase = "The quick brown fox jumps over the lazy dog"
def is_pangram(phrase):
c = 0
alphabet = "abcdefghijklmnopqrstuvwxyz"
phraseLetters = ""
for char in phrase:
for letter in alphabet:
if char == letter and char not in phraseLetters:
phraseLetters += char
for char in phraseLetters:
for letter in alphabet:
if char == letter:
c += 1
if c == 26:
return True
else:
print phraseLetters, alphabet
return False
print is_pangram(myPhrase)Solution
This can be improved and made more Pythonic. First, there is the
You will have to change the logic slightly:
I iterate over the letters in
This can be simplified even further if you are not interested in printing the letters that are in the phrase. I would recommend cutting it down even further to this:
This way, you can check whether the phrase is a pangram without always printing to the screen, which is good for re-using the code later; you can then write another method to print the strings in the phrase and the alphabet. Additionally, I would recommend printing the letters in
Edit:
Thanks to matsjoyce in the comments, you can simplify it even further to this:
This way, you can even return the letters not in
Additionally, you should probably name
in and not in keyword, which you already know of as you used it. Why not utilize that instead of doing two loops?You will have to change the logic slightly:
def is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
phraseLetters = ""
for char in phrase:
if char in alphabet:
phraseLetters += char
for char in alphabet:
if char not in phrase:
print phraseLetters, alphabet
return False
return TrueI iterate over the letters in
phrase, checking if they are in the alphabet, and add them to the phraseLetters string. Then, I iterate over the alphabet and check whether each character is in the phrase.This can be simplified even further if you are not interested in printing the letters that are in the phrase. I would recommend cutting it down even further to this:
def is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in alphabet:
if char not in phrase:
return False
return TrueThis way, you can check whether the phrase is a pangram without always printing to the screen, which is good for re-using the code later; you can then write another method to print the strings in the phrase and the alphabet. Additionally, I would recommend printing the letters in
phrase alphabetically so it is easy to see which letter is not in phrase, which will also make it easier for any potential debugging you have to do.Edit:
Thanks to matsjoyce in the comments, you can simplify it even further to this:
def is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
return not (set(alphabet) - set(phrase))This way, you can even return the letters not in
phrase for ease of reference by just removing the not keyword, but you would also have to change the name of the method as is_something implies a boolean value being returned.Additionally, you should probably name
alphabet in all caps to signify it is a constant variable.Code Snippets
def is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
phraseLetters = ""
for char in phrase:
if char in alphabet:
phraseLetters += char
for char in alphabet:
if char not in phrase:
print phraseLetters, alphabet
return False
return Truedef is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in alphabet:
if char not in phrase:
return False
return Truedef is_pangram(phrase):
alphabet = "abcdefghijklmnopqrstuvwxyz"
return not (set(alphabet) - set(phrase))Context
StackExchange Code Review Q#84051, answer score: 12
Revisions (0)
No revisions yet.