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