2

I am trying to write a python program that checks if a given string is a pangram - contains all letters of the alphabet.

Therefore, "We promptly judged antique ivory buckles for the next prize" should return True while any string that does not contain every letter of the alphabet at least once should return False.

I believe I should be using RegEx for this one, but I'm not sure how. It should look similar to this:

import sys
import re

input_string_array = sys.stdin.readlines()
input_string = input_string_array[0]

if (re.search('string contains every letter of the alphabet',input_string):
    return True
else:
    return False
rypel
  • 4,686
  • 2
  • 25
  • 36
Maslor
  • 1,821
  • 3
  • 20
  • 44

7 Answers7

12

This is not something I'd solve with a regular expression, no. Create a set of the lowercased string and check if it is a superset of the letters of the alphabet:

import string

alphabet = set(string.ascii_lowercase)

def ispangram(input_string):
    return set(input_string.lower()) >= alphabet

Only if every letter of the alphabet is in the set created from the input text will it be a superset; by using a superset and not equality, you allow for punctuation, digits and whitespace, in addition to the (ASCII) letters.

Demo:

>>> import string
>>> alphabet = set(string.ascii_lowercase)
>>> input_string = 'We promptly judged antique ivory buckles for the next prize'
>>> set(input_string.lower()) >= alphabet
True
>>> set(input_string[:15].lower()) >= alphabet
False
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

This is my solution in python:

alphabet = "abcdefghijklmnopqrstuvwxyz"
sentence = input()
sentence = sentence.lower()
missing = ''
for letter in alphabet:
  if letter not in sentence:
    missing = missing+letter
if (len(missing) != 0):
  print("missing", missing)
else:
  print("pangram")
jeonbik
  • 11
  • 1
0

You dont need regex. What you want can be done in two lines with good space efficiency.

ms = itertools.chain(range(ord("a"),ord("z")),range(ord("A"),ord("Z")))
flag = all(chr(o) in string for o in ms)

That's it. string is the string you want to check. flag will be either True or False depending on if all chars are in string or not.

C Panda
  • 3,297
  • 2
  • 11
  • 11
0

A pangram is a function that contains at least each letter of the alphabet. I have tried in this way:

def pangram():
n = str(input('give me a word to check if it is a pangram:\n'))
n = n.lower()
n = n.replace(' ','')
if not isinstance(n, str):
    return n, False
elif set(n) >= set('abcdefghijklmnopqrstuvxywz'):
    return n, True
else: 
    return n, False

The function isinstance(n, str) checks if n is a string. The function set() gives us a set. For example set('penny') returns {'y', 'e', 'p', 'n'}... as you see it is a set without the repeated letters.

  • This is an incorrect solution. For example, it will report that a string consisting of 26 question mark characters is a pangram. While a pangram has to consist of at least 26 characters, not all strings of 26 characters are pangrams. – Stephen C Apr 01 '17 at 15:02
  • @StephenC I have edited the script, it should work now. – Emanuela Masucci Apr 01 '17 at 16:03
  • Well yes. Now it is essentially the same as the accepted solution. – Stephen C Apr 01 '17 at 16:12
0

I was doing the same exercise today, maybe it's not the best aproach, but I think it's easy to understand.

def ispangram(s):
  stringy = ''
  flag = True
  validLetters = "abcdefghijklmnopqrstuvwxyz"

  #transform the given string in simple string with no symbols only letters
  for char in s.lower():
    if(char in validLetters):
      stringy += char

  #check if all the letters of the alphabet exist on the string
  for char in validLetters:
    if(char in stringy):
      pass
    else:
      flag = False
      break

  return flag

if(ispangram("We promptly judged antique ivory buckles for the next prize")):
  print("It's PANGRAM!")
else:
  print("It's not Pangram :(")
mseromenho
  • 163
  • 7
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Feb 22 '18 at 18:44
  • @rollstuhlfahrer Thanks for you comment, it's the first time that I actually post something in stackoverflow, could you help me understanding what could be "context regarding why and/or how this code answers the question". Thanks again. – mseromenho Feb 22 '18 at 18:49
0
import string

def ispangram(str1, alphabet=string.ascii_lowercase):
    return ''.join(sorted(set(str1.lower().replace(" ","")))) == alphabet

First changed all alphabets to lowercase and then removed all spaces using replace. Then Converted into set to have unique chars and then used sorted function to sort alphabetically. As sorted function gives a list, so join func to join it without spaces and then compared it to all lowercase chars.

Jay
  • 1
0

Here is my solution:

def isaPangrams(s):
    alph = list(string.ascii_lowercase)
    s = s.lower()
    s = list(s)

    for letter in alph:
        if letter not in s:
            print('not pangram')
            present='false'
            break
        if letter in s:
            present = 'true'

    if present == 'true':
        print('pangram')

if __name__ == '__main__':
    s = input()
    answer = isaPangrams(s)
Flair
  • 2,609
  • 1
  • 29
  • 41
  • welcome to SO. Please note that in python the convention is to use snake_case for function names. "isaPangrams" is in conflict with that convention – Neuron Jan 11 '22 at 00:47