Google Code Jam “Cookie Clicker”

This example was given as a spin-off to the Google Code Jam “Cookie Clicker” challenge.

  
class Investor
 
  def initialize(*args)
    @c, @f, @x = args
    @income = 2.0
    @seconds = 0
  end
 
  def solve
    return proper_seconds if (@x-@c)/@income < @x/(@income + @f)
    @seconds += @c/@income
    @income += @f
    solve
  end
 
  def proper_seconds
    (@seconds + @x/@income).round(7)
  end
 
end
 
values = [
  [30.0, 1.0, 2.0],
  [30.0, 2.0, 100.0],
  [30.5, 3.14159,1999.1999],
  [500.0, 4.0, 2000.0]      
]
 
values.each { |settings| puts Investor.new(*settings).solve }
  

“FizzBuzz” code test

Here is my simple answer to a typical test given during interviews

class FizzBuzz
 
  attr_reader :output
 
  def initialize(iterations = 100)
    @output = (1..iterations).map { |i| fizz_buzz_test(i) }
  end
 
  def fizz_buzz_test(iteration)
    case
    when iteration % 15 == 0
      'FizzBuzz'
    when iteration % 5 == 0
      'Buzz'
    when iteration % 3 == 0
      "Fizz"
    else
      iteration
    end
  end
 
  def print
    @output.join(', ')
  end
 
end