Exemplo n.º 1
0
        public RangeRule(int from, int to, int numberOfDigits)
        {
            if (numberOfDigits <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfDigits));
            }

            if (to < from)
            {
                throw new ArgumentOutOfRangeException(nameof(to), "to must be > than from");
            }

            if (to > Math.Pow(to, numberOfDigits))
            {
                throw new ArgumentOutOfRangeException(nameof(to));
            }

            if (from > Math.Pow(from, numberOfDigits))
            {
                throw new ArgumentOutOfRangeException(nameof(from));
            }

            NumberOfDigits = numberOfDigits;
            _digitsFrom    = new int[numberOfDigits];
            _digitsTo      = new int[numberOfDigits];

            for (int i = 1; i <= numberOfDigits; i++)
            {
                _digitsFrom[i - 1] = DigitHelper.NthDigit(from, i);
                _digitsTo[i - 1]   = DigitHelper.NthDigit(to, i);
            }
        }
Exemplo n.º 2
0
        private bool IsMatch(int number)
        {
            //Start with largest power
            for (var i = NumberOfDigits; i >= 1; i--)
            {
                var nthDigitOfNumber = DigitHelper.NthDigit(number, i);

                if (nthDigitOfNumber < _digitsFrom[i - 1])
                {
                    return(false);
                }
                if (nthDigitOfNumber > _digitsTo[i - 1])
                {
                    return(false);
                }
                if (nthDigitOfNumber < _digitsTo[i - 1])
                {
                    return(true);
                }
            }

            return(true);
        }