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.159 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.158 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.157 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.156 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.155 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.154 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.153 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.152 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.151 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.150 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.149 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.148 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.147 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.146 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.145 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.144 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.143 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.142 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.141 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb
trackler-2.2.1.140 tracks/ruby/exercises/all-your-base/.meta/solutions/all_your_base.rb