Пример #1
0
        public void Rot13()
        {
            const string input  = @"How can you tell an extrovert from an introvert at NSA? Va gur ryringbef, gur rkgebireg ybbxf ng gur BGURE thl'f fubrf.";
            const string output = @"Ubj pna lbh gryy na rkgebireg sebz na vagebireg ng AFN? In the elevators, the extrovert looks at the OTHER guy's shoes.";

            string rot(int degree, string src)
            {
                var alphabet        = Enumerate.From('a').To('z');
                var shiftedAlphabet = alphabet.Cycle().Skip(degree).Take(alphabet.Count());

                IEnumerable <char> repeatWithUpperCase(IEnumerable <char> x)
                => x.Concat(x.Select(char.ToUpper));

                var transformation = repeatWithUpperCase(alphabet)
                                     .Zip(repeatWithUpperCase(shiftedAlphabet))
                                     .ToDictionary();

                var result = src.Select(c => transformation.ContainsKey(c) ? transformation[c] : c).BuildString();

                return(result);
            }

            var rot13 = ((Func <int, string, string>)rot).Curry()(13);

            Assert.Equal(output, rot13(input));
            Assert.Equal(input, rot13(output));
        }
Пример #2
0
        public void Problem34_DigitFactorials()
        {
            long factorial(int n)
            {
                return(1.Yield()
                       .Concat(Natural.Numbers.Scan((x, y) => x * y))
                       .ElementAt(n));
            }

            IEnumerable <int> digits(int n)
            {
                return(n
                       .Iterate(x => x / 10)
                       .TakeUntil(0)
                       .Select(x => x % 10));
            }

            const int lowerBound      = 10; // cannot be one digit
            var       upperDigitCount = Enumerate.From(2).First(x => x * factorial(9) < Math.Pow(10, x));
            var       upperBound      = (int)(upperDigitCount * factorial(9));

            long numbersSum = Enumerate.From(lowerBound).To(upperBound).AsParallel()
                              .Where(x => digits(x).Sum((Func <int, long>)factorial) == x) // sum+equals could be optimized
                              .Sum();

            Assert.Equal(40730, numbersSum);
        }
Пример #3
0
        public void EnumerateChar_FromThenTo()
        {
            // act
            var numbers = Enumerate.From('a').Then('b').To('f');

            // assert
            Assert.Equal(new[] { 'a', 'b', 'c', 'd', 'e', 'f' }, numbers);
        }
