Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, September 17, 2009

Extending Watir

My current project is my first project that is solely a web application. I was looking for a way to automate testing for acceptance and regression tests. In reviewing the available frameworks I chose watir. Watir is built using Ruby which fit well with my project as I am using Rake for my build system.

The app makes extensive use of divs and spans in the page templates. Watir allows divs and spans to be tested but it required a lot of repetitive coding. To test that a div exists, and if so that it meets some criteria you have to do something like this.
# validate the login error message is not displayed
#
divFound = false
@browser.divs.each{ |d|
if d.id == 'ValidationSummary'
assert( false ==
d.text().include?('Enter a valid e-mail address.'),
"Expected the error message: 'Enter a valid e-mail " +
"address'. Received error message: #{d.text()}\n"
)
divFound = true
break
end
}
assert( divFound, "Did not find the ValidationSummary div\n")
Not a big deal but it gets tedious if just about everything is in a div or span. To make life a bit easier I wrote routines that encapsulate the repetitive code. They will test if the div or span exists and if so call an optional block. Since all objects in Ruby are open I coded these as an extension of watir.
module Watir
module Container

# Search the list of divs for the one specified by id.
# If the div is not found return false. If a block is
# provide return its result. Otherwise return true.
#
# id - The id of the div to test for
# block - The optional code block
#
def div?(id, &block )
self.divs.each{ |d|
if d.id == id
return (block) ? yield(d) : true
end
}
false
end

# Search the list of spans for the one specified by id.
# If the span is not found return false. If a block is
# provide return its result. Otherwise return true.
#
# id - The id of the span to test for
# block - The optional code block
#
def span?(id, &block )
self.spans.each{ |d|
if d.id == id
return (block) ? yield(d) : true
end
}
false
end
end
end

We can then change the first example to:
# validate the login error message is not displayed
#
divFound = @browser.divs?( 'ValidationSummary') { |d|
assert( false ==
d.text().include?('Enter a valid e-mail address.'),
"Expected the error message: 'Enter a valid e-mail " +
"address'. Received error message: #{d.text()}\n")
true }
assert( divFound, "Did not find the ValidationSummary1 div\n")
If you just wanted to test for the existence of the div you would do this.
assert( @browser.divs?( 'ValidationSummary'), "Did not find....")

I'm working on a patch submission to add these to the watir project.

Thursday, April 16, 2009

Project Euler Problems 18 and 67

Problem 18 and problem 67 are the same problems. The difference being that problem 67 cannot be brute forced. These problems are given a triangle on numbers starting at the top and moving to adjacent numbers on the row below, find the path with the maximum sum.

My first thought was building a tree and using a greedy algorithm to find the path. However, a little thought, and sketching out trees, will show that you can easily build a tree where a greedy algorithm will fail to find maximum path. So what we need to no is traverse the tree and calculate the maximum value for each node. We can do the going down and then scan for the largest value form the nodes that form the bottom of the triangle, or go from the bottom up which causes the root node to have the largest value.

For my solution the triangle represented as an array of arrays. Where each element represents a row of the triangle, and each row is one element longer than the previous. Also each element e[r,i] has to be compared to the elements e[r+1,i] and e[r+1, i+1] where r is index of the row and i the index of the element in that row. The code is as follows.

# receives the table generated from readTable and calculate the largest
# value. The calculation starts with the second to the last table row and
# modifies every element e[r,i] = e[r,i] + max( e[r+1,i], e[r+1, i+1] ).
#
def calcTable( table )
(table.length - 2).downto(0){ |r|
table[r].each_with_index{ |val, idx|
table[r][idx] =
table[r][idx] + max(
table[r+1][idx],
table[r+1][idx+1])
}
}
table[0][0]
end

Saturday, April 11, 2009

Project Euler problems 11 - 15

Here are problems 11 - 16 for project Euler.

Problem 11: What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid? I used an array of arrays to represent the grid.

def euler11(grid)
maxVal = 0
(0..19).each{ |i|
(0..19).each{ |j|
# calculate horizontal
if j < 15 then
val = grid[i][j] * grid[i][j+1]
* grid[i][j+2] *grid[i][j+3]
maxVal = val if maxVal < val

# calculate diag down right
if i < 15 then
val = grid[i][j] * grid[i+1][j+1]
* grid[i+2][j+2] * grid[i+3][j+3]
maxVal = val if maxVal < val
end

# calculate diag up right
if i > 3 then
val = grid[i][j] * grid[i-1][j+1]
* grid[i-2][j+2] * grid[i-3][j+3]
maxVal = val if maxVal < val
end
end

# calculate vertical
if i < 15 then
val = grid[i][j] * grid[i+1][j]
* grid[i+2][j] * grid[i+3][j]
maxVal = val if maxVal < val
end
}
}
maxVal
end
Problem 12: What is the value of the first triangle number to have over five hundred divisors?

