-4
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!

  • 3
    My approach is to write down what steps to take in a human-understandable way. Then, when that is done, you can write the code that does this. This makes the transition between the intention and the implementation in code much easier for a beginner. BTW: As a new user here, take the [tour] and read [ask]. – Ulrich Eckhardt Dec 07 '19 at 10:09
  • 1
    Why do you think there is something wrong with the code? – mkrieger1 Dec 07 '19 at 10:15
  • what do you think your code is doing exactly? step by step? What part of it would you say takes care of the requirement "every letter of the alphabet"? how about the part that says "at least once"? – njzk2 Jan 17 '20 at 07:05

12 Answers12

3

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.

Matthias
  • 12,873
  • 6
  • 42
  • 48
Tr4cEr
  • 47
  • 4
  • 3
    Python uses four spaces of indentation. Other than that, this is a very concise and good approach. An explanation would help a beginner though. – Ulrich Eckhardt Dec 07 '19 at 10:21
  • 2
    Best solution until now, but as @UlrichEckhardt said: please add some explanation. – Matthias Dec 07 '19 at 10:46
  • @UlrichEckhardt Thanks for pointing out the Indentation mistake i updated the code and also added some explanation as you mentioned . Please point out if any changes are needed to be made as i am an Newbie over here and this is my first answer – Tr4cEr Dec 07 '19 at 11:08
  • This depends on your definition of "pangram". Is `aaa bbb ccc...zzz` a pangram? – georg Dec 07 '19 at 11:10
  • @georg this program checks the whole set meaning every character needs to present in the sentence set for it to return true ... you can try and execute the code with 26 a letter – Tr4cEr Dec 07 '19 at 11:15
0

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
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • Put a period after the sentence like it's supposed to be done and that algorithm fails. Funny how even a seasoned programmer copy'n'pastes that bug from the original question. – Ulrich Eckhardt Dec 07 '19 at 10:24
0

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)
0
    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") 
Sumana
  • 17
  • 2
  • In Python, you don't put brackets after `if`. Further, the `== True` compares a boolean to a boolean and returns a boolean. Don't do that. – Ulrich Eckhardt Dec 08 '19 at 12:22
0
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

  • Please add some explanation so other can understand ! – Bhavik Hirani Jan 17 '20 at 07:10
  • Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers – borchvm Jan 17 '20 at 07:13
  • Thanks a lot for the information, I've made the changes and gave an explanation. Do advice if i need to develop more. – Mooventh Chiyan Jan 18 '20 at 08:05
0

Here's my contribution.

print("Yes" if len(set(filter(lambda x: x.isalpha(), input().lower()))) == 26 else "No")
laplace
  • 656
  • 7
  • 15
0
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
Yahia Naeem
  • 63
  • 1
  • 2
  • 7
0

The check for equality would work as essentially what you are checking is whether all letters from the alphabet are found in the string. No matter whether you have got duplicates.

  def isPangram(text, alphabet = string.ascii_lowercase):
      modified = text.replace(" ", "").lower()
      return set(alphabet) == set(modified)
Denise Ignatova
  • 465
  • 4
  • 7
0

Try this:

import string
def is_pangram(sentence):
    return set(string.ascii_lowercase) <= set(sentence.lower())
user16217248
  • 3,119
  • 19
  • 19
  • 37
-1
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.

Hive minD
  • 11
  • 6
-1
# 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")```
think-maths
  • 917
  • 2
  • 10
  • 28
-2
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.

Hive minD
  • 11
  • 6
  • @UlrichEckhardt This function is able to handle the characters and numbers which comes in the string. And please do tell me the issues in this code so that i can improve. – Hive minD Dec 07 '19 at 10:29
  • I see two things: One is the use of so-called "magic numbers", the other is the redundant stripping of spaces. BTW: There's codereview.stackexchange.com, which may be a better place for such a discussion. Anyhow, one more simplification: Use `97 <= ord(i) <= 122` as condition inside the loop. It does the right thing in Python. – Ulrich Eckhardt Dec 07 '19 at 10:33