Don't say so much, just coding...
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!
def find_missing_letter(arr)
#your code here
end
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
function findMissingLetter(array){
return ' ';
}
describe("KataTests", function(){
it("exampleTests", function(){
Test.assertEquals(findMissingLetter(['a','b','c','d','f']), 'e');
Test.assertEquals(findMissingLetter(['O','Q','R','S']), 'P');
});
});
想法(1): 後面的英文字要是前一个英文字的下一个,不是的话,则回传下一个英文字
想法(2): 可以用 ord
来完成,但其实有更快的方法,值得好好想一下...
# 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
// 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)
如果不想要大家一起通灵通出一坨O,请不要偷懒做好规划。我说那个Excel写出来的功能列表也是不够的,...
UIView , UIViewController Life Cycle 常见问题 在执行专案的过程...
今天想要来记录这篇文件 Why the “volatile” type class should n...
本系列文章同步发布於笔者网站 上一篇讲述了 OVN Plug-in 在 OpenStack 中的参考...
继续昨天的主题,该怎麽用 Chakra UI 做开发呢?现成的元件该怎麽去做客制化?专案有定设计系统...