Skip to content

Commit

Permalink
Add all-your-base exercise
Browse files Browse the repository at this point in the history
- Deprecate binary, trinary, octal and hexadecimal exercises
  • Loading branch information
ErikSchierboom committed Aug 31, 2016
1 parent e609512 commit c840475
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 295 deletions.
54 changes: 13 additions & 41 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@
"etl",
"phone-number",
"strain",
"binary",
"scrabble-score",
"robot-name",
"kindergarten-garden",
"clock",
"triangle",
"octal",
"beer-song",
"trinary",
"sieve",
"queen-attack",
"hexadecimal",
"robot-simulator",
"secret-handshake",
"allergies",
Expand All @@ -48,6 +44,7 @@
"word-count",
"prime-factors",
"meetup",
"all-your-base",
"pascals-triangle",
"simple-cipher",
"roman-numerals",
Expand Down Expand Up @@ -197,15 +194,6 @@
"Filtering"
]
},
{
"slug": "binary",
"difficulty": 3,
"topics": [
"Integers",
"Parsing",
"Transforming"
]
},
{
"slug": "scrabble-score",
"difficulty": 3,
Expand Down Expand Up @@ -246,15 +234,6 @@
"Enumerations"
]
},
{
"slug": "octal",
"difficulty": 3,
"topics": [
"Integers",
"Parsing",
"Transforming"
]
},
{
"slug": "beer-song",
"difficulty": 3,
Expand All @@ -263,15 +242,6 @@
"Algorithms"
]
},
{
"slug": "trinary",
"difficulty": 3,
"topics": [
"Integers",
"Parsing",
"Transforming"
]
},
{
"slug": "sieve",
"difficulty": 3,
Expand All @@ -287,15 +257,6 @@
"Classes"
]
},
{
"slug": "hexadecimal",
"difficulty": 3,
"topics": [
"Integers",
"Parsing",
"Transforming"
]
},
{
"slug": "robot-simulator",
"difficulty": 3,
Expand Down Expand Up @@ -420,6 +381,14 @@
"Dates"
]
},
{
"slug": "all-your-base",
"difficulty": 4,
"topics": [
"Integers",
"Transforming"
]
},
{
"slug": "pascals-triangle",
"difficulty": 4,
Expand Down Expand Up @@ -541,7 +510,10 @@
}
],
"deprecated": [

"binary",
"trinary",
"octal",
"hexadecimal"
],
"ignored": [
"docs",
Expand Down
222 changes: 222 additions & 0 deletions exercises/all-your-base/AllYourBaseTest.cs
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));
}
}
46 changes: 46 additions & 0 deletions exercises/all-your-base/Example.cs
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();
}
}
Loading

0 comments on commit c840475

Please sign in to comment.