Ejemplo n.º 1
0
        private static Node <T> AddLoop <T>(Node <T> head, params T[] loop)
            where T : IEquatable <T>
        {
            while (head?.Next != null)
            {
                head = head.Next;
            }

            var loopStart = ListHelpers.CreateLinkedList(loop);
            var loopEnd   = loopStart;

            while (loopEnd.Next != null)
            {
                loopEnd = loopEnd.Next;
            }

            if (head != null)
            {
                head.Next = loopStart;
            }

            loopEnd.Next = loopStart;

            return(loopStart);
        }
Ejemplo n.º 2
0
        public void Question_2_7_BasicCases()
        {
            var l1           = ListHelpers.CreateLinkedList(1, 2);
            var l2           = ListHelpers.CreateLinkedList(3, 4);
            var intersection = ListHelpers.CreateLinkedList(5, 6);

            AddIntersection(l1, l2, intersection);
            Validate(intersection, l1, l2);

            l1           = ListHelpers.CreateLinkedList(1);
            l2           = ListHelpers.CreateLinkedList(2);
            intersection = ListHelpers.CreateLinkedList(3);
            AddIntersection(l1, l2, intersection);
            Validate(intersection, l1, l2);

            l1           = ListHelpers.CreateLinkedList(1, 2, 3, 4);
            l2           = ListHelpers.CreateLinkedList(2);
            intersection = ListHelpers.CreateLinkedList(5);
            AddIntersection(l1, l2, intersection);
            Validate(intersection, l1, l2);

            l1           = ListHelpers.CreateLinkedList(1, 2, 3, 4);
            l2           = ListHelpers.CreateLinkedList(2, 2, 2, 2);
            intersection = ListHelpers.CreateLinkedList(5, 4, 3, 2, 3);
            AddIntersection(l1, l2, intersection);
            Validate(intersection, l1, l2);

            intersection = ListHelpers.CreateLinkedList(4);
            Validate(intersection, intersection, intersection);
        }
Ejemplo n.º 3
0
        public void Question_2_1_BasicCases()
        {
            // No duplicates
            var l1       = ListHelpers.CreateLinkedList(1, 2, 3);
            var expected = ListHelpers.CloneList(l1);

            this.ValidateResult(expected, l1);

            // Several (3x) duplicates
            var l2 = ListHelpers.CreateLinkedList(1, 1, 1);

            expected = ListHelpers.CreateLinkedList(1);
            this.ValidateResult(expected, l2);

            // two sets of duplicates, one next to each other one separated
            var l3 = ListHelpers.CreateLinkedList(1, 2, 2, 1);

            expected = ListHelpers.CreateLinkedList(1, 2);
            this.ValidateResult(expected, l3);

            // 3x duplicate case split up
            var l4 = ListHelpers.CreateLinkedList(1, 2, 2, 3, 2);

            expected = ListHelpers.CreateLinkedList(1, 2, 3);
            this.ValidateResult(expected, l4);

            // Several duplicates in the list
            var l5 = ListHelpers.CreateLinkedList(1, 2, 2, 1, 3, 4, 7, 5, 6, 2, 3, 4, 5, 6);

            expected = ListHelpers.CreateLinkedList(1, 2, 3, 4, 7, 5, 6);
            this.ValidateResult(expected, l5);
        }
Ejemplo n.º 4
0
            public void Question_2_2_EdgeCases()
            {
                // 1 => 1 - Only one element.
                var l1 = ListHelpers.CreateLinkedList(1);

                Assert.AreEqual(1, Question_2_2.ReturnKthToLast(l1, 0));
            }
Ejemplo n.º 5
0
        public void Question_2_6_EdgeCases()
        {
            var l1 = ListHelpers.CreateLinkedList(1);
            var l2 = ListHelpers.CreateLinkedList(2, 2);
            var l3 = ListHelpers.CreateLinkedList(1, 2);

            Validate(true, l1);
            Validate(true, l2);
            Validate(false, l3);
        }
Ejemplo n.º 6
0
        public void Question_2_3_InvalidCases()
        {
            // Remove end of list
            var l3       = ListHelpers.CreateLinkedList(1, 2);
            var expected = ListHelpers.CreateLinkedList(1);

            TestHelpers.AssertExceptionThrown(() => Question_2_3.RemoveMiddleNode(l3.Next), typeof(ArgumentException));

            // Pass null node.
            Node <int> head = null;

            TestHelpers.AssertExceptionThrown(() => Question_2_3.RemoveMiddleNode(head), typeof(ArgumentNullException));
        }
Ejemplo n.º 7
0
        public void Question_2_6_BasicCases()
        {
            var l1 = ListHelpers.CreateLinkedList(1, 2, 1);
            var l2 = ListHelpers.CreateLinkedList(1, 2, 2, 2, 2, 2);
            var l3 = ListHelpers.CreateLinkedList(1, 1, 1, 1, 1);
            var l4 = ListHelpers.CreateLinkedList(1, 2, 3, 4, 5);
            var l5 = ListHelpers.CreateLinkedList(1, 2, 2, 2, 2, 2, 1);

            Validate(true, l1);
            Validate(false, l2);
            Validate(true, l3);
            Validate(false, l4);
            Validate(true, l5);
        }
