pythion 算法题:Find the missing letter

#Find the missing letter
Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.
You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2.
The array will always contain letters in only one case.

Example:
['a','b','c','d','f'] -> 'e'

['O','Q','R','S'] -> 'P'

这道题当时做的比较纠结,主要是chr()和ord()的用法,还要注意i的取值范围问题,超出列表就不适用!

def find_missing_letter(chars):
    for i in range(len(chars)-1):
        if ord(chars[i])+1 != ord(chars[i+1]):
            return chr(ord(chars[i])+1)


发布了36 篇原创文章 · 获赞 5 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章