A triangle number is calculated as Tn = (n**2 + n) / 2.

def euler12()
maxInt = 1 << 64
(500..maxInt).each{|val|
numT = (val**2 + val)/2
limit = Math.sqrt(numT)
stepVal = (0 == numT % 2) ? 1 : 2
divs = 0
(1..limit).step(stepVal){ |x|
if 0 == numT % x then
divs += 2
end
}
return numT if divs > 500
}
end
Problem 13: Find the first 10 digits (most significant) of the sum of 100 50-digit numbers.

def euler13
numArr = [...]
sum = numArr.inject(0){|s,n| n+s}
val=sum.to_s.slice(0..9)
end
Problem 14: Find the starting number, under 1,000,000, that produces the longest chain for the Collatz problem.

The Collatz conjecture, or problem, generates a chain of numbers that terminates at 1. The chain is generated as Ni+1 = { Ni / 2 if Ni is even, 3 Ni + 1 if Ni is odd. At first read this was an "egads!" problem. But after looking at it was depressing easy to solve. The immediate solution that came to mind is a recursive algorithm using memoization. As follows, this runs in just under 59 seconds on my computer.

def findLen(start, cache)
len = cache[start]
if len
return start, len
else
nextVal = (0 == start%2) ? start / 2 : 3 * start + 1
nextVal, len = findLen(nextVal, cache)
cache[start] = len + 1
return start, len +1
end
end

def euler14()
cache = { 1 => 1 }
maxVal = 1
maxLen = 1

(2..1000000).each{ |start|
val, len = findLen(start, cache)

if maxLen < len
maxLen = len
maxVal = val
end
}

return maxVal, maxLen
end
I came up with an iterative approach as well. This is the same algorithm as the recursive solution, but it pushes values with unknown chain length onto a stack, instead of pushing them onto the call stack. This runs in just over 10 seconds which give a good indication on how expensive function calls can be.

def euler14_b()
cache = {1 => 1}
maxVal = 1
maxLen = 1
stack = []

(2..1000000).each{ |start|

val = start
len = cache[val]
while !len
stack.push(val)
val = (0 == val%2) ? val / 2 : 3 * val + 1
len = cache[val]
end

val = stack.pop
while val
len += 1
cache[val] = len
val = stack.pop
end

if maxLen < len
maxLen = len
maxVal = start
end
}

return maxVal, maxLen

end
Problem 15: How many routes are there through a 20×20 grid?

This was the most interesting problem out of this set. My first idea was to start at the end and generate valid paths back to the start. My second idea was, hey this is a tree, I should be able to calculate the number of paths directly. I thought that the number of paths should be proportional to the height of the tree. A good initial thought, but a little time at the white board showed that it was incorrect. A Google search turned up a blog entries by James Horsley and Real Ultimate Programming, after reading them and cracking open my dusty old discrete math books the answer is 40! / (20! * 20!) or C(40, 20).

To describe the answer in a bit more detail. One must always make 40 moves to get from the start to the finish. 20 moves down and 20 moves right. This gives 40! possibilities (20 down + 20 right). But since moves are indistinguishable, unordered, and without replacement, the total is reduced by 20! ways to select down moves, and 20! ways to select right moves. Or 40! / (20! * 20!).

Monday, December 22, 2008

Project Euler problems 6 - 10

I finally found some time to sit down and do problems 6 through 10 on Project Euler.

Problem 6: Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
def euler6()
(1..100).inject(0){|val,k| val += k} ** 2 -
(1..100).inject(0){|val,k| val += k**2}
end

Problem 7: What is the 10001st prime number?
def euler7()
myprime = Prime.new
(1..10000).each{ myprime.next}
myprime.next
end

Problem 8: Find the greatest product of five consecutive digits in the 1000-digit number.

The 1000 digit number is a string in my code.
def euler8(bigNum)
len = bigNum.size
stopAt = len - 5
sliceStart = 0
res = 0
while sliceStart < stopAt do
val = 1
s = bigNum.slice(sliceStart, 5)
s.each_char{|x| val *= x.to_i}
res = val if res < val
sliceStart += 1
end
res
end

Problem 9: There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
def euler9()
val = 0
(1..500).each{ |a|
(a..500).each{ |b|
c = (a**2 + b**2)**0.5
next unless c == c.to_i # continue unless c is an integer
val = a + b + c
return a * b * c if val == 1000
break if val > 1000
}
}

end

Project 10: Find the sum of all the primes below two million.

This was the only really challenging problem in this set. I tried to use Ruby's Prime class in a loop for this. When ran for 50,000 iterations it would return in about 14 seconds. For 2,000,000 I eventually killed the process after 15 minutes. The algorithm used in the Prime class seems to fall down when used to generate large lists of prime numbers.

I did some searching and found that most people were using some type of number sieve to generate the list of prime numbers, see Wikipedia here and here. I found and shamelessly stole a Ruby implementation of the Sieve of Eratosthenes from Paul Callagher. I slightly modified Paul's implementation to make it a standalone function instead of a class method.
# problem 10, find the sum of all primes below 2,000,000.
# prime series up to this limit, using Sieve of Eratosthenes method
# http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
#
# from http://tardate.blogspot.com/2008/10/rolling-project-euler-on-ruby.html
def sieve_of_eratosthenes( maxVal )
limit = Math.sqrt(maxVal)
a = (2..maxVal).to_a
n = 2
while (n < limit) do
x = n*2
begin
a[x-2]=2
x+=n
end until (x > maxVal )
begin
n+=1
end until ( a[n-2] != 2 )
end
a.uniq!
end

def euler10()
sieve_of_eratosthenes( 2_000_000 ).inject(0){|val, x| val += x }
end

This code returns in about 4 seconds.

Wednesday, November 26, 2008

Project Euler

I recently found out about Project Euler which has a collection of math/programming problems to solve. I have started working through the problems using Ruby. I have decided on using Ruby in my current project, which I will do some posts on later, but I don't have any real experience using it, so this will give me something to work with while learning the language.

The first problem: Sum all integers less than 1,000 that are multiples of 3 or 5. This is my third version and I have it down to a single line.
(1..999).inject(0){|result, n|
(0==n%3 or 0==n%5)? result + n : result}


The second problem: Sum all even numbers in the Fibonacci sequence that do not exceed 4,000,000. My first solution uses a recursive algorithm.
def euler2_rec(n1=1, n2=1)
return 0 if n1 > 4000000
fib = n1 + n2
sum = euler2_rec(n2, fib)
sum += fib if 0 == fib % 2
return sum
end

Since this is tail recursive and any tail recursive algorithm can be converted to a loop. I decided to create a loop version.
def euler2_loop
n1 = 1
n2 = 1
sum = 0
begin
fib = n1 + n2
sum += fib if 0 == fib % 2
n1 = n2
n2 = fib
end while fib < 4000000
return sum
end

The third problem: Find the largest prime factor of 600,851,475,143. For this I used Ruby's Prime class.
def euler3()
num = 600851475143
primes = Prime.new
factor = 0
while num > 1 do
factor = primes.next
num /= factor if 0 == num % factor
end
return factor
end

I had originally used num / 2 as my halting condition. But reading the comments after solving the problem I saw that others divided out the factors as they found them. This allows the loop to exit after the largest factor is found, and is rather obvious once you see it.

The fourth problem
: Find the largest palindromic number that is made from the product of two 3-digit numbers.
def euler4()
result = 0
100.upto(999) { |i|
i.upto(999) { |j|
val = i * j
result = val if result < val and
val.to_s == val.to_s.reverse
}}
return result
end

The fifth problem: Find the smallest number that is evenly divisible by all numbers between 1 and 20.

The easy and obvious approach for this problem is to take each number and test it to see if it evenly divisible by the numbers 2 to 20, all integers being evenly divisible by 1. One can think of some optimization that can be applied to this approach. For example a number must end in a 0 to be evenly divisible by 10. Also any number that is evenly divisible by 20 is also divisible by 10, 5, 4, and 2. Also one can consider that all, non prime, numbers can be factored into prime numbers. So the problem can be reduced to working with a set of prime numbers.

However, I wasn't sure of the correct approach, and being too lazy to get off of the couch to crack open a dusty old math book, I opted for brute force and ignorance and wrote program to iteratively check all numbers. While that was chewing up my CPU in the background I searched for a better solution and found this blog post on the problem. The solution proposed in that post is to find the accumulated least common multiple (LCM) for the numbers 2 to 20. A bit more searching turned up this page which had sample code, in Ruby, which would calculate the LCM and greatest common factor (GCF). So a little bit of time later, and with my original code still running, I had this solution.
require "gcf_lcm.rb"
def euler5()
lcm = calc_lcm(2,3, calc_gcf(2,3) )
4.upto(20) { |n|
lcm = calc_lcm(lcm, n, calc_gcf(lcm, n) ) }
return lcm
end

This returns almost instantly, and with the correct answer. When looking through the comments on this problem I found a posting with a Ruby solution. It turns out there are library routines in Ruby for LCM and GCF and the solution can be turned into a one liner.
(2..20).inject(1){|res,n| res.lcm n}