Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Last active September 29, 2017 19:53
Show Gist options
  • Save barbchoy/3dcf2d1b2c71a877a469f467c6cd84d7 to your computer and use it in GitHub Desktop.
Save barbchoy/3dcf2d1b2c71a877a469f467c6cd84d7 to your computer and use it in GitHub Desktop.
null created by barbchoy - https://repl.it/Br7Q/2500
# Write a method that takes a string and returns true if it is a
# palindrome. A palindrome is a string that is the same whether written
# backward or forward. Assume that there are no spaces; only lowercase
# letters will be given.
#
# Difficulty: easy.
def palindrome?(string)
i=0
palindrome_or_no=true
letters=string.split('')
j=letters.length-1
while i<=j
j=letters.length-i-1
if letters[i]!=letters[j]
palindrome_or_no=false
end
i+=1
end
return palindrome_or_no
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #palindrome?")
puts("===============================================")
puts('palindrome?("abc") == false: ' + (palindrome?('abc') == false).to_s)
puts('palindrome?("abcba") == true: ' + (palindrome?('abcba') == true).to_s)
puts('palindrome?("z") == true: ' + (palindrome?('z') == true).to_s)
puts('palindrome?("abcddcba") == true: ' + (palindrome?('abcddcba') == true).to_s)
puts('palindrome?("abcde") == false: ' + (palindrome?('abcde') == false).to_s)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment