Free Code Camp Algorithmic Challenges Part 2

Please Note

This is part 2 of a series in which I give solutions to the Free Code Camp Algorithmic challenges. Have a look at part 1 here. You can also look at the github repo with all my solutions here.

Don't look at this post until you've solved these challenges yourself! You've been warned. :)

Also, I'm using ramdajs and ES6 for all of these challenges, something you can't do at freecodecamp.com

In the last post we just got setup for doing Free Code Camp's algorithmic challenges starting with reversing a string and factorial.

Here are some more problems and solutions. They'll get harder as we go along:

Checking For Palindromes

A palindrome is a word or phrase that is spelled the same when it's reversed. "Eye" is a palindrome, so is "race car" and "never odd or even" (assuming we omit spaces).

This challenge is to write a function that accurately determines if a given string is a palindrome. We are to ignore spaces, capitalization, and non-alphanumeric characters.

Here's how I sanitized the string of all the stuff we're supposed to ignore:

module.exports =  
    R.pipe(
        R.replace(/[^A-Za-z0-9]/g, ''),
        R.toLower,
        palindrome_test)

Note that R.pipe works just like R.compose. Both chain function calls but in opposite directions.

In this example I'm simply getting rid of all non alpha-numeric characters with a regular expression. (You can learn more about regular expressions here), then I set the remaining characters to lower case before piping the resulting string into palindrome_test.

Here's palindrome_test:

var palindrome_test = (string) => {  
    if(string.length < 2) return true
    else if (R.head(string) === R.last(string))
        return palindrome_test(R.slice(1, -1, string))
    else return false
}

If the string is less than 2 characters long, it's automatically a palindrome. "a" is a palindrome. It's spelled the same way backwards and forwards. :)

If the first character of a string (i.e. the string's head) and the last character of the string are equal, we recursively test the middle of the string again. The middle simply being the string with it's first and last characters removed.

Eventually we'll get to a string of less than 2 characters or...

If neither of these conditions is true, we return false.

Find The Longest Word In A String

In this problem we're given a string with words separated by a single space. Our task is to find the biggest word in the string and return its length.

My solution is a composition function that first splits the string into individual words, then uses a reduce call to compare each word to the value of the accumulator (the largest word so far) and either returns the new or old word as the new accumulator value. Finally we get the length of that word.

module.exports =  
  R.pipe(
    R.split(' '),
    R.reduce(
        (longest, word) =>
            longest.length >= word.length
                ? longest
                : word,
        ''),
    R.length)

Ok, that's enough for now. Stay tuned next time! Same funky time! Same funky channel!

Looking for a software developer?