简体   繁体   中英

Checking if two characters are equal to each other in python

I am trying to write a program to find a missing letter in an array of letters of alphabetical order. ex. [a,b,c,d,f] missing => 'e'.

Right now I have this:

def find_missing_letter(chars):

    # Creates variables of complete alphabet.

    alphabetLower = list(string.ascii_lowercase)
    enumeratedLower = []
    alphabetUpper = list(string.ascii_uppercase)
    enumeratedUpper = []

    # Checks if the function has to enumerate the upper- or lowercase alphabet.

    if(chars[0].islower()):
        for c, value in enumerate(alphabetLower, 1):
            enumeratedLower.append([c, value])
    else:
        for c, value in enumerate(alphabetUpper, 1):
            enumeratedUpper.append([c, value])

    # Checks at what letter the characters begin. 
    # After that it checks if the following letters are equal to eachother.

    if(chars[0].isupper()):
        for x in range(1, 26):
            print enumeratedUpper[x][1]
            print 'char:' + chars[0]
            if(chars[0] == enumeratedUpper[x][1]):
                for i in range(enumeratedUpper[x][0], len(chars)):
                    if(chars[i] != alphabetUpper[i]):
                        return alphabetUpper[i]
    else:
        for x in range(1, 26):
            print enumeratedLower[x][1]
            print 'char:' + chars[0]
            if(chars[0] == enumeratedLower[x][1]):
                for i in range(enumeratedLower[x][0], len(chars)):
                    if(chars[i] != alphabetLower[i]):
                        return alphabetLower[i]

However, the if statements

if(chars[0] == enumeratedUpper[x][1]):

and

if(chars[0] == enumeratedLower[x][1]):

are not working for some reason. The reason for this statement is because a given array of characters (chars), can start at a random letter (doesn't have to start at 'a' or 'A'). I put the print statement there to see what was wrong and the output is this:

b
char:o
c
char:o
d
char:o
e
char:o
f
char:o
g
char:o
h
char:o
i
char:o
j
char:o
k
char:o
l
char:o
m
char:o
n
char:o
o
char:o
p
char:o
q
char:o
r
char:o
s
char:o
t
char:o
u
char:o
v
char:o
w
char:o
x
char:o
y
char:o
z
char:o

You are overcomplicating things a bit. There is no need to handle upper and lowercase letters separately. Also, you don't have to use enumerate just use index method to find where characters begin.

import string 

def find_missing_letter(chars):   
    # Creates variables of complete alphabet.
    alphabet = string.ascii_lowercase if chars[0].islower() else string.ascii_uppercase
    # Checks at what letter the characters begin
    start = alphabet.index(chars[0])
    # After that it checks if the following letters are equal to each other.
    for x, y in zip(chars, aplhabet[start:]):
        if x != y:
            return y

Sample output:

>>> find_missing_letter('abcdf')
'e'

Your approach seems to be too complicated. Just convert the list of chars to list of nums using map and ord , then enumerate the resulting list to find the first missing number, convert it back to char using chr

>>> l = ['a', 'b', 'c', 'd', 'f']
chr(next(i for i,j in enumerate(map(ord, l), ord(l[0])) if i!=j))
'e'

You may search the letters that are not in chars, using a generator expression, and then sort them if you need them in alphabetic order

>>> chars = ['a', 'b', 'd', 'f']
>>> alphabet = string.ascii_lowercase if chars[0].islower() else string.ascii_uppercase
>>> sorted(letter for letter in alphabet if letter not in chars and letter > chars[0] and letter < chars[-1])

['c', 'e']

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