Find the missing letter

爱被打了一巴掌 2022-06-16 07:09 690阅读 0赞

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:

  1. ['a','b','c','d','f'] -> 'e'
  2. ['O','Q','R','S'] -> 'P'

Code:

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

Code2:

  1. def find_missing_letter(chars):
  2. return [chr(n) for n in range(ord(chars[0]),ord(chars[-1])+1) if n not in [ord(c) for c in chars]][0]

发表评论

表情:
评论列表 (有 0 条评论,690人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Find the Missing Number

    方法一: 数学方法,先找到最大的值,需要比较最大的值和array size, 要是比array size小, 说明最大值missing。 然后用等差数列公式求得如果不缺失值的和