Created
November 20, 2023 10:46
-
-
Save anchietajunior/c5b0f6d31b15841100cd0672b5faeeee to your computer and use it in GitHub Desktop.
Palindrome algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def palindrome?(word) | |
left = 0 | |
right = word.length - 1 | |
while left < right | |
return false if word[left] != word[right] | |
left += 1 | |
right -= 1 | |
end | |
true | |
end | |
# Tests | |
require 'rspec/autorun' | |
describe 'palindrome?' do | |
it 'returns true for a palindrome word' do | |
expect(palindrome?("HANNAH")).to be true | |
end | |
it 'returns false for a non-palindrome word' do | |
expect(palindrome?("GAGA")).to be false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment