diff --git a/exercises/luhn/Example.cs b/exercises/luhn/Example.cs index c5846c1d2d..852dcd0783 100644 --- a/exercises/luhn/Example.cs +++ b/exercises/luhn/Example.cs @@ -1,22 +1,23 @@ using System; using System.Linq; -public class Luhn +public static class Luhn { - private readonly long number; + public static bool IsValid(string number) + { + number = number.Replace(" ", ""); - public long CheckDigit { get { return number % 10; } } - public int[] Addends { get; private set; } - public int Checksum { get { return Addends.Sum(); } } - public bool Valid { get { return Checksum % 10 == 0; } } + if (number.Length < 2 || number.Any(c => c < '0' || c > '9')) + { + return false; + } - public Luhn(long number) - { - this.number = number; - Addends = GenerateAddends(); + var checksum = GenerateChecksum(number); + + return checksum % 10 == 0; } - private int[] GenerateAddends() + private static int GenerateChecksum(string number) { var reversedIntArray = SplitToReversedIntArray(number); for (int i = 1; i < reversedIntArray.Length; i++) @@ -25,12 +26,12 @@ private int[] GenerateAddends() reversedIntArray[i] = ConvertDigitForAddend(reversedIntArray[i]); } Array.Reverse(reversedIntArray); - return reversedIntArray; + return reversedIntArray.Sum(); } - private static int[] SplitToReversedIntArray(long value) + private static int[] SplitToReversedIntArray(string value) { - return value.ToString().Select(c => int.Parse(c.ToString())).Reverse().ToArray(); + return value.Select(c => int.Parse(c.ToString())).Reverse().ToArray(); } private static int ConvertDigitForAddend(int value) @@ -38,21 +39,4 @@ private static int ConvertDigitForAddend(int value) var doubled = value * 2; return doubled < 10 ? doubled : doubled - 9; } - - public static long Create(long number) - { - var zeroCheckDigitNumber = number * 10; - var luhn = new Luhn(zeroCheckDigitNumber); - - if (luhn.Valid) - return zeroCheckDigitNumber; - - return zeroCheckDigitNumber + CreateCheckDigit(luhn.Checksum); - } - - private static int CreateCheckDigit(int value) - { - var nearestTen = (int)(Math.Ceiling(value / 10.0m) * 10); - return nearestTen - value; - } } \ No newline at end of file diff --git a/exercises/luhn/LuhnTest.cs b/exercises/luhn/LuhnTest.cs index 07821b58a7..7f7c426bbc 100644 --- a/exercises/luhn/LuhnTest.cs +++ b/exercises/luhn/LuhnTest.cs @@ -3,60 +3,14 @@ [TestFixture] public class LuhnTest { - [Test] - public void Check_digit_is_the_rightmost_digit() - { - Assert.That(new Luhn(34567).CheckDigit, Is.EqualTo(7)); - } - - [Ignore("Remove to run test")] - [Test] - public void Addends_doubles_every_other_number_from_the_right() - { - Assert.That(new Luhn(12121).Addends, Is.EqualTo(new[] { 1, 4, 1, 4, 1 })); - } - - [Ignore("Remove to run test")] - [Test] - public void Addends_subtracts_9_when_doubled_number_is_more_than_9() - { - Assert.That(new Luhn(8631).Addends, Is.EqualTo(new[] { 7, 6, 6, 1 })); - } - - [Ignore("Remove to run test")] - [TestCase(4913, ExpectedResult = 22)] - [TestCase(201773, ExpectedResult = 21)] - public int Checksum_adds_addends_together(int number) - { - return new Luhn(number).Checksum; - } - - [Ignore("Remove to run test")] - [TestCase(738, ExpectedResult = false)] - [TestCase(8739567, ExpectedResult = true)] - public bool Number_is_valid_when_checksum_mod_10_is_zero(int number) - { - return new Luhn(number).Valid; - } - - [Ignore("Remove to run test")] - [Test] - public void Luhn_can_create_simple_numbers_with_valid_check_digit() - { - Assert.That(Luhn.Create(123), Is.EqualTo(1230)); - } - - [Ignore("Remove to run test")] - [Test] - public void Luhn_can_create_larger_numbers_with_valid_check_digit() - { - Assert.That(Luhn.Create(873956), Is.EqualTo(8739567)); - } - - [Ignore("Remove to run test")] - [Test] - public void Luhn_can_create_huge_numbers_with_valid_check_digit() - { - Assert.That(Luhn.Create(837263756), Is.EqualTo(8372637564)); + [TestCase("1", ExpectedResult = false)] // single digit strings can not be valid + [TestCase("0", ExpectedResult = false, Ignore = "Remove to run test")] // a single zero is invalid + [TestCase("046 454 286", ExpectedResult = true, Ignore = "Remove to run test")] // valid Canadian SIN + [TestCase("046 454 287", ExpectedResult = false, Ignore = "Remove to run test")] // invalid Canadian SIN + [TestCase("8273 1232 7352 0569", ExpectedResult = false, Ignore = "Remove to run test")] // invalid credit card + [TestCase("827a 1232 7352 0569", ExpectedResult = false, Ignore = "Remove to run test")] // strings that contain non-digits are not valid + public bool ValidateChecksum(string number) + { + return Luhn.IsValid(number); } } \ No newline at end of file