简体   繁体   中英

Missing letters method with python

Hello I have the code below, but it's not working as expected, I cannot see where I made the mistake, I'm using python 3 the missing_letters method is not working as expected because the second output is wrong, I need it to take an input and return the missing letters using the alphabet string as a comparative

alphabet = "abcdefghijklmnopqrstuvwxyz"   

test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the 

def missing_letters(string):
    compare = histogram(string) 
    for key in sorted(compare): 
        if key in alphabet: 
            result = alphabet.replace(key,'') 
            return result # print result
        else:
            return None

for letters in test_miss: 
    if missing_letters(letters) == None:
        print(letters, 'uses all the letters')   
    else:
        print(letters , 'is missing letters', missing_letters(letters))

# output: zzz is missing letters abcdefghijklmnopqrstuvwxy                                                                               
# subdermatoglyphic is missing letters bcdefghijklmnopqrstuvwxyz                                                                 
# the quick brown fox jumps over the lazy dog uses all the letters

You don't need to count the number of letter, you can directly use a set (that will have only one occurence of each letter from your input string ). Once you have that set you can subtract this set from the alphabet and you'll end up with a set that contains the missing letters:

from string import ascii_lowercase

alphabet = set(ascii_lowercase)

def missing_letters(string):
    return alphabet - set(string.lower())

print(missing_letters("abcdefghijklmnopqrstuv"))

Will output {'w', 'y', 'x', 'z'} . Note that ascii_lowercase simply is a string containing all lower-case letters.

The main problem is that you return inside of the loop, ie after checking the very first letter. Also, it seems like you are iterating letters in the word and checking whether they are in the alphabet, instead of the other way around, which would make more senes. Also, it seems unnecessary to create a histogram of letter frequencies.

You can try something like this:

def missing_letters(string):
    missing = []
    for key in alphabet:
        if key not in string:
            missing.append(key)
    return ''.join(missing) or None

Or you could convert both the alphabet and the string to set and use set difference - :

def missing_letters(string):
    return ''.join(sorted(set(alphabet) - set(string))) or None

Use sets for membership testing: first make alphabet a set

In [10]: alphabet = "abcdefghijklmnopqrstuvwxyz"

In [11]: alphabet = set(alphabet)

For the string 'ab' `histogram returns

In [12]: compare = {'a':1,'b':1}

Use set methods to determine the difference

In [13]: alphabet.difference(compare.keys())
Out[13]: 
{'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'}

Your method 'def missing_letters(string):' is wrong. I have corrected it, Following code/method will work, try it

alphabet = "abcdefghijklmnopqrstuvwxyz"   

test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]     

def missing_letters(string):
    result = alphabet
    compare = set(string)# use histogram to make a dictionary
    compare=sorted(compare)
    for key in compare: # iterate and sort in alphabetical order
        if key in alphabet: # check if the key in alphabet
            result = result.replace(key,'')# remove it from alphabet
    return result

# Test the function          
print(missing_letters("ab")) # output: bcdefghijklmnopqrstuvwxyz

##### test_miss for loop #####

for letters in test_miss: 
    if len( missing_letters(letters) ) == 0:
        print(letters, 'uses all the letters')   
    else:
        print(letters , 'is missing letters', missing_letters(letters))

# output: zzz is missing letters abcdefghijklmnopqrstuvwxy                                                                               
# subdermatoglyphic is missing letters bcdefghijklmnopqrstuvwxyz                                                                 
# the quick brown fox jumps over the lazy dog uses all the letters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM