Skip to content

Commit e50b1cb

Browse files
sebarmanoInsti
authored andcommitted
Add all-your-base exercise tests and example (#403)
1 parent 502cc91 commit e50b1cb

File tree

3 files changed

+338
-1
lines changed

3 files changed

+338
-1
lines changed

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
"perfect-numbers",
8484
"connect",
8585
"list-ops",
86-
"diamond"
86+
"diamond",
87+
"all-your-base"
8788
],
8889
"deprecated": [
8990
"octal",
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
gem 'minitest', '>= 5.0.0'
2+
require 'minitest/autorun'
3+
require_relative 'all_your_base'
4+
5+
class AllYourBaseTest < Minitest::Test
6+
def test_single_bit_to_one_decimal
7+
digits = [1]
8+
input_base = 2
9+
output_base = 10
10+
expected = [1]
11+
12+
converted = BaseConverter.convert(input_base, digits, output_base)
13+
14+
assert_equal expected, converted,
15+
"Input base: #{input_base}, output base #{output_base}. " \
16+
"Expected #{expected} but got #{converted}."
17+
end
18+
19+
def test_binary_to_single_decimal
20+
skip
21+
digits = [1, 0, 1]
22+
input_base = 2
23+
output_base = 10
24+
expected = [5]
25+
26+
converted = BaseConverter.convert(input_base, digits, output_base)
27+
28+
assert_equal expected, converted,
29+
"Input base: #{input_base}, output base #{output_base}. " \
30+
"Expected #{expected} but got #{converted}."
31+
end
32+
33+
def test_single_decimal_to_binary
34+
skip
35+
digits = [5]
36+
input_base = 10
37+
output_base = 2
38+
expected = [1, 0, 1]
39+
40+
converted = BaseConverter.convert(input_base, digits, output_base)
41+
42+
assert_equal expected, converted,
43+
"Input base: #{input_base}, output base #{output_base}. " \
44+
"Expected #{expected} but got #{converted}."
45+
end
46+
47+
def test_binary_to_multiple_decimal
48+
skip
49+
digits = [1, 0, 1, 0, 1, 0]
50+
input_base = 2
51+
output_base = 10
52+
expected = [4, 2]
53+
54+
converted = BaseConverter.convert(input_base, digits, output_base)
55+
56+
assert_equal expected, converted,
57+
"Input base: #{input_base}, output base #{output_base}. " \
58+
"Expected #{expected} but got #{converted}."
59+
end
60+
61+
def test_decimal_to_binary
62+
skip
63+
digits = [4, 2]
64+
input_base = 10
65+
output_base = 2
66+
expected = [1, 0, 1, 0, 1, 0]
67+
68+
converted = BaseConverter.convert(input_base, digits, output_base)
69+
70+
assert_equal expected, converted,
71+
"Input base: #{input_base}, output base #{output_base}. " \
72+
"Expected #{expected} but got #{converted}."
73+
end
74+
75+
def test_trinary_to_hexadecimal
76+
skip
77+
digits = [1, 1, 2, 0]
78+
input_base = 3
79+
output_base = 16
80+
expected = [2, 10]
81+
82+
converted = BaseConverter.convert(input_base, digits, output_base)
83+
84+
assert_equal expected, converted,
85+
"Input base: #{input_base}, output base #{output_base}. " \
86+
"Expected #{expected} but got #{converted}."
87+
end
88+
89+
def test_hexadecimal_to_trinary
90+
skip
91+
digits = [2, 10]
92+
input_base = 16
93+
output_base = 3
94+
expected = [1, 1, 2, 0]
95+
96+
converted = BaseConverter.convert(input_base, digits, output_base)
97+
98+
assert_equal expected, converted,
99+
"Input base: #{input_base}, output base #{output_base}. " \
100+
"Expected #{expected} but got #{converted}."
101+
end
102+
103+
def test_15_bit_integer
104+
skip
105+
digits = [3, 46, 60]
106+
input_base = 97
107+
output_base = 73
108+
expected = [6, 10, 45]
109+
110+
converted = BaseConverter.convert(input_base, digits, output_base)
111+
112+
assert_equal expected, converted,
113+
"Input base: #{input_base}, output base #{output_base}. " \
114+
"Expected #{expected} but got #{converted}."
115+
end
116+
117+
def test_empty_list
118+
skip
119+
digits = []
120+
input_base = 2
121+
output_base = 10
122+
expected = []
123+
124+
converted = BaseConverter.convert(input_base, digits, output_base)
125+
126+
assert_equal expected, converted,
127+
"Input base: #{input_base}, output base #{output_base}. " \
128+
"Expected #{expected} but got #{converted}."
129+
end
130+
131+
def test_single_zero
132+
skip
133+
digits = [0]
134+
input_base = 10
135+
output_base = 2
136+
expected = [0]
137+
138+
converted = BaseConverter.convert(input_base, digits, output_base)
139+
140+
assert_equal expected, converted,
141+
"Input base: #{input_base}, output base #{output_base}. " \
142+
"Expected #{expected} but got #{converted}."
143+
end
144+
145+
def test_multiple_zeroes
146+
skip
147+
digits = [0, 0, 0]
148+
input_base = 10
149+
output_base = 2
150+
expected = [0]
151+
152+
converted = BaseConverter.convert(input_base, digits, output_base)
153+
154+
assert_equal expected, converted,
155+
"Input base: #{input_base}, output base #{output_base}. " \
156+
"Expected #{expected} but got #{converted}."
157+
end
158+
159+
def test_leading_zeros
160+
skip
161+
digits = [0, 6, 0]
162+
input_base = 7
163+
output_base = 10
164+
expected = [4, 2]
165+
166+
converted = BaseConverter.convert(input_base, digits, output_base)
167+
168+
assert_equal expected, converted,
169+
"Input base: #{input_base}, output base #{output_base}. " \
170+
"Expected #{expected} but got #{converted}."
171+
end
172+
173+
def test_negative_digit
174+
skip
175+
digits = [1, -1, 1, 0, 1, 0]
176+
input_base = 2
177+
output_base = 10
178+
expected = nil
179+
180+
converted = BaseConverter.convert(input_base, digits, output_base)
181+
182+
assert_equal expected, converted,
183+
"Input base: #{input_base}, output base #{output_base}. " \
184+
"Expected #{expected} but got #{converted}."
185+
end
186+
187+
def test_invalid_positive_digit
188+
skip
189+
digits = [1, 2, 1, 0, 1, 0]
190+
input_base = 2
191+
output_base = 10
192+
expected = nil
193+
194+
converted = BaseConverter.convert(input_base, digits, output_base)
195+
196+
assert_equal expected, converted,
197+
"Input base: #{input_base}, output base #{output_base}. " \
198+
"Expected #{expected} but got #{converted}."
199+
end
200+
201+
def test_first_base_is_one
202+
skip
203+
digits = []
204+
input_base = 1
205+
output_base = 10
206+
expected = nil
207+
208+
converted = BaseConverter.convert(input_base, digits, output_base)
209+
210+
assert_equal expected, converted,
211+
"Input base: #{input_base}, output base #{output_base}. " \
212+
"Expected #{expected} but got #{converted}."
213+
end
214+
215+
def test_second_base_is_one
216+
skip
217+
digits = [1, 0, 1, 0, 1, 0]
218+
input_base = 2
219+
output_base = 1
220+
expected = nil
221+
222+
converted = BaseConverter.convert(input_base, digits, output_base)
223+
224+
assert_equal expected, converted,
225+
"Input base: #{input_base}, output base #{output_base}. " \
226+
"Expected #{expected} but got #{converted}."
227+
end
228+
229+
def test_first_base_is_zero
230+
skip
231+
digits = []
232+
input_base = 0
233+
output_base = 10
234+
expected = nil
235+
236+
converted = BaseConverter.convert(input_base, digits, output_base)
237+
238+
assert_equal expected, converted,
239+
"Input base: #{input_base}, output base #{output_base}. " \
240+
"Expected #{expected} but got #{converted}."
241+
end
242+
243+
def test_second_base_is_zero
244+
skip
245+
digits = [7]
246+
input_base = 10
247+
output_base = 0
248+
expected = nil
249+
250+
converted = BaseConverter.convert(input_base, digits, output_base)
251+
252+
assert_equal expected, converted,
253+
"Input base: #{input_base}, output base #{output_base}. " \
254+
"Expected #{expected} but got #{converted}."
255+
end
256+
257+
def test_first_base_is_negative
258+
skip
259+
digits = [1]
260+
input_base = -2
261+
output_base = 10
262+
expected = nil
263+
264+
converted = BaseConverter.convert(input_base, digits, output_base)
265+
266+
assert_equal expected, converted,
267+
"Input base: #{input_base}, output base #{output_base}. " \
268+
"Expected #{expected} but got #{converted}."
269+
end
270+
271+
def test_second_base_is_negative
272+
skip
273+
digits = [1]
274+
input_base = 2
275+
output_base = -7
276+
expected = nil
277+
278+
converted = BaseConverter.convert(input_base, digits, output_base)
279+
280+
assert_equal expected, converted,
281+
"Input base: #{input_base}, output base #{output_base}. " \
282+
"Expected #{expected} but got #{converted}."
283+
end
284+
285+
def test_both_bases_are_negative
286+
skip
287+
digits = [1]
288+
input_base = -2
289+
output_base = -7
290+
expected = nil
291+
292+
converted = BaseConverter.convert(input_base, digits, output_base)
293+
294+
assert_equal expected, converted,
295+
"Input base: #{input_base}, output base #{output_base}. " \
296+
"Expected #{expected} but got #{converted}."
297+
end
298+
299+
def test_bookkeeping
300+
skip
301+
assert_equal 1, BookKeeping::VERSION
302+
end
303+
end

exercises/all-your-base/example.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module BookKeeping
2+
VERSION = 1
3+
end
4+
5+
class BaseConverter
6+
def self.convert(base_from, number_array, base_to)
7+
return if number_array.any?{|number| number < 0 || number >= base_from}
8+
return if base_from <= 1 || base_to <= 1
9+
return [] unless number_array.any?
10+
number_in_canonical_base = convert_to_canonical_base(number_array, base_from)
11+
convert_from_canonical_base(number_in_canonical_base, base_to)
12+
end
13+
14+
private
15+
def self.convert_to_canonical_base(number_array, base)
16+
total = 0
17+
number_array.reverse.each_with_index do |number, index|
18+
total += number * base ** index
19+
end
20+
total
21+
end
22+
23+
def self.convert_from_canonical_base(number, base_to)
24+
result = []
25+
current_number = number
26+
while current_number >= base_to do
27+
result << current_number % base_to
28+
current_number = current_number / base_to
29+
end
30+
result << current_number % base_to
31+
result.reverse
32+
end
33+
end

0 commit comments

Comments
 (0)