Skip to main content
added explanation
Source Link
doetoe
  • 131
  • 2

This one requires only one integer of storage:

    ord_a = 97 # ord('a')
    ord_z = 122 # ord('z')

    def is_pangram(s):
      found = 0
      for c in s:
        ord_char = ord(c.lower())
        if ord_a <= ord_char <= ord_z:
          found |= 1 << (ord_char - ord_a)
      return found == (1 << 26) - 1

The data structure used is the integer found, whose least significant 26 bits are used to indicate if the corresponding letter has been found. The coding is simply a=0, b=1, etc, which can be conveniently done obtained from the ASCII code as ord(char) - ord('a'). The characters in the string have to be iterated, if you wish you could stop when you found them all.

Note that even though the algorithmic complexity is essentially the lowest possible, the fact that the loop is in pure Python makes it slower than one which avoids looping in Python, even if it has to perform more operations.

This one requires only one integer of storage:

    ord_a = 97 # ord('a')
    ord_z = 122 # ord('z')

    def is_pangram(s):
      found = 0
      for c in s:
        ord_char = ord(c.lower())
        if ord_a <= ord_char <= ord_z:
          found |= 1 << (ord_char - ord_a)
      return found == (1 << 26) - 1

This one requires only one integer of storage:

    ord_a = 97 # ord('a')
    ord_z = 122 # ord('z')

    def is_pangram(s):
      found = 0
      for c in s:
        ord_char = ord(c.lower())
        if ord_a <= ord_char <= ord_z:
          found |= 1 << (ord_char - ord_a)
      return found == (1 << 26) - 1

The data structure used is the integer found, whose least significant 26 bits are used to indicate if the corresponding letter has been found. The coding is simply a=0, b=1, etc, which can be conveniently done obtained from the ASCII code as ord(char) - ord('a'). The characters in the string have to be iterated, if you wish you could stop when you found them all.

Note that even though the algorithmic complexity is essentially the lowest possible, the fact that the loop is in pure Python makes it slower than one which avoids looping in Python, even if it has to perform more operations.

Source Link
doetoe
  • 131
  • 2

This one requires only one integer of storage:

    ord_a = 97 # ord('a')
    ord_z = 122 # ord('z')

    def is_pangram(s):
      found = 0
      for c in s:
        ord_char = ord(c.lower())
        if ord_a <= ord_char <= ord_z:
          found |= 1 << (ord_char - ord_a)
      return found == (1 << 26) - 1