Write a function that takes an array of consecutive (increasing) letters as input and returns the missing letter.
- You will always get an valid array.
- The array 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.
The main breakthrough is realizing that the fundamental problem is comparing one character to the next character. A nice cljs trick is to use
partition to get the data structure that better represents our problem:
- With our problem-aligned data structure, we work our way through the list, comparing the character code of the first letter to that of the second.
- If the characters are in order, we return
- If we’ve found our missing letter, we return it.
keepreturns a sequence of all non-nil values, so the value of
keepwill be the sequence of missing letters.
- Since the problem states there will only be one missing letter, we return the
keepis lazy; when we find our first value, cljs will stop processing the rest of the list).
Find this post useful? Share the love! Tweet