Skip to content

Commit c840475

Browse files
Add all-your-base exercise
- Deprecate binary, trinary, octal and hexadecimal exercises
1 parent e609512 commit c840475

11 files changed

Lines changed: 281 additions & 295 deletions

File tree

config.json

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121
"etl",
2222
"phone-number",
2323
"strain",
24-
"binary",
2524
"scrabble-score",
2625
"robot-name",
2726
"kindergarten-garden",
2827
"clock",
2928
"triangle",
30-
"octal",
3129
"beer-song",
32-
"trinary",
3330
"sieve",
3431
"queen-attack",
35-
"hexadecimal",
3632
"robot-simulator",
3733
"secret-handshake",
3834
"allergies",
@@ -48,6 +44,7 @@
4844
"word-count",
4945
"prime-factors",
5046
"meetup",
47+
"all-your-base",
5148
"pascals-triangle",
5249
"simple-cipher",
5350
"roman-numerals",
@@ -197,15 +194,6 @@
197194
"Filtering"
198195
]
199196
},
200-
{
201-
"slug": "binary",
202-
"difficulty": 3,
203-
"topics": [
204-
"Integers",
205-
"Parsing",
206-
"Transforming"
207-
]
208-
},
209197
{
210198
"slug": "scrabble-score",
211199
"difficulty": 3,
@@ -246,15 +234,6 @@
246234
"Enumerations"
247235
]
248236
},
249-
{
250-
"slug": "octal",
251-
"difficulty": 3,
252-
"topics": [
253-
"Integers",
254-
"Parsing",
255-
"Transforming"
256-
]
257-
},
258237
{
259238
"slug": "beer-song",
260239
"difficulty": 3,
@@ -263,15 +242,6 @@
263242
"Algorithms"
264243
]
265244
},
266-
{
267-
"slug": "trinary",
268-
"difficulty": 3,
269-
"topics": [
270-
"Integers",
271-
"Parsing",
272-
"Transforming"
273-
]
274-
},
275245
{
276246
"slug": "sieve",
277247
"difficulty": 3,
@@ -287,15 +257,6 @@
287257
"Classes"
288258
]
289259
},
290-
{
291-
"slug": "hexadecimal",
292-
"difficulty": 3,
293-
"topics": [
294-
"Integers",
295-
"Parsing",
296-
"Transforming"
297-
]
298-
},
299260
{
300261
"slug": "robot-simulator",
301262
"difficulty": 3,
@@ -420,6 +381,14 @@
420381
"Dates"
421382
]
422383
},
384+
{
385+
"slug": "all-your-base",
386+
"difficulty": 4,
387+
"topics": [
388+
"Integers",
389+
"Transforming"
390+
]
391+
},
423392
{
424393
"slug": "pascals-triangle",
425394
"difficulty": 4,
@@ -541,7 +510,10 @@
541510
}
542511
],
543512
"deprecated": [
544-
513+
"binary",
514+
"trinary",
515+
"octal",
516+
"hexadecimal"
545517
],
546518
"ignored": [
547519
"docs",
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
[TestFixture]
5+
public class AllYourBaseTest
6+
{
7+
[Test]
8+
public void Single_bit_one_to_decimal()
9+
{
10+
const int inputBase = 2;
11+
var inputDigits = new [] { 1 };
12+
const int outputBase = 10;
13+
var outputDigits = new [] { 1 };
14+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
15+
}
16+
17+
[Ignore("Remove to run test")]
18+
[Test]
19+
public void Binary_to_single_decimal()
20+
{
21+
const int inputBase = 2;
22+
var inputDigits = new [] { 1, 0, 1 };
23+
const int outputBase = 10;
24+
var outputDigits = new [] { 5 };
25+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
26+
}
27+
28+
[Ignore("Remove to run test")]
29+
[Test]
30+
public void Single_decimal_to_binary()
31+
{
32+
const int inputBase = 10;
33+
var inputDigits = new [] { 5 };
34+
const int outputBase = 2;
35+
var outputDigits = new [] { 1, 0, 1 };
36+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
37+
}
38+
39+
[Ignore("Remove to run test")]
40+
[Test]
41+
public void Binary_to_multiple_decimal()
42+
{
43+
const int inputBase = 2;
44+
var inputDigits = new [] { 1, 0, 1, 0, 1, 0 };
45+
const int outputBase = 10;
46+
var outputDigits = new [] { 4, 2 };
47+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
48+
}
49+
50+
[Ignore("Remove to run test")]
51+
[Test]
52+
public void Decimal_to_binary()
53+
{
54+
const int inputBase = 10;
55+
var inputDigits = new [] { 4, 2 };
56+
const int outputBase = 2;
57+
var outputDigits = new [] { 1, 0, 1, 0, 1, 0 };
58+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
59+
}
60+
61+
[Ignore("Remove to run test")]
62+
[Test]
63+
public void Trinary_to_hexadecimal()
64+
{
65+
const int inputBase = 3;
66+
var inputDigits = new [] { 1, 1, 2, 0 };
67+
const int outputBase = 16;
68+
var outputDigits = new [] { 2, 10 };
69+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
70+
}
71+
72+
[Ignore("Remove to run test")]
73+
[Test]
74+
public void Hexadecimal_to_trinary()
75+
{
76+
const int inputBase = 16;
77+
var inputDigits = new [] { 2, 10 };
78+
const int outputBase = 3;
79+
var outputDigits = new [] { 1, 1, 2, 0 };
80+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
81+
}
82+
83+
[Ignore("Remove to run test")]
84+
[Test]
85+
public void Using_15_bit_integer()
86+
{
87+
const int inputBase = 97;
88+
var inputDigits = new [] { 3, 46, 60 };
89+
const int outputBase = 73;
90+
var outputDigits = new [] { 6, 10, 45 };
91+
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits));
92+
}
93+
94+
[Ignore("Remove to run test")]
95+
[Test]
96+
public void Empty_array()
97+
{
98+
const int inputBase = 2;
99+
var inputDigits = new int[0];
100+
const int outputBase = 10;
101+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
102+
}
103+
104+
[Ignore("Remove to run test")]
105+
[Test]
106+
public void Single_zero()
107+
{
108+
const int inputBase = 10;
109+
var inputDigits = new [] { 0 };
110+
const int outputBase = 2;
111+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
112+
}
113+
114+
[Ignore("Remove to run test")]
115+
[Test]
116+
public void Multiple_zeros()
117+
{
118+
const int inputBase = 10;
119+
var inputDigits = new [] { 0, 0, 0 };
120+
const int outputBase = 2;
121+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
122+
}
123+
124+
[Ignore("Remove to run test")]
125+
[Test]
126+
public void Leading_zeros()
127+
{
128+
const int inputBase = 7;
129+
var inputDigits = new [] { 0, 6, 0 };
130+
const int outputBase = 10;
131+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
132+
}
133+
134+
[Test]
135+
public void Negative_digit()
136+
{
137+
const int inputBase = 2;
138+
var inputDigits = new [] { 1, -1, 1, 0, 1, 0 };
139+
const int outputBase = 10;
140+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
141+
}
142+
143+
[Ignore("Remove to run test")]
144+
[Test]
145+
public void Invalid_positive_digit()
146+
{
147+
const int inputBase = 2;
148+
var inputDigits = new [] { 1, 2, 1, 0, 1, 0 };
149+
const int outputBase = 10;
150+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
151+
}
152+
153+
[Ignore("Remove to run test")]
154+
[Test]
155+
public void First_base_is_one()
156+
{
157+
const int inputBase = 1;
158+
var inputDigits = new int[0];
159+
const int outputBase = 10;
160+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
161+
}
162+
163+
[Ignore("Remove to run test")]
164+
[Test]
165+
public void Second_base_is_one()
166+
{
167+
const int inputBase = 2;
168+
var inputDigits = new [] { 1, 0, 1, 0, 1, 0 };
169+
const int outputBase = 1;
170+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
171+
}
172+
173+
[Ignore("Remove to run test")]
174+
[Test]
175+
public void First_base_is_zero()
176+
{
177+
const int inputBase = 0;
178+
var inputDigits = new int[0];
179+
const int outputBase = 10;
180+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
181+
}
182+
183+
[Ignore("Remove to run test")]
184+
[Test]
185+
public void Second_base_is_zero()
186+
{
187+
const int inputBase = 10;
188+
var inputDigits = new [] { 7 };
189+
const int outputBase = 0;
190+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
191+
}
192+
193+
[Ignore("Remove to run test")]
194+
[Test]
195+
public void First_base_is_negative()
196+
{
197+
const int inputBase = -2;
198+
var inputDigits = new [] { 1 };
199+
const int outputBase = 10;
200+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
201+
}
202+
203+
[Ignore("Remove to run test")]
204+
[Test]
205+
public void Second_base_is_negative()
206+
{
207+
const int inputBase = 2;
208+
var inputDigits = new [] { 1 };
209+
const int outputBase = -7;
210+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
211+
}
212+
213+
[Ignore("Remove to run test")]
214+
[Test]
215+
public void Both_bases_are_negative()
216+
{
217+
const int inputBase = -2;
218+
var inputDigits = new [] { 1 };
219+
const int outputBase = -7;
220+
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase));
221+
}
222+
}

exercises/all-your-base/Example.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public static class Base
6+
{
7+
public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase)
8+
{
9+
if (inputBase < 2) throw new ArgumentException("Invalid input base.");
10+
if (outputBase < 2) throw new ArgumentException("Invalid output base.");
11+
if (inputDigits.Length == 0) throw new ArgumentException("Empty input digits.");
12+
13+
return ToDigits(outputBase, FromDigits(inputBase, inputDigits));
14+
}
15+
16+
private static int FromDigits(int fromBase, int[] fromDigits)
17+
{
18+
return fromDigits.Aggregate(0, (acc, x) =>
19+
{
20+
if (x < 0 || x >= fromBase || (x == 0 & acc == 0)) throw new ArgumentException("Invalid input digit");
21+
22+
return acc*fromBase + x;
23+
});
24+
}
25+
26+
private static int[] ToDigits(int toBase, int x)
27+
{
28+
var digits = new List<int>();
29+
var remainder = x;
30+
var multiplier = 1;
31+
32+
while (remainder > 0)
33+
{
34+
multiplier *= toBase;
35+
36+
var value = remainder % multiplier;
37+
var digit = value/(multiplier/toBase);
38+
39+
digits.Add(digit);
40+
remainder -= value;
41+
}
42+
43+
digits.Reverse();
44+
return digits.ToArray();
45+
}
46+
}

0 commit comments

Comments
 (0)