见习村28 - Find the missing letter

28 - Find the missing letter

Don't say so much, just coding...

Instruction

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'

["a","b","c","d","f"] -> "e"
["O","Q","R","S"] -> "P"
(Use the English alphabet with 26 letters!)

Have fun coding it and please don't forget to vote and rank this kata! :-)

I have also created other katas. Take a look if you enjoyed this kata!

Ruby

Init

  def find_missing_letter(arr)
    #your code here
  end

Sample Testing

 Test.describe("Basic tests") do
    Test.assert_equals(find_missing_letter(["a","b","c","d","f"]), "e")
    Test.assert_equals(find_missing_letter(["O","Q","R","S"]), "P")
    Test.assert_equals(find_missing_letter(["b","d"]), "c")
    Test.assert_equals(find_missing_letter(["a","b","d"]), "c")
    Test.assert_equals(find_missing_letter(["b","d","e"]), "c")
  end

Javascript

Init

  function findMissingLetter(array){
    return ' ';
  }

Sample Testing

  describe("KataTests", function(){
    it("exampleTests", function(){
      Test.assertEquals(findMissingLetter(['a','b','c','d','f']), 'e');
      Test.assertEquals(findMissingLetter(['O','Q','R','S']), 'P');
    });
  });

Thinking

想法(1): 後面的英文字要是前一个英文字的下一个,不是的话,则回传下一个英文字
想法(2): 可以用 ord 来完成,但其实有更快的方法,值得好好想一下...

https://ithelp.ithome.com.tw/upload/images/20201013/20120826HwX3ZSzM9n.jpg
图片来源:Unsplash Emile Perron

Hint & Reference

Solution

Ruby

  # Solution 1
  def find_missing_letter(arr)
    (arr.length - 1).times{ |num| return (arr[num].ord + 1).chr if arr[num].ord + 1 != arr[num + 1].ord }
    return nil
  end
  
  # Solution 2
  def find_missing_letter(arr)
    arr[0...-1].each_with_index{ |num, index| return num.next if num.next != arr[index + 1] }
    return nil
  end
  
  # Solution 3
  def find_missing_letter(arr)
    ((arr.first..arr.last).to_a - arr).first
  end

Javascript

  // Solution 1
  function findMissingLetter(array) {
    var string = array.join('');
    for(var i = 0; i < string.length; i++) {
      if(string.charCodeAt(i + 1) - string.charCodeAt(i) != 1){
        return String.fromCharCode(string.charCodeAt(i) + 1);
      }
    }
  }

<<:  [DAY 28] 章节3-8: 前往农场前夕- k-means(k平均分类演算法) (2/2)

>>:  DAY28-如何与人协同工作与好好沟通-外国人篇

[Day03 - 规划与设计] 建立 Wireframe 让你开发不迷路

如果不想要大家一起通灵通出一坨O,请不要偷懒做好规划。我说那个Excel写出来的功能列表也是不够的,...

UIView , UIViewController Life Cycle 常见问题

UIView , UIViewController Life Cycle 常见问题 在执行专案的过程...

# Day 8 Why the “volatile” type class should not be used

今天想要来记录这篇文件 Why the “volatile” type class should n...

OpenStack Neutron 介绍 — OVN vs OVS

本系列文章同步发布於笔者网站 上一篇讲述了 OVN Plug-in 在 OpenStack 中的参考...

#14 No-code 之旅 — 怎麽利用 Chakra UI 去做 React 元件客制化?

继续昨天的主题,该怎麽用 Chakra UI 做开发呢?现成的元件该怎麽去做客制化?专案有定设计系统...