Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added is_n_palin.rb and is_n_palin.sh

  • Loading branch information...
commit ff0d33545455bff9e73ff9d702be0dea13fd5971 1 parent f21e2ef
@nvijayap authored
Showing with 45 additions and 0 deletions.
  1. +39 −0 is_n_palin.rb
  2. +6 −0 is_n_palin.sh
View
39 is_n_palin.rb
@@ -0,0 +1,39 @@
+#
+# --------------
+# is_n_palin.rb
+# --------------
+# Checks if a number is palindrome or not by -
+# . Using math logic only, and
+# . Not using any Ruby Library
+#
+
+def is_n_palin? n
+ begin
+ if Float(n).to_i != n.to_i
+ abort "'#{n}' is NOT a Whole Number"
+ end
+ rescue
+ abort "'#{n}' is NOT a Number"
+ end
+
+ n = n.to_i
+ m = n ; r = 0
+
+ while m > 0 do
+ r = 10 * r + m - 10 * (m/10)
+ m = m/10
+ end
+
+ n==r
+end
+
+describe "test a number palindrome" do
+ it "is not a palindrome" do
+ is_n_palin?(12).should == false
+ end
+
+ it "is a palindrome" do
+ is_n_palin?(121).should == true
+ end
+end
+
View
6 is_n_palin.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+cat is_n_palin.rb
+
+rspec is_n_palin.rb
+

0 comments on commit ff0d335

Please sign in to comment.
Something went wrong with that request. Please try again.