Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Length of the string Python

I need to check if the string is Panagram or not. Why I get right solution only when I use set. Why it is not working with string?

def checkIfPangram(sentence):
    return len(set(sentence)) == 26 # answer is True

print(checkIfPangram('thequickbrownfoxjumpsoverthelazydog'))

but for this solution it does not work:

def checkIfPangram(sentence):
    return len(sentence) == 26 #

print(checkIfPangram('thequickbrownfoxjumpsoverthelazydog'))

Answer is not correct. With len(sentence) I get only False, but if I use set(sentence) then solution is correct

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In the first solution, you are using the set data structure to convert the sentence into a set of unique characters. The set data structure only allows unique elements, so by converting the sentence into a set, you are removing any duplicate characters. Then, you are checking if the length of the set is equal to 26, which is the number of unique letters in the English alphabet.

In the second solution, you are simply checking if the length of the sentence is equal to 26. This will only return True if the sentence has exactly 26 characters, regardless of whether those characters are all unique or not. This means that the second solution will not work for checking if a sentence is a pangram, because a pangram can have more than 26 characters if it includes duplicate letters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading