Solutions for Longest Word
View the challenge

Official solution

This challenge requires you to return the longest word in a sentence. Our goal is therefore to somehow loop through the string (what is a string?) and compare the lengths of the words. For example, if the string were "hello from coderbyte" the program should return the word "coderbyte" because it has the longest length. To solve this we need to: 1. Strip away any punctuation because the challenge tells us to ignore these symbols, so the string "hello$%()" is actually just "hello" 2. Separate the sentence into a list of words in order to easily retrieve words and their respective lengths 3. Then loop through this list and compare the words to find the one with the longest length We will be using regular expression functions in order to strip away punctuation and turn the string into a list of words. Regular expressions are patterns we define for the program to find in some text. Below is some code that performs each of these steps:

function LongestWord(sen) { 

  // we use the regex match function which searches the string for the
  // pattern and returns an array of strings it finds
  // in our case the pattern we define below returns words with
  // only the characters a through z and 0 through 9, stripping away punctuation
  // e.g. "hello$% ##all" becomes [hello, all]
  var arr = sen.match(/[a-z0-9]+/gi);

  // the array sort function takes a function as a parameter
  // which is used to compare each element in the array to the
  // next element in the array
  var sorted = arr.sort(function(a, b) {
    return b.length - a.length;
  });

  // this array now contains all the words in the original
  // string but in order from longest to shortest length
  // so we simply return the first element
  return sorted[0];
         
}
   
LongestWord("the $$$longest# word is coderbyte");                            
def LongestWord(sen): 

  # first we remove non alphanumeric characters from the string
  # using the translate function which deletes the specified characters
  sen = sen.translate(None, "[email protected]#$%^&*()-_+={}[]:;'<>?/,.|`")

  # now we separate the string into a list of words
  arr = sen.split(" ")

  # the list max function will return the element in arr
  # with the longest length because we specify key=len
  return max(arr, key=len)
    
print LongestWord("the $$$longest# word is coderbyte")  
def LongestWord(sen)

  # we use the regex match function which searches the string for the
  # pattern and returns an array of strings it finds
  # in our case the pattern we define below returns words with
  # only the characters a through z and 0 through 9, stripping away punctuation
  # e.g. "hello$% ##all" becomes [hello, all]
  arr = sen.split.map do |w| 
    /[a-zA-Z0-9\s]+/.match(w) 
  end
  
  # no need to sort and get the last word, you can
  # easily find largest word in array using max_by
  longest = arr.max_by do |w| 
      w.to_s.length 
  end
  
  return longest
         
end
   
puts LongestWord(STDIN.gets)

Top Rated User Solution

loading...
loading...
loading...
loading...
loading...
loading...
loading...
loading...
loading...
loading...
Top User Solutions

Loading...