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
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Randomly selected coin and lighter/heavier its value with Ruby: