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 a 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:
Good luck!
Today's challenge comes from user5036852 onCodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (23)
This makes an assumption about where the missing letter is in the string that isn't valid for all the test cases?
It returns incorrectly for both test cases that they propose in the problem. You're on the right track but have a little bit more work to do.
Thanks for the heads up. I did not get the challenge right and posted a wrong answer. Here is the correct one, I hope :)
Here is another JS version
The LETTERS constant could be inside the findMissing function... but that seemed less reusable. Covers all the test cases and should only iterate the arrays fully if there is no missing character to be found. So should be pretty fast as well.
Nice challenge, I tried doing it in JS with an array.reduce and character codes. I think it works well enough! I don't think the arr.splice(1) is really clean though, so perhaps a traditional loop would be better.
This my solution, I also tried to account for the possibility that there wasn't just one letter missing:
Time to Go find the missing letter!
missing_letter.go
missing_letter_test.go
Here it is in JS, uses
charCodeAt
andfromCharCode
to convert from char to ASCII number, then looks for the number difference of2
(missing number in between them):Almost identical but I try with the functional approach and typescript
checks any number of chars instead of one.
codesandbox.io/s/quirky-davinci-sk826
My take at the challenge written in TypeScript this time and using a recursive solution!
I haven't done that much of a test so do not hesitate to tell me if I'm wrong. I also have returned false whether when there was no missing letters. I guess my solution will fail when the first letter is at the beginning or at the end of the array. The source-code with the playground is available here!
I also did use my algorithm in Elm. Here is the source-code for the inner logic function.
I bet there could be a way to use it in Haskell or PureScript as well, but I'm more confortable using Elm for now.
And the testing is of course available here on Ellie App.
A quicky using
reduce
in JSFirst up it converts the array of letters to an array of char codes.
It then uses reduce to find the 2-code gap, and return the letter with char code one less that that of the letter just after the gap.
If there is more than one gap it will return the letter for the last one.
If there is no 2-code gap, then it will return undefined.
(checked on kata)
I wrote the code (in Python) assuming that the missing letter is within the list, if not there would be 2 answers to it.
Here is my previous implementation, I wrote this to handle the scenario that the input list wasn't in an ascending order (I didn't read the question completely 🤣)