Пример #4
0
        public void EnumerateFloat_From()
        {
            // act
            var numbers = Enumerate.From((float)1).Take(5);

            // assert
            Assert.Equal(new float[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #5
0
        public void EnumerateByte_FromTo_Empty()
        {
            // act
            var numbers = Enumerate.From((byte)5).To(1);

            // assert
            Assert.Empty(numbers);
        }
Пример #6
0
        public void EnumerateDateTime_From()
        {
            // act
            var numbers = Enumerate.From(new DateTime(1990, 7, 5)).Take(5);

            // assert
            Assert.Equal(new[] { new DateTime(1990, 7, 5), new DateTime(1990, 7, 6), new DateTime(1990, 7, 7), new DateTime(1990, 7, 8), new DateTime(1990, 7, 9) }, numbers);
        }
Пример #7
0
        public void EnumerateDecimal_From()
        {
            // act
            var numbers = Enumerate.From((decimal)1).Take(5);

            // assert
            Assert.Equal(new decimal[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #8
0
        public void EnumerateLong_From()
        {
            // act
            var numbers = Enumerate.From(1L).Take(5);

            // assert
            Assert.Equal(new long[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #9
0
        public void EnumerateByte_From_Overflow()
        {
            // act
            var overflow = Enumerate.From((byte)1).ElementAt(255);

            // assert
            Assert.Equal(0, overflow);
        }
Пример #10
0
        public void EnumerateDecimal_FromThenTo()
        {
            // act
            var numbers = Enumerate.From((decimal)1).Then(1.1M).To(2M);

            // assert
            Assert.Equal(new[] { 1, 1.1M, 1.2M, 1.3M, 1.4M, 1.5M, 1.6M, 1.7M, 1.8M, 1.9M, 2M }, numbers);
        }
Пример #11
0
        public void EnumerateDouble_FromThenTo()
        {
            // act
            var numbers = Enumerate.From(1d).Then(0.5).To(-1);

            // assert
            Assert.Equal(new[] { 1, 0.5, 0, -0.5, -1 }, numbers, new DoublePrecisionComparer(2));
        }
Пример #12
0
        public void EnumerateDouble_FromThen()
        {
            // act
            var numbers = Enumerate.From(1d).Then(0.9).Take(5);

            // assert
            Assert.Equal(new[] { 1, 0.9d, 0.8d, 0.7d, 0.6d }, numbers, new DoublePrecisionComparer(2));
        }
Пример #13
0
        public void EnumerateBigInteger_FromThenTo()
        {
            // act
            var numbers = Enumerate.From(new BigInteger(1)).Then(3).To(9);

            // assert
            Assert.Equal(new BigInteger[] { 1, 3, 5, 7, 9 }, numbers);
        }
Пример #14
0
        public void EnumerateDecimal_FromThen()
        {
            // act
            var numbers = Enumerate.From((decimal)1).Then(1.1M).Take(5);

            // assert
            Assert.Equal(new[] { 1, 1.1M, 1.2M, 1.3M, 1.4M }, numbers);
        }
Пример #15
0
        public void EnumerateChar_FromThen()
        {
            // act
            var numbers = Enumerate.From('a').Then('c').Take(5);

            // assert
            Assert.Equal(new[] { 'a', 'c', 'e', 'g', 'i' }, numbers);
        }
Пример #16
0
        public void EnumerateChar_FromTo_Empty()
        {
            // act
            var numbers = Enumerate.From('e').To('a');

            // assert
            Assert.Empty(numbers);
        }
Пример #17
0
        public void EnumerateBigInteger_From()
        {
            // act
            var numbers = Enumerate.From(new BigInteger(1)).Take(5);

            // assert
            Assert.Equal(new BigInteger[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #18
0
        public void EnumerateFloat_FromThenTo()
        {
            // act
            var numbers = Enumerate.From(1f).Then(10).To(40);

            // assert
            Assert.Equal(new float[] { 1, 10, 19, 28, 37 }, numbers, new FloatPrecisionComparer(2));
        }
Пример #19
0
        public void EnumerateInt_From()
        {
            // act
            var numbers = Enumerate.From(1).Take(5);

            // assert
            Assert.Equal(new[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #20
0
        public void EnumerateInt_FromThenTo()
        {
            // act
            var numbers = Enumerate.From(1).Then(-1).To(-10);

            // assert
            Assert.Equal(new[] { 1, -1, -3, -5, -7, -9 }, numbers);
        }
Пример #21
0
        public void EnumerateByte_From()
        {
            // act
            var numbers = Enumerate.From((byte)1).Take(5);

            // assert
            Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #22
0
        public void EnumerateInt_FromThenTo_Count3()
        {
            // act
            var numbers = Enumerate.From(1).Then(-1).To(-3);

            // assert
            Assert.Equal(new[] { 1, -1, -3 }, numbers);
        }
Пример #23
0
        public void EnumerateChar_From()
        {
            // act
            var numbers = Enumerate.From('a').Take(5);

            // assert
            Assert.Equal(new[] { 'a', 'b', 'c', 'd', 'e' }, numbers);
        }
Пример #24
0
        public void EnumerateLong_FromThenTo()
        {
            // act
            var numbers = Enumerate.From(1L).Then(1).To(1);

            // assert
            Assert.Equal(new long[] { 1 }, numbers);
        }
Пример #25
0
        public void EnumerateDateTimeOffset_From()
        {
            // act
            var numbers = Enumerate.From(new DateTimeOffset(1990, 7, 5, 0, 0, 0, 0, TimeSpan.FromHours(2))).Take(5);

            // assert
            Assert.Equal(new[] { new DateTimeOffset(1990, 7, 5, 0, 0, 0, 0, TimeSpan.FromHours(2)), new DateTimeOffset(1990, 7, 6, 0, 0, 0, 0, TimeSpan.FromHours(2)), new DateTimeOffset(1990, 7, 7, 0, 0, 0, 0, TimeSpan.FromHours(2)), new DateTimeOffset(1990, 7, 8, 0, 0, 0, 0, TimeSpan.FromHours(2)), new DateTimeOffset(1990, 7, 9, 0, 0, 0, 0, TimeSpan.FromHours(2)) }, numbers);
        }
Пример #26
0
        public void EnumerateInt_FromTo_Empty()
        {
            // act
            var numbers = Enumerate.From(5).To(1);

            // assert
            Assert.Empty(numbers);
        }
Пример #27
0
        public void EnumerateDouble_From()
        {
            // act
            var numbers = Enumerate.From((double)1).Take(5);

            // assert
            Assert.Equal(new double[] { 1, 2, 3, 4, 5 }, numbers);
        }
Пример #28
0
        public void EnumerateByte_FromThenTo_Empty()
        {
            // act
            var numbers = Enumerate.From((byte)253).Then(254).To(3);

            // assert
            Assert.Empty(numbers);
        }
Пример #29
0
        public void EnumerateByte_FromThenTo()
        {
            // act
            var numbers = Enumerate.From((byte)1).Then(3).To(9);

            // assert
            Assert.Equal(new byte[] { 1, 3, 5, 7, 9 }, numbers);
        }
Пример #30
0
        public void EnumerateLong_FromTo_Empty()
        {
            // act
            var numbers = Enumerate.From((long)5).To(1L);

            // assert
            Assert.Empty(numbers);
        }