Created
April 30, 2011 17:59
-
-
Save carbonphyber/949837 to your computer and use it in GitHub Desktop.
Ruby function to detect if a Fixednum is a palendrome.
This file contains 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
# @author David Wortham <djwortham+programming@gmail.com> | |
# @date 2011-04-30 | |
# @license MIT License: http://www.opensource.org/licenses/mit-license.php | |
def isPalendrome(number) | |
numberStr = number.to_s() | |
return false if numberStr.empty? || numberStr.length < 0 | |
return true if numberStr.length < 2 | |
lCursor = 0 | |
rCursor = numberStr.length - 1 | |
while lCursor < rCursor | |
return false if numberStr.getbyte(lCursor) != numberStr.getbyte(rCursor) | |
lCursor = lCursor + 1 | |
rCursor = rCursor - 1 | |
end | |
return true | |
end | |
# usage | |
# puts "111 is a palendrome? #{isPalendrome(111)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment