Ruby Way to Check for String Palindrome

Ruby way to Check for string palindrome

def check_palindromic(variable)
if variable.reverse == variable #Check if string same when reversed
puts "#{ variable } is a palindrome."
else # If string is not the same when reversed
puts "#{ variable } is not a palindrome."
end
end

How do I write a palindrome check?

The newline character is not being deleted from the string. Try this code:

print "enter a string:\n"
string = gets.chomp
if string.reverse == string
print "it's a palindrome"
else
print "not a palindrome.\n"
end

Here is some more explanation:

>> string = gets
racecar # input string
=> "racecar\n"
>> "racecar\n" == "racecar\n".reverse # "racecar\n" is not a palindrome with newline character
=> false
>> string = gets.chomp # chomp method deletes newline character
racecar
=> "racecar"
>> "racecar" == "racecar".reverse # "racecar" without a newline character is a palindrome
=> true

Ruby programming exercise to determine whether a string is a palindrome

As far as your solution is concerned, I think you are mistaken your code works as expected. Of course false == false is true, so palindrome?("abc') == false is true.

Although not directly related to your solution but how about using inbuilt reverse functionality of ruby

def palindrome?(string):
string == string.reverse
end

How to recursively check if a string is palindrome in Ruby

Smells like homework assignment since you specifically request a recursive solution.

A solution with recursion is to check whether first equals last letter and if they are recursively check the string in between. An empty or one character string is a palindrome.

def palindrome?(str)
str.length <= 1 or (str[0,1] == str[-1,1] and palindrome?(str[1..-2]))
end

palindrome function in ruby terminates without return value

There are several ways you can check for a Palindrome.

The recursive way:

def palindrome?(n)
if n.length == 1 || n.length == 0
true
else
if n[0] == n[-1]
palindrome?(n[1..-2])
else
false
end
end
end

Using Ruby's .reverse method:

def isPalindrome(n)
n.downcase == n.reverse.downcase ? true : false
end

Another way to think about a palindrome word is by breaking down the string into an array of characters each representing a position in the list. For example, the word "civic" split into an array gives us ["c","i","v","i","c"]. We can then check if the rear character "c" is equal to the front character "c" removing the rear and front every time the condition is true.

 def isPalindrome(n)
new_char = n.split("")
answer = false
while new_char.length > 1
if new_char[0] == new_char[-1]
answer = true
else
answer = false
break
end
new_char.pop
new_char.shift
end
answer
end

Since we can shift and pop characters in the array, we can compare them and continue only if they match. We can keep matching the rear front of the array until we either run out of characters or be left with a array of size 1 depending on whether the length of the original string was even or odd.

Ruby - Finding the longest palindromic substring in a string

If your just looking for the largest palindrome substring, Here is a quick and dirty solution.

def longest_palindrome(string, size)
string.size.times do |start| # loop over the size of the string
break if start + size > string.size # bounds check

reverse = string[start, size].reverse

if string.include? reverse #look for palindrome
return reverse #return the largest palindrome
end
end
longest_palindrome(string, size - 1) # Palindrome not found, lets look for the next smallest size
end

Ruby palindrome boolean returning false

  1. At first, you shouldn't use reverse!. This method modifies word, but you only need to get the modified result. So you should use reverse instead.
  2. Your method can be simplified to:

    def palindrome?(word)
    word == word.reverse
    end
  3. "Stats" is not a palindrome unless you convert it to lower or upper register:

    def palindrome?(word)
    word = word.downcase
    word == word.reverse
    end

The result of testing the final version is:

puts palindrome?("Stats")
# => true


Related Topics



Leave a reply



Submit