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

12.7k views Asked by At

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??

5

There are 5 answers

0
Uriel On BEST ANSWER

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]
4
Tushar Jain On

Irrespective of lowercase or uppercase, this should work. It can even work for other consecutive sequences.

def missing_elements(L):
    start, end = L[0], L[-1]
    return sorted(set(range(start, end + 1)).difference(L))

def find_missing_letter(chars):
    numbers = list(map(ord, chars))
    n = missing_elements(numbers)
    return chr(n[0])

Test Example:

chars = ['a','b', 'd']
print(find_missing_letter(chars))

Output:

c

1
Dalen On

import string

def find_missing (txt):
    """Takes string or other type of iterable containing letters.
    Returns a list of all letters that aren't present in the input string/iterable.
    Missing letters are returned as lower case.
    """
    txt = (x.lower() for x in txt if x.isalpha()) # Generator object
    found = dict.fromkeys(string.ascii_lowercase, 0)
    for x in txt: found[x] = 1
    return [x for x in found if found[x]==0]

0
Projesh Bhoumik On

You can use string module and string find method to locate the missing letter. Where

find()

method return -1 if the letter/word is missing in a string.

import string
input_str=raw_input()
output_str=""

for letter in string.letters:

    if input_str.find(letter) ==-1:
        output_str=output_str+letter

print output_str    
0
Ashrith On

Codewars Excercise

import string
def find_missing_letter(char):
    if char[0] in string.ascii_lowercase:
        num=string.ascii_lowercase.index(chars[0])
        for letters in string.ascii_lowercase[num:num+len(chars)+1]:
            if letters not in chars:
                return letters
    elif chars[0] in string.ascii_uppercase:
        num=string.ascii_uppercase.index(chars[0])
        for letters in string.ascii_uppercase[num:num+len(chars)+1]:
            if letters not in chars:
                return letters