Skip to main content
added 475 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Both are too slow since you don't need to care about the amount but you're storing a count of each letter anyway. You just need to know if all of the relevant characters are in the string. You can do this easier, faster and with less memory usage by calling all.

def is_pangram(astr):
    return all(char in astr.lower() for char in string.ascii_lowercase)

This will just check each character in ascii_lowercase and see if it's in astr. No need to store values at all, and this supports shortcircuiting. Short circuiting will prematurely end a test if it realises that it already has its result.

So the script will run through and might find an a, b, c, d, and e. But then maybe it can't find an f in the string, instead of continuing to check for g it will immediately return False at that point because it already knows that one letter is missing, so the expression can't be True. This saves on time since the rest of the checks are redundant for the result you want.

Both are too slow since you don't need to care about the amount but you're storing a count of each letter anyway. You just need to know if all of the relevant characters are in the string. You can do this easier, faster and with less memory usage by calling all.

def is_pangram(astr):
    return all(char in astr.lower() for char in string.ascii_lowercase)

This will just check each character in ascii_lowercase and see if it's in astr. No need to store values at all, and this supports shortcircuiting.

Both are too slow since you don't need to care about the amount but you're storing a count of each letter anyway. You just need to know if all of the relevant characters are in the string. You can do this easier, faster and with less memory usage by calling all.

def is_pangram(astr):
    return all(char in astr.lower() for char in string.ascii_lowercase)

This will just check each character in ascii_lowercase and see if it's in astr. No need to store values at all, and this supports shortcircuiting. Short circuiting will prematurely end a test if it realises that it already has its result.

So the script will run through and might find an a, b, c, d, and e. But then maybe it can't find an f in the string, instead of continuing to check for g it will immediately return False at that point because it already knows that one letter is missing, so the expression can't be True. This saves on time since the rest of the checks are redundant for the result you want.

Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Both are too slow since you don't need to care about the amount but you're storing a count of each letter anyway. You just need to know if all of the relevant characters are in the string. You can do this easier, faster and with less memory usage by calling all.

def is_pangram(astr):
    return all(char in astr.lower() for char in string.ascii_lowercase)

This will just check each character in ascii_lowercase and see if it's in astr. No need to store values at all, and this supports shortcircuiting.