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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章