How to check if a string is pangram?

I want to create a function that takes a string as input and checks if the string is pangram or not (pangram is a piece of text that contains every letter of the alphabet).

I wrote the following code that works, but I am looking for an alternative way to do this, I hope, briefly.

import string def is_pangram (gram): gram = gram.lower() gram_list_old = sorted([c for c in gram if c != ' ']) gram_list = [] for c in gram_list_old: if c not in gram_list: gram_list.append(c) if gram_list == list(string.ascii_lowercase): return True else: return False 

I feel this question may be against the rules of this website, but hopefully this is not the case. I am just curious and would like to see alternative ways to do this.

+7
source share
17 answers
 is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower()) >>> is_pangram('abc') False >>> is_pangram('the quick brown fox jumps over the lazy dog') True >>> is_pangram('Does the quick brown fox jump over the lazy dog?') True >>> is_pangram('Do big jackdaws love my sphinx of quartz?') True 

The test string s is a pangram, if we start with the alphabet, delete every letter found in the test string, and all letters of the alphabet are deleted.

Explanation

Using lambda is a way to create a function, so one line is equivalent to writing def like:

  def is_pangram(s): return not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower()) 

set() creates a data structure in which there can be no duplicates, and here:

  • The first set is the (English) letters of the alphabet, lowercase
  • The second set is the characters from the test string, also in lower case. And all the duplicates also disappeared.

Subtracting things like set(..) - set(..) returns the contents of the first set, minus the contents of the second set. set('abcde') - set('ace') == set('bd') .

In this pangram test:

  • we take the characters in the test string from the alphabet
  • If nothing is left, then the test line contains all the letters of the alphabet and should be pangram.
  • If something remains, then the test line does not contain all the letters of the alphabet, so it should not be a pangram.

  • any spaces, punctuation marks from the set of test strings were never in the alphabetical set, so they do not matter.

set(..) - set(..) will return an empty set or a set with content. If we insert sets into the simplest True / False values ​​in Python, then containers with the contents of "True" and empty containers of "False".

So, we use not to check "is there anything left?" forcing the result to True / False, depending on whether there are any leftovers or not.

not also changes True β†’ False, and False β†’ True. This is useful here because (the alphabet used) is β†’ an empty set that is False , but we want is_pangram return True in this case. Conversely, (the alphabet has some leftovers) -> a set of letters that is True , but we want is_pangram return False for this.

Then return the result True / False.

 is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower()) # Test string `s` #is a pangram if # the alphabet letters # minus # the test string letters # has NO leftovers 
+13
source

You can use something simple like:

 import string is_pangram = lambda s: all(c in s.lower() for c in string.ascii_lowercase) 
+5
source

Great membership testing kits:

 >>> import string >>> candidate = 'ammdjri * itouwpo ql ? k @ finvmcxzkasjdhgfytuiopqowit' >>> ascii_lower = set(string.ascii_lowercase) 

Separate the space and punctuation from the candidate, then check:

 >>> candidate_lower = ascii_lower.intersection(candidate.lower()) >>> ascii_lower == candidate_lower False 

Find out what is missing:

 >>> ascii_lower.symmetric_difference(candidate_lower) set(['b', 'e']) 

Try again, but add the missing letters:

 >>> candidate = candidate + 'be' >>> candidate_lower = ascii_lower.intersection(candidate.lower()) >>> ascii_lower == candidate_lower True >>> 
+3
source
  def pangram(word): return all(chr(c+97) in word for c in range(25)) 
+2
source

How to simply check if each of the lower alphabet is in a sentence:

 text = input() s = set(text.lower()) if sum(1 for c in s if 96 < ord(c) < 123) == 26: print ('pangram') else: print ('not pangram') 

or in function:

 def ispangram(text): return sum(1 for c in set(text.lower()) if 96 < ord(c) < 123) == 26 
+1
source

Here is another definition:

 def is_pangram(s): return len(set(s.lower().replace(" ", ""))) == 26 
+1
source

I see that this thread is a little old, but I thought that I would give up my decision anyway.

 import string def panagram(phrase): new_phrase=sorted(phrase.lower()) phrase_letters = "" for index in new_phrase: for letter in string.ascii_lowercase: if index == letter and index not in phrase_letters: phrase_letters+=letter print len(phrase_letters) == 26 

or for the last line:

  print phrase_letters == string.ascii_lowercase 
0
source

Using ASCII aproach:

 def checkPan(s): alphaT= sum([i for i in range(97,123)]) + 32 s = [i.lower() for i in s] s = list(set(s)) sn = sum([ord(i) for i in s]) if sn==alphaT: return("pangram") else: return("not pangram") 
0
source
 def panagram(phrase): alphabet="abcdefghiklmnopqrstuvwxyz" pharseletter="" for char in phrase: if char in aphabet: phraseletter= phraseletter + char for char in aplhabet: if char not in phrase: return false 
0
source
 import string def ispangram(str, alphabet=string.ascii_lowercase): alphabet = set(alphabet) return alphabet <= set(str.lower()) 

or easier way

 def ispangram(str): return len(set(str.lower().replace(" ", ""))) == 26 
0
source
 import string def is_pangram(phrase, alpha=string.ascii_lowercase): num = len(alpha) count=0 for i in alpha: if i in phrase: count += 1 return count == num 
0
source
 def panagram(str1): str1=str1.replace(' ','').lower() s=set(str1) l=list(s) if len(l)==26: return True return False str1='The quick brown fox jumps over the dog' q=panagram(str1) print(q) 

true

0
source
 import string def ispangram(str1,alphabet=string.ascii.lowercase): for myalphabet in alphabet: if myalphabet not in str1: print(it not pangram) break else: print(it pangram) 

Run the command:

 ispangram("The quick brown fox jumps over the lazy dog") 

Conclusion: "this is a pangram." Hint: string.ascii_lowercase returns output

 abcdefghijklmnopqrstuvwxyz 
0
source

I came up with the simplest and not using a program module.

 def checking(str_word): b=[] for i in str_word: if i not in b: b.append(i) b.sort() #print(b) #print(len(set(b))) if(len(set(b))>=26): print(b) print(len(b)) print(" String is pangram .") else: print(" String isn't pangram .") #b.sort() #print(b) str_word=input(" Enter the String :") checking(str_word) 
0
source

I am new to Python and coding. and I made my code to check the pangram, and the person who received the error can check what is wrong in my code. thank you in advance

 import string def ispangram(s): al=string.ascii_lowercase q=0 k=[] s.lower() k=list(s) e=[] e=list(al) x=0 for x in k: if k[x] in e and q<27: q=q+1 elif q==26: return True ispangram("The quick brown fox jumps over the lazy dog") 
0
source
 import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { String s; char f; Scanner in = new Scanner(System.in); s = in.nextLine(); char[] charArray = s.toLowerCase().toCharArray(); final Set set = new HashSet(); for (char a : charArray) { if ((int) a >= 97 && (int) a <= 122) { f = a; set.add(f); } } if (set.size() == 26){ System.out.println("pangram"); } else { System.out.println("not pangram"); } } } 
-1
source
 import string import re list_lower= list(string.lowercase); list_upper=list(string.uppercase); list_total=list_lower + list_upper ; def is_panagram(temp): for each in temp: if each not in list_total : return 'true' sample=raw_input("entre the string\n"); string2=re.sub('[^A-Za-z0-9]+', '', sample) ram=is_panagram(string2); if ram =='true': print "sentence is not a panagram" else:`enter code here` print"sentece is a panagram" 
-1
source

Source: https://habr.com/ru/post/972283/


All Articles