loading...

re: Daily Challenge #108 - Find the Counterfeit Coin VIEW POST

FULL DISCUSSION
 

Randomly selected coin and lighter/heavier its value with Ruby:

# frozen_string_literal: true

# Save file as coins.rb and run `ruby coins.rb`
class Coins
  def initialize(place, value)
    @coins = Array.new(7, 1).insert(place, value)
    @count = 0
  end

  def weight
    return puts @count if @coins.size == 1

    @count += 1
    step = @coins.size / 2
    first = @coins.first(step)
    last = @coins.last(step)
    @coins = first.sum > last.sum ? first : last
    weight
  end
end

Coins.new(rand(0..7), [0, 2].sample).weight
code of conduct - report abuse