tracks/csharp/exercises/luhn/Example.cs in trackler-2.0.6.8 vs tracks/csharp/exercises/luhn/Example.cs in trackler-2.0.6.9
- old
+ new
@@ -1,58 +1,42 @@
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++)
{
if (i % 2 != 0)
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)
{
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