Ejemplo n.º 8
0
        public void Question_2_4_EdgeCases()
        {
            // One node list, no change
            var l1 = ListHelpers.CreateLinkedList(1);

            ValidateResult(l1, 0, 1);
            ValidateResult(l1, 1, 1);
            ValidateResult(l1, 2, 1);

            // List with all the same values
            var l2 = ListHelpers.CreateLinkedList(2, 2, 2, 2, 2, 2);

            ValidateResult(l2, 2, 2, 2, 2, 2, 2, 2);
        }
Ejemplo n.º 9
0
            public void Question_2_2_BasicCases()
            {
                // Cover all case for K for a list of length 6.
                const int NumberElements = 6;

                var list = ListHelpers.CreateLinkedList(Enumerable.Range(0, NumberElements).Reverse().ToArray());

                for (var i = 0; i < NumberElements; i++)
                {
                    var listCopy = ListHelpers.CloneList(list);
                    var result   = Question_2_2.ReturnKthToLast(listCopy, i);
                    Assert.AreEqual(i, result);
                }
            }
Ejemplo n.º 10
0
        public void Question_2_4_BasicCases()
        {
            // List already partitioned.
            var l1 = ListHelpers.CreateLinkedList(1, 2, 3);

            ValidateResult(l1, 2, 1, 2, 3);

            // List already partitioned.
            var l2 = ListHelpers.CreateLinkedList(1, 2, 3);

            ValidateResult(l2, 1, 1, 2, 3);

            // List needs to be partitioned.
            var l3 = ListHelpers.CreateLinkedList(5, 4, 3, 2, 1);

            ValidateResult(l3, 3, 2, 1, 3, 5, 4);
        }
Ejemplo n.º 11
0
            public void Question_2_2_InvalidCases()
            {
                Node <int> invalidHead = null;
                var        head        = ListHelpers.CreateLinkedList(1);

                TestHelpers.AssertExceptionThrown(
                    () => Question_2_2.ReturnKthToLast(invalidHead, 0),
                    typeof(ArgumentNullException));

                TestHelpers.AssertExceptionThrown(
                    () => Question_2_2.ReturnKthToLast(head, -1),
                    typeof(ArgumentOutOfRangeException));

                TestHelpers.AssertExceptionThrown(
                    () => Question_2_2.ReturnKthToLast(head, 1),
                    typeof(ArgumentOutOfRangeException));
            }
Ejemplo n.º 12
0
        public void Question_2_8_BasicCases()
        {
            var list      = ListHelpers.CreateLinkedList(1, 2, 3);
            var loopStart = AddLoop(list, 4, 5, 6);

            Validate(loopStart, list);

            list      = ListHelpers.CreateLinkedList(1);
            loopStart = AddLoop(list, 4, 5, 6);
            Validate(loopStart, list);

            list      = ListHelpers.CreateLinkedList(1);
            loopStart = AddLoop(list, 4);
            Validate(loopStart, list);

            loopStart = AddLoop(null, 5);
            Validate(loopStart, loopStart);
        }
Ejemplo n.º 13
0
        public void Question_2_1_EdgeCases()
        {
            // 1 => 1 - Only one element.
            var l1       = ListHelpers.CreateLinkedList(1);
            var expected = ListHelpers.CloneList(l1);

            this.ValidateResult(expected, l1);

            // 1, 1 => 1 - Only duplicate elements
            var l2 = ListHelpers.CreateLinkedList(1, 1);

            expected = ListHelpers.CreateLinkedList(1);
            this.ValidateResult(expected, l2);

            // 1, 2, 2 => 1, 2 - Last element duplicated
            var l3 = ListHelpers.CreateLinkedList(1, 2, 2);

            expected = ListHelpers.CreateLinkedList(1, 2);
            this.ValidateResult(expected, l3);
        }
Ejemplo n.º 14
0
        public void Question_2_3_BasicCases()
        {
            // Remove 1 middle node: 1, 2, 3 => 1, 3
            var l1       = ListHelpers.CreateLinkedList(1, 2, 3);
            var expected = ListHelpers.CreateLinkedList(1, 3);

            Question_2_3.RemoveMiddleNode(l1.Next);
            ListHelpers.ValidateLinkedListContent(expected, l1);

            // Remove 2: 1, 2, 3, 4 => 1, 4
            var l2 = ListHelpers.CreateLinkedList(1, 2, 3, 4);

            expected = ListHelpers.CreateLinkedList(1, 4);
            Question_2_3.RemoveMiddleNode(l2.Next);
            Question_2_3.RemoveMiddleNode(l2.Next);
            ListHelpers.ValidateLinkedListContent(expected, l2);

            // Remove 1 node from big list: 1, 2, 3, 4, 5, 6, 7 => 1, 2, 3, 5, 6, 7
            var l3 = ListHelpers.CreateLinkedList(1, 2, 3, 4, 5, 6, 7);

            expected = ListHelpers.CreateLinkedList(1, 2, 3, 5, 6, 7);
            Question_2_3.RemoveMiddleNode(l3.Next.Next.Next);
            ListHelpers.ValidateLinkedListContent(expected, l3);
        }
Ejemplo n.º 15
0
        private static Node <Digit> CreateDigitList(int num, bool reversed = false)
        {
            var list = CreateList(num, reversed);

            return(ListHelpers.CreateLinkedList(list.Select(n => new Digit(n)).ToArray()));
        }