Sha256: 717e12e026a70f9b37836a7fd29979de22bd81db4ddd82afc9d2860cd5ee41c5

Contents?: true

Size: 1.04 KB

Versions: 339

Compression:

Stored size: 1.04 KB

Contents

module BookKeeping
  VERSION = 2
end

class BaseConverter
  def self.convert(base_from, number_array, base_to)
    fail ArgumentError if invalid_inputs?(base_from, number_array, base_to)
    return [] unless number_array.any?
    number_in_canonical_base = convert_to_canonical_base(number_array, base_from)
    convert_from_canonical_base(number_in_canonical_base, base_to)
  end

  private_class_method

  def self.invalid_inputs?(base_from, number_array, base_to)
    number_array.any? { |number| number < 0 || number >= base_from } ||
      base_from <= 1 || base_to <= 1
  end

  def self.convert_to_canonical_base(number_array, base)
    total = 0
    number_array.reverse.each_with_index do |number, index|
      total += number * base**index
    end
    total
  end

  def self.convert_from_canonical_base(number, base_to)
    result = []
    current_number = number
    while current_number >= base_to
      result << current_number % base_to
      current_number /= base_to
    end
    result << current_number % base_to
    result.reverse
  end
end

Version data entries

339 entries across 339 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.138 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.137 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.136 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.135 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.134 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.133 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.132 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.131 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.130 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.129 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.128 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.127 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.126 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.125 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.124 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.123 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.122 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.121 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.120 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb