simple-exercises/ruby-exercises/easy-2.rb
# Write a method that takes in a string of lowercase letters (no
# uppercase letters, no repeats). Consider the *substrings* of the
# string: consecutive sequences of letters contained inside the string.
# Find the longest such string of letters that is a palindrome.
#
# Note that the entire string may itself be a palindrome.
#
# You may want to use Array's `slice(start_index, length)` method,
# which returns a substring of length `length` starting at index
# `start_index`:
Write a method that takes in a string of lowercase letters (no
uppercase letters, no repeats). Consider the *substrings* of the
string: consecutive sequences of letters contained inside the string.
Find the longest such string of letters that is a palindrome.

Note that the entire string may itself be a palindrome.


Write a method that takes in a string of lowercase letters ( no uppercase letters no repeats ) . Consider the * substrings * of the string : consecutive sequences of letters contained inside the string . Find the longest such string of letters that is a palindrome . Note that the entire string may itself be a palindrome .
def longest_palindrome(string)
  if string.length <= 1
    return string
  end
  i = 0
  string.length.downto(1).each do |length|
    (0..i).each do |start_index|
      if palindrome?(string.slice(start_index, length))
        return string.slice(start_index, length)
      end
    end
    i += 1
  end
  string[0]
end
def longest_palindrome ( string ) if string . length <= 1 return string end i = 0 string . length . downto ( 1 ) . each do | length | ( 0 .. i ) . each do | start_index | if palindrome? ( string . slice ( start_index , length ) ) return string . slice ( start_index , length ) end end i += 1 end string [ 0 ] end
Write a method that takes in a string of lowercase letters (no
uppercase letters, no repeats).
Write a method that takes in a string of lowercase letters ( no uppercase letters no repeats ) .
# .. is inclusive
string
<not_specific>