-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Deprecate binary, trinary, octal and hexadecimal exercises
- Loading branch information
1 parent
e609512
commit c840475
Showing
11 changed files
with
281 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class AllYourBaseTest | ||
{ | ||
[Test] | ||
public void Single_bit_one_to_decimal() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1 }; | ||
const int outputBase = 10; | ||
var outputDigits = new [] { 1 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Binary_to_single_decimal() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1, 0, 1 }; | ||
const int outputBase = 10; | ||
var outputDigits = new [] { 5 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Single_decimal_to_binary() | ||
{ | ||
const int inputBase = 10; | ||
var inputDigits = new [] { 5 }; | ||
const int outputBase = 2; | ||
var outputDigits = new [] { 1, 0, 1 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Binary_to_multiple_decimal() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1, 0, 1, 0, 1, 0 }; | ||
const int outputBase = 10; | ||
var outputDigits = new [] { 4, 2 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Decimal_to_binary() | ||
{ | ||
const int inputBase = 10; | ||
var inputDigits = new [] { 4, 2 }; | ||
const int outputBase = 2; | ||
var outputDigits = new [] { 1, 0, 1, 0, 1, 0 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Trinary_to_hexadecimal() | ||
{ | ||
const int inputBase = 3; | ||
var inputDigits = new [] { 1, 1, 2, 0 }; | ||
const int outputBase = 16; | ||
var outputDigits = new [] { 2, 10 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Hexadecimal_to_trinary() | ||
{ | ||
const int inputBase = 16; | ||
var inputDigits = new [] { 2, 10 }; | ||
const int outputBase = 3; | ||
var outputDigits = new [] { 1, 1, 2, 0 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Using_15_bit_integer() | ||
{ | ||
const int inputBase = 97; | ||
var inputDigits = new [] { 3, 46, 60 }; | ||
const int outputBase = 73; | ||
var outputDigits = new [] { 6, 10, 45 }; | ||
Assert.That(Base.Rebase(inputBase, inputDigits, outputBase), Is.EqualTo(outputDigits)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Empty_array() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new int[0]; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Single_zero() | ||
{ | ||
const int inputBase = 10; | ||
var inputDigits = new [] { 0 }; | ||
const int outputBase = 2; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Multiple_zeros() | ||
{ | ||
const int inputBase = 10; | ||
var inputDigits = new [] { 0, 0, 0 }; | ||
const int outputBase = 2; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Leading_zeros() | ||
{ | ||
const int inputBase = 7; | ||
var inputDigits = new [] { 0, 6, 0 }; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Test] | ||
public void Negative_digit() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1, -1, 1, 0, 1, 0 }; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Invalid_positive_digit() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1, 2, 1, 0, 1, 0 }; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void First_base_is_one() | ||
{ | ||
const int inputBase = 1; | ||
var inputDigits = new int[0]; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Second_base_is_one() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1, 0, 1, 0, 1, 0 }; | ||
const int outputBase = 1; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void First_base_is_zero() | ||
{ | ||
const int inputBase = 0; | ||
var inputDigits = new int[0]; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Second_base_is_zero() | ||
{ | ||
const int inputBase = 10; | ||
var inputDigits = new [] { 7 }; | ||
const int outputBase = 0; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void First_base_is_negative() | ||
{ | ||
const int inputBase = -2; | ||
var inputDigits = new [] { 1 }; | ||
const int outputBase = 10; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Second_base_is_negative() | ||
{ | ||
const int inputBase = 2; | ||
var inputDigits = new [] { 1 }; | ||
const int outputBase = -7; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
[Test] | ||
public void Both_bases_are_negative() | ||
{ | ||
const int inputBase = -2; | ||
var inputDigits = new [] { 1 }; | ||
const int outputBase = -7; | ||
Assert.Throws<ArgumentException>(() => Base.Rebase(inputBase, inputDigits, outputBase)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public static class Base | ||
{ | ||
public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase) | ||
{ | ||
if (inputBase < 2) throw new ArgumentException("Invalid input base."); | ||
if (outputBase < 2) throw new ArgumentException("Invalid output base."); | ||
if (inputDigits.Length == 0) throw new ArgumentException("Empty input digits."); | ||
|
||
return ToDigits(outputBase, FromDigits(inputBase, inputDigits)); | ||
} | ||
|
||
private static int FromDigits(int fromBase, int[] fromDigits) | ||
{ | ||
return fromDigits.Aggregate(0, (acc, x) => | ||
{ | ||
if (x < 0 || x >= fromBase || (x == 0 & acc == 0)) throw new ArgumentException("Invalid input digit"); | ||
|
||
return acc*fromBase + x; | ||
}); | ||
} | ||
|
||
private static int[] ToDigits(int toBase, int x) | ||
{ | ||
var digits = new List<int>(); | ||
var remainder = x; | ||
var multiplier = 1; | ||
|
||
while (remainder > 0) | ||
{ | ||
multiplier *= toBase; | ||
|
||
var value = remainder % multiplier; | ||
var digit = value/(multiplier/toBase); | ||
|
||
digits.Add(digit); | ||
remainder -= value; | ||
} | ||
|
||
digits.Reverse(); | ||
return digits.ToArray(); | ||
} | ||
} |
Oops, something went wrong.