Step-by-step solution for Longest Word
View the challenge

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, "~!@#$%^&*()-_+={}[]:;'<>?/,.|`")

  # 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 User Solutions
1 hyuil Challenge Score: 10 Cpp
1 Qlogin Challenge Score: 10 Python
2 Sabin Challenge Score: 10 Python
1 junjunparkpark Challenge Score: 10 Ruby
1 cjordanball Challenge Score: 10 JavaScript
3 Corinto Challenge Score: 10 Python
4 mmalkavian Challenge Score: 10 Python
5 gzamfire Challenge Score: 10 Python
2 EmperorDuny Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
3 cdms0 Challenge Score: 10 Cpp
3 jackgeek Challenge Score: 10 JavaScript
6 hurgadion Challenge Score: 9 Python
7 scates Challenge Score: 10 Python
8 ricky0309 Challenge Score: 10 Python
4 chrigolex Challenge Score: 10 JavaScript
9 philtroin Challenge Score: 10 Python
4 sanguookchoi Challenge Score: 10 Cpp
5 beeboop19 Challenge Score: 10 JavaScript
6 mattlarsh Challenge Score: 10 JavaScript
7 deuzen Challenge Score: 10 JavaScript
8 SuprDewd Challenge Score: 10 JavaScript
5 gutty333 Challenge Score: 10 Cpp
1 arjnklc Challenge Score: 10 Java
9 safetyscissors Challenge Score: 10 JavaScript
10 franko Challenge Score: 9 JavaScript
10 theBestFella Challenge Score: 10 Python
2 toasterbob Challenge Score: 10 Ruby
3 Yosejimyte Challenge Score: 10 Ruby
4 duncanbb Challenge Score: 10 Ruby
5 Bohdan Challenge Score: 10 Ruby
6 emuchynski Challenge Score: 10 Ruby
7 mienaineko Challenge Score: 10 Ruby
6 johne Challenge Score: 10 Cpp
8 randyjap Challenge Score: 10 Ruby
1 ionutforyou Challenge Score: 10 PHP
9 kelaiyarao1 Challenge Score: 9 Ruby
7 Justification Challenge Score: 10 Cpp
10 CoderAPS Challenge Score: 10 Ruby
8 Zeikar Challenge Score: 10 Cpp
2 danaksim Challenge Score: 10 Java
3 jakmaj Challenge Score: 10 Java
4 mikejagen Challenge Score: 10 Java
5 binfeng Challenge Score: 10 Java
9 anta0 Challenge Score: 10 Cpp
6 ph4ntom Challenge Score: 10 Java
10 lingqyzeng Challenge Score: 10 Cpp
7 nicklivens Challenge Score: 10 Java
8 KSmithson Challenge Score: 10 Java
9 kester824 Challenge Score: 10 Java
2 longcheng Challenge Score: 10 PHP
10 Redmanter Challenge Score: 10 Java
3 torbjornperttu Challenge Score: 10 PHP
4 albionselimaj Challenge Score: 10 PHP
5 teplicko Challenge Score: 10 PHP
6 smcdowell Challenge Score: 10 PHP
7 jetonr Challenge Score: 10 PHP
8 copacubanana Challenge Score: 10 PHP
9 mihainem Challenge Score: 9 PHP
10 khattab Challenge Score: 10 PHP