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.180 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.179 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.178 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.177 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.176 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.175 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.174 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.173 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.172 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.171 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.170 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.169 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.167 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.166 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.165 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.164 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.163 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.162 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.161 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.160 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb