pythonalphabet

Find Missing Letter in List (Lowercase or Uppercase, Exclusively)


Input is a list, consistently lower or uppercase. Within the sequence, when sorted correctly, one letter is missing. The function should return the missing letter as string output. See code below, where you'll notice I'm halfway done having calculated the missing letter just for lowercase lists.

import string 
def find_missing_letter(chars):
    for letter in string.ascii_lowercase:
        if letter not in chars:
            return letter[0]

Test examples:

test.assert_equals(find_missing_letter(['a','b','c','d','f']), 'e')
test.assert_equals(find_missing_letter(['O','Q','R','S']), 'P')

Anyone know how to check regardless of letter case??


Solution

  • 2 changes are required for your specification:

    1. Determine your charset by checking for the type of the letters chars contains.

    2. Start your check from the character that is the head of chars - that way chacking for b, c, e will result in d and not a.

    Should go like:

    def find_missing_letter(chars):
        charset = string.ascii_lowercase if chars[0] >= 'a' else string.ascii_uppercase
        for letter in charset[charset.index(chars[0]):]:
            if letter not in chars:
                return letter[0]