Skip to content

A bit more idiomatic Ruby #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions exercises/all-your-base/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ def self.convert(base_from, number_array, base_to)
convert_from_canonical_base(number_in_canonical_base, base_to)
end

private
private_class_method

def self.invalid_inputs?(base_from, number_array, base_to)
number_array.any?{ |number| number < 0 || number >= base_from } ||
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
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 do
result << current_number % base_to
current_number = current_number / base_to
while current_number >= base_to
result << current_number % base_to
current_number /= base_to
end
result << current_number % base_to
result.reverse
Expand Down