palindrome?

Ali Hamdi Ali Fadel · @AliOsm · almost 3 years

Takes a string and return true if it is a palindrome, false otherwise.

def palindrome?(string)
i = 0
j = string.length - 1
while i < j do
if string[i] != string[j]
return false
end
i += 1
j -= 1
end
return true
end
0 · 3 · 0
Emad Elsaid · @emad-elsaid · almost 3 years

How about renaming it to palindrome? Also a simpler implementation can be

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

I agree on the name, put the name field didn’t accept the question mark and shows Name is invalid error. Regarding to the implementation, using the reverse method will create a new string object unlike the current implementation which uses the same string object and a constant memory, what do you think?