这是我碰到一道面试题,当时时间紧我用的字符串分隔实现的,其具体要求为:
将一非负整数转化为int[],如输入1234得到int[4]{ 1, 2, 3, 4 }。
今晚有兴致运用TDD方法做了个不转字符串的版本,主是依靠浮点数转为整数时丢失小数位值这一特性实现的。
*注:代码编写过程中单元测试与实现代码是相互交替的,并不像下面看到的分隔得那么明显。
2、单元测试:
[TestFixture]
public class IntToArrayHelperTests : IntToArrayHelper
{
[Test]
public void TestGetNumberAt()
{
Assert.AreEqual(0, GetNumberAt(0, 1));
Assert.AreEqual(1, GetNumberAt(1, 1));
Assert.AreEqual(1, GetNumberAt(21, 1));
Assert.AreEqual(1, GetNumberAt(10001, 1));
Assert.AreEqual(2, GetNumberAt(21, 2));
Assert.AreEqual(1, GetNumberAt(87654321, 1));
Assert.AreEqual(2, GetNumberAt(87654321, 2));
Assert.AreEqual(7, GetNumberAt(87654321, 7));
Assert.AreEqual(8, GetNumberAt(87654321, 8));
}
[Test]
public void TestGetDigitCount()
{
Assert.AreEqual(1, GetDigitCount(0));
Assert.AreEqual(1, GetDigitCount(1));
Assert.AreEqual(1, GetDigitCount(9));
Assert.AreEqual(2, GetDigitCount(10));
Assert.AreEqual(10, GetDigitCount(Int32.MaxValue));
}
[Test]
public void TestConvert()
{
int[] a = new int[1] { 0 };
Assert.AreEqual(a, Convert(0));
int[] b = new int[1] { 1 };
Assert.AreEqual(b, Convert(1));
int[] c = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
Assert.AreEqual(c, Convert(1234567890));
int[] d = new int[9] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Assert.AreEqual(d, Convert(987654321));
int[] max = new int[10] { 2, 1, 4, 7, 4, 8, 3, 6, 4, 7 };
Assert.AreEqual(max, Convert(Int32.MaxValue));
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestConvertWithNegative()
{
Convert(-1);
}
}
public class IntToArrayHelperTests : IntToArrayHelper
{
[Test]
public void TestGetNumberAt()
{
Assert.AreEqual(0, GetNumberAt(0, 1));
Assert.AreEqual(1, GetNumberAt(1, 1));
Assert.AreEqual(1, GetNumberAt(21, 1));
Assert.AreEqual(1, GetNumberAt(10001, 1));
Assert.AreEqual(2, GetNumberAt(21, 2));
Assert.AreEqual(1, GetNumberAt(87654321, 1));
Assert.AreEqual(2, GetNumberAt(87654321, 2));
Assert.AreEqual(7, GetNumberAt(87654321, 7));
Assert.AreEqual(8, GetNumberAt(87654321, 8));
}
[Test]
public void TestGetDigitCount()
{
Assert.AreEqual(1, GetDigitCount(0));
Assert.AreEqual(1, GetDigitCount(1));
Assert.AreEqual(1, GetDigitCount(9));
Assert.AreEqual(2, GetDigitCount(10));
Assert.AreEqual(10, GetDigitCount(Int32.MaxValue));
}
[Test]
public void TestConvert()
{
int[] a = new int[1] { 0 };
Assert.AreEqual(a, Convert(0));
int[] b = new int[1] { 1 };
Assert.AreEqual(b, Convert(1));
int[] c = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
Assert.AreEqual(c, Convert(1234567890));
int[] d = new int[9] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Assert.AreEqual(d, Convert(987654321));
int[] max = new int[10] { 2, 1, 4, 7, 4, 8, 3, 6, 4, 7 };
Assert.AreEqual(max, Convert(Int32.MaxValue));
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestConvertWithNegative()
{
Convert(-1);
}
}
3、实现代码:
public class IntToArrayHelper
{
protected int GetNumberAt(int value, int digit)
{
int contrastNumber = (int)Math.Pow(10, digit);
int digitCutter = (int)Math.Pow(10, digit - 1);
return (value - value / contrastNumber * contrastNumber) / digitCutter;
}
protected int GetDigitCount(int value)
{
if (value == 0)
return 1;
return (int)Math.Log10(value) + 1;
}
public int[] Convert(int value)
{
if (value < 0)
throw new ArgumentOutOfRangeException("value", "Not less than zero.");
int digitCount = GetDigitCount(value);
int[] result = new int[digitCount];
for (int i = 0; i < digitCount; ++i)
{
int index = digitCount - i - 1;
result[index] = GetNumberAt(value, i + 1);
}
return result;
}
}
{
protected int GetNumberAt(int value, int digit)
{
int contrastNumber = (int)Math.Pow(10, digit);
int digitCutter = (int)Math.Pow(10, digit - 1);
return (value - value / contrastNumber * contrastNumber) / digitCutter;
}
protected int GetDigitCount(int value)
{
if (value == 0)
return 1;
return (int)Math.Log10(value) + 1;
}
public int[] Convert(int value)
{
if (value < 0)
throw new ArgumentOutOfRangeException("value", "Not less than zero.");
int digitCount = GetDigitCount(value);
int[] result = new int[digitCount];
for (int i = 0; i < digitCount; ++i)
{
int index = digitCount - i - 1;
result[index] = GetNumberAt(value, i + 1);
}
return result;
}
}