'Writing a Python function to check whether a string is pangram or not
def pangram(s):
check=""
small=s.lower()
combine=small.replace(" ","")
for i in combine:
if i in check:
return False
else:
check+=i
return True
print(pangram("The quick brown fox jumps over the lazy dog"))
Note : Pangrams are words or sentences containing every letter of the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"
I can't find out what's wrong with my code, plz help!
Solution 1:[1]
You can use this snippet
import string
def ispangram(sentence, alphabet=string.ascii_lowercase):
return set(alphabet) <= set(sentence.lower())
print(ispangram(input('Sentence: ')))
set(alphabet)
creates a set of all characters of a given alphabet.
set(sentence.lower())
creates a set of all characters of the input sentence in lower case.
The comparison set(alphabet) <= set(sentence.lower()
checks if the characters of the sentence are at least the characters of the alphabet.
Solution 2:[2]
You can use set for it.
A set object is an unordered collection of distinct hashable objects.
import string
def is_pangram(s, alphabet=string.ascii_lowercase):
return set(alphabet) == set(s.replace(" ", "").lower())
text = "The quick brown fox jumps over the lazy dog"
assert is_pangram(text) is True
text = "Some text for testing"
assert is_pangram(text) is False
Solution 3:[3]
import string
alphabet = set(string.ascii_lowercase)
def ispangram(str):
return not set(alphabet) - set(str)
string = 'the quick brown fox jumps over the lazy dog'
if(ispangram(string) == True):
print("Yes")
else:
print("No")
Solution 4:[4]
Your strategy will fail. For each character in the string, "combine", you are checking if you have seen that character before. A pangram can have repeated characters. What you want to do is check that the input string "s" has each character of the alphabet. The following code is simple and fast and demonstrates two very useful tools: string and set.
import string
def is_pangram(s):
s = s.lower().replace(" ","")
return set(s.lower()) >= set(string.ascii_lowercase)
Solution 5:[5]
def is_panagram(sentence):
alp='abcdefghijklmnopqrstuvwxyz' # define a alp with all alphabets(alp)
for letter in alp: # checking every letter is present in alp.
if letter in sentence.lower(): # string.lower to avoid case difference
return True # return only if all alp is present in sentence
return False # return false if it fails
Panagram- Where every word in the alphabets are present in the given sentence.
create a alphabet strings (alp) Get the run-time input with function(sentence) check every letter in alp is present in the given sentence after converting to lower using string.lower() return True,False-- accordingly
I'm new,code in reference to geeksforgeeks.org .. happy to learn
Solution 6:[6]
Here's my contribution.
print("Yes" if len(set(filter(lambda x: x.isalpha(), input().lower()))) == 26 else "No")
Solution 7:[7]
import string
def ispangram(str1, alphabet=string.ascii_lowercase):
list1 = []
characters = str1.split()
x = ''.join(characters) #to remove the spaces from the string
for i in range(len(x)) :
if x[i] in alphabet :
if not x[i] in list1 :
list1.append(x[i])
if len(list1) == len(alphabet):
return True
return False
Solution 8:[8]
import string
import re
def pangram(s):
s = s.lower() # convert the string to lowercase
s = re.compile('[^a-z]').sub('',s) # string after removing non English alphabets
return len(set(s)) == len(string.ascii_lowercase)
I hope this solves your problem.
Here we convert the string to lower case and then eliminates all the unwanted characters. then finds out the unique values and check if the length is same as the number of characters in English alphabet.
Solution 9:[9]
# Using Set
print("Pangram" if len(set([ord(s[i])-ord('a') for i in range(len(s)) if s[i]!=' ']))==26 else "Not Pangram")```
Solution 10:[10]
import re
def pangram(s):
s = s.lower()
s = re.compile('[^a-z]').sub('',s) # string after removing non English alphabets
return len(set(s)) == 26
Try this code. Here we convert the string to lower case and then eliminates all the unwanted characters. then finds out the unique values and check if the length is same as the number of characters in English alphabet.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Matthias |
Solution 2 | |
Solution 3 | Sumana |
Solution 4 | |
Solution 5 | |
Solution 6 | laplace |
Solution 7 | Yahia Naeem |
Solution 8 | Hive minD |
Solution 9 | think-maths |
Solution 10 |