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 emuchynski Challenge Score: 10 Ruby
3 KlaraKathrina Challenge Score: 10 Python
2 junjunparkpark Challenge Score: 10 Ruby
1 cjordanball Challenge Score: 10 JavaScript
4 Corinto Challenge Score: 10 Python
2 EmperorDuny Challenge Score: 10 JavaScript
5 gzamfire Challenge Score: 10 Python
6 mmalkavian Challenge Score: 10 Python
3 GarenLiang Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
4 beeboop19 Challenge Score: 10 JavaScript
3 cdms0 Challenge Score: 10 Cpp
5 jackgeek Challenge Score: 10 JavaScript
6 escapiststupor Challenge Score: 10 JavaScript
7 hurgadion Challenge Score: 9 Python
4 gutty333 Challenge Score: 10 Cpp
8 scates Challenge Score: 10 Python
9 ricky0309 Challenge Score: 10 Python
7 chrigolex Challenge Score: 10 JavaScript
10 philtroin Challenge Score: 10 Python
3 Bohdan Challenge Score: 10 Ruby
5 sanguookchoi Challenge Score: 10 Cpp
8 mattlarsh Challenge Score: 10 JavaScript
4 zestybmac Challenge Score: 10 Ruby
9 deuzen Challenge Score: 10 JavaScript
1 arjnklc Challenge Score: 10 Java
10 SuprDewd Challenge Score: 10 JavaScript
5 mattfara Challenge Score: 10 Ruby
6 toasterbob Challenge Score: 10 Ruby
7 SSJ6Porfy Challenge Score: 10 Ruby
8 Yosejimyte Challenge Score: 10 Ruby
9 duncanbb Challenge Score: 10 Ruby
10 kelaiyarao1 Challenge Score: 9 Ruby
6 johne Challenge Score: 10 Cpp
1 ionutforyou Challenge Score: 10 PHP
7 Justification Challenge Score: 10 Cpp
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 ph4ntom Challenge Score: 10 Java
6 binfeng Challenge Score: 10 Java
9 anta0 Challenge Score: 10 Cpp
7 Niliyam Challenge Score: 10 Java
10 lingqyzeng Challenge Score: 10 Cpp
8 nicklivens Challenge Score: 10 Java
9 KSmithson Challenge Score: 10 Java
10 kester824 Challenge Score: 10 Java
2 longcheng Challenge Score: 10 PHP
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

Become a Premium Member

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

“Daniel - Thanks so much for making Coderbyte! We still recommend it to people.” ― Shawn Drost
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses