News for hackers. Stuff that matters.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by diego on Thursday January 05, 2012 @04:56PM (#3430737)
Here's my naive Ruby version for what it's worth. It took me way too long, perhaps because I have a nasty cold. I've been programming since 1983. If I showed up for a code interview today, I'd probably fail miserably.

  def print_pascal(rows)
    row=[1, 0]
    (1..rows).each do |r|
      next_row = Array.new(r + 1)
      next_row[0] = 1 
      (1..row.length-1).each do |i|
        next_row[i] = row[i] + row[i-1]
      end
      print row[0..-2], "\n"
      row = next_row << 0 
    end
  end

  print_pascal(100)
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by mbernstein on Thursday January 05, 2012 @06:33PM (#3431133)
Couple minutes in ruby without using the knowledge of the binomial theorem or coefficients.

  def p(row)
    [1,Range.new(0, row.length - 2).map {|l|     
      row[l] + row[l+1] 
      }, 1].flatten
  end

  a = [1]
  while true
    a = p(a)
    puts a.inspect
  end

edit: fixed formatting, sorry :(
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by nileshtrivedi on Friday January 06, 2012 @01:24AM (#3432265)
Here is my version. Took me less than 5 minutes and worked the first time:

  def pascal(num)
    return [1] if num == 1
    res = []
    prev = [0] + pascal(num-1) + [0]
    (1..num).to_a.each { |i| res << (prev[i-1] + prev[i]) }
    return res
  end
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers by mbernstein on Friday January 06, 2012 @02:03AM (#3432358)
Recursion in this case - generally in ruby as well - is not a great idea. Also, you have quite high memory requirements: O(n^2)

O(n) is quite easy in this problem.