Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers
by zyzzy on Friday January 06, 2012 @08:56PM
(#3435913)
If someone had some experience with pascal triangle, they could come up with a very efficient solution, that might put them at a advantage. If the interviewer valued efficient solutions.
So far
dspeyer, has one of the most efficient algorithms.
def pascal(i):
o=[1]
for j in xrange(1,i):
o.append(o[-1] * (i - j) / j)
return o
in ruby this could be
def pascal2(i)
o=[1]
i += 1
for j in 1.upto(i - 1 )
o << o[-1] * (i - j)/j
end
o
end
we could make it even more efficient if we remember a there is symmetry in a pascal triangle. So that we don't have to calculate all the values in a row, maybe just half of the values. We have to calculate the row a little different if it is even or odd in number.
def pascal(i)
o=[1]
i += 1
if i == 1 then
return o
end
i.even? ? d = i / 2 - 1 : d = i / 2
for j in 1.upto(d )
o << o[-1] * (i - j)/j
end
o[0..i/2] + o[0..i/2 - 1].reverse
end
So far it up to 2x as fast. I wrote this method in Ruby to time the different methods, pascal, pascal2.
def timeA(m,n)
begin_time = Time.now
send(m,n)
end_time = Time.now
p "Time elapased #{(end_time - begin_time)*1000} milliseconds"
end
So far dspeyer, has one of the most efficient algorithms.
in ruby this could be we could make it even more efficient if we remember a there is symmetry in a pascal triangle. So that we don't have to calculate all the values in a row, maybe just half of the values. We have to calculate the row a little different if it is even or odd in number. So far it up to 2x as fast. I wrote this method in Ruby to time the different methods, pascal, pascal2. these are the times I get