Skip to content

Commit 5cce519

Browse files
committed
exercism-66 All your Base
1 parent 865f01b commit 5cce519

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
283283
63. [Perfect Numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/perfect_numbers.rb)
284284
64. [Binary Search](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/binary_search.rb)
285285
65. [Matching Brackets](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/matching_brackets.rb)
286+
66. [All Your Base](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/all_your_base.rb)
286287

287288
<a name="leetcode"/>
288289

exercism/all_your_base.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/all-your-base
2+
3+
# Solution:
4+
class BaseConverter
5+
def self.convert(input_base, digits, output_base)
6+
raise ArgumentError if input_base<=1 or output_base<=1
7+
return [0] if digits.empty?
8+
num = 0
9+
digits.reverse.each_with_index do |digit,idx|
10+
raise ArgumentError if digit<0 or digit>=input_base
11+
num+=(digit*(input_base**idx))
12+
end
13+
output = num==0 ? [0] : []
14+
until num==0
15+
num,remainder = num.divmod(output_base)
16+
output.push(remainder)
17+
end
18+
output = output.reverse
19+
end
20+
end
21+

0 commit comments

Comments
 (0)