Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add rake test that allows fast tests and isbouncy

  • Loading branch information...
commit 82095550618db2677e2cadc7bdc84fa8a1352e3f 1 parent 1b3e01d
@D4L authored
Showing with 30 additions and 0 deletions.
  1. +10 −0 Rakefile
  2. +20 −0 lib/boolIsBouncyInt.rb
View
10 Rakefile
@@ -50,6 +50,16 @@ task :time do
puts "Time it takes to selection sort a 1000 number array: " + gettime(Proc.new{a=Array.new(1000){|i|Random.rand};def s(a, i, j);t=a[i];a[i]=a[j];a[j]=t;end;def i(a);(0..(a.size-1)).each{|i|l=1;p=0;(i..(a.size-1)).each{|j|p,l=j,a[j] if a[j]<l};s(a,i,p);};end;i(a)}).to_s
end
+desc "You can change this code and test individual peices of code fast"
+task :test do
+ testObject = Proc.new do
+ boolIsBouncyInt(93322)
+ end
+ testResult = gettime(testObject, "full")
+ puts "Time taken: " + "%e" % testResult[0].to_s
+ puts "Computed result: " + testResult[1].to_s
+end
+
namespace "specific" do
desc "Find the solution to a specific solution"
task :solution do
View
20 lib/boolIsBouncyInt.rb
@@ -0,0 +1,20 @@
+public
+
+# A bouncy number is one that is neither ascending or descending
+# An ascending or descending number is one which when going through the digits, each one is either less or more than the previous one
+# an example of a bouncy number is 25394
+# an example of a non-bouncy number is 1468
+def boolIsBouncyInt i
+ numDigits = i.to_s.length
+ return false if numDigits < 3 # any number 0-99 is non-bouncy
+ sortedNumber = i.to_s.each_char.sort.join.to_i
+ sortedNumberReverse = sortedNumber.to_s.reverse.to_i
+ return false if sortedNumber % 10 == sortedNumber / 10**(numDigits - 1)
+ if i % 10 == sortedNumber % 10 # this means it's an ascending number
+ return (not sortedNumber == i)
+ elsif i % 10 == sortedNumberReverse % 10 # this means it's a descending number
+ return (not sortedNumberReverse == i)
+ end
+ true
+end
+# 6263746982
Please sign in to comment.
Something went wrong with that request. Please try again.