Beispiel #1
0
        public void Should_ReturnReversedLinkedList_When_LinkedListIsPassed()
        {
            // [arrange]
            var underTest = new ReversedLinkedList();
            var input     = new LinkedListNode()
            {
                Data = 0,
                Next = new LinkedListNode()
                {
                    Data = 0,
                    Next = new LinkedListNode()
                    {
                        Data = 1,
                        Next = new LinkedListNode()
                        {
                            Data = 2
                        }
                    }
                }
            };

            // [act]
            var result = underTest.ReverseLinkedList(input);

            // [assert]
            Assert.Equal(input.Next.Next.Next.Data, result.Data);
            Assert.Equal(input.Next.Next.Data, result.Next.Data);
            Assert.Equal(input.Next.Data, result.Next.Next.Data);
            Assert.Equal(input.Data, result.Next.Next.Next.Data);
        }
Beispiel #2
0
            public static IList <IEnumerable <T> > GetShortestSequences <T>(T start, T end, IList <Func <T, T> > functions)
                where T : IComparable <T>
            {
                var results      = new List <IEnumerable <T> >();
                var visited      = new HashSet <T>();
                var currentNodes = new Queue <Node <T> >();

                visited.Add(start);
                currentNodes.Enqueue(new Node <T>(start));

                while (currentNodes.Count > 0)
                {
                    var nextNodes = new Queue <Node <T> >();

                    var nextVisited = new HashSet <T>();

                    while (currentNodes.Count > 0)
                    {
                        var currentNode = currentNodes.Dequeue();

                        foreach (var function in functions)
                        {
                            T nextElement = function(currentNode.Value);

                            if (nextElement.CompareTo(end) > 0)
                            {
                                continue;
                            }

                            if (visited.Contains(nextElement))
                            {
                                continue;
                            }

                            var nextNode = new Node <T>(nextElement);
                            nextNode.Previous = currentNode;

                            nextVisited.Add(nextElement);
                            nextNodes.Enqueue(nextNode);

                            if (nextElement.Equals(end))
                            {
                                var sequence = new ReversedLinkedList <T>(nextNode);
                                results.Add(sequence.Reverse());
                            }
                        }
                    }
                    visited.UnionWith(nextVisited);

                    if (results.Count != 0)
                    {
                        nextNodes.Clear();
                    }
                    currentNodes = nextNodes;
                }
                return(results);
            }
Beispiel #3
0
        static void Main(string[] args)
        {
            NumberOfIslands number = new NumberOfIslands();

            Console.WriteLine(number.IslandCount());

            SpiralPrint sp = new SpiralPrint();

            sp.Print();

            PascalTriangle pt = new PascalTriangle();

            pt.Generate(6); pt.Print();

            MedianOfSortedArrays ms = new MedianOfSortedArrays();

            Console.WriteLine(ms.MedianOf2SortedArrays(ms.array1, ms.array2));

            FindtheMissingNumber fn = new FindtheMissingNumber();

            Console.WriteLine(fn.FindtheMissingNumberMethod1());

            ContainsDuplicate cd = new ContainsDuplicate();

            Console.WriteLine(cd.isDuplicateMethod2());

            ReversedLinkedList rl = new ReversedLinkedList();

            rl.reversedList();

            ReverseNumber rn = new ReverseNumber();

            rn.ReverseANumber(12345);

            LinearSearch ls = new LinearSearch();
            BinarySearch bs = new BinarySearch();

            ZigZag zz = new ZigZag();

            Console.WriteLine(zz.Convert("PAYPALISHIRING", 3));

            BinaryTree bt = new BinaryTree();

            bt.InorderTraverse(bt.root);
            Console.WriteLine();
            bt.PreorderTraverse(bt.root);
            Console.WriteLine();
            bt.PostorderTraverse(bt.root);

            RotateArray ra = new RotateArray(3);
            //SearchRotatedArray sr = new SearchRotatedArray(3);
            //Console.WriteLine(sr.Find());

            FindSumSarray  fS    = new FindSumSarray();
            MaxSumSubArray mxSum = new MaxSumSubArray();
        }
Beispiel #4
0
        public void Should_ReturnListThatWasPassed_When_EmptyLinkedListIsPassed()
        {
            // [arrange]
            var underTest = new ReversedLinkedList();
            var input     = new LinkedListNode();

            // [act]
            var result = underTest.ReverseLinkedList(input);

            // [assert]
            Assert.Same(input, result);
        }
Beispiel #5
0
    static IList <IEnumerable <T> > GetShortestSequences <T>(T start, T end, IList <Func <T, T> > operations)
        where T : IComparable <T>
    {
        var results = new List <IEnumerable <T> >();

        var visited     = new HashSet <T>();
        var currentWave = new Queue <Node <T> >();

        visited.Add(start);
        currentWave.Enqueue(new Node <T>(start));

        int level = 1;

        while (currentWave.Count != 0)
        {
            var nextWave = new Queue <Node <T> >();

            var nextVisited = new HashSet <T>();

            level++;

            while (currentWave.Count != 0)
            {
                var currentNode = currentWave.Dequeue();

                foreach (var operation in operations)
                {
                    T nextElement = operation(currentNode.Value);

                    if (nextElement.CompareTo(end) > 0)
                    {
                        continue;
                    }

                    if (visited.Contains(nextElement))
                    {
                        continue;
                    }

                    var nextNode = new Node <T>(nextElement);
                    nextNode.Previous = currentNode;

                    nextVisited.Add(nextElement);
                    nextWave.Enqueue(nextNode);

                    if (nextElement.Equals(end))
                    {
                        var sequence = new ReversedLinkedList <T>(nextNode);
                        results.Add(sequence.Reverse());
                    }
                }
            }

            visited.UnionWith(nextVisited);

            if (results.Count != 0)
            {
                nextWave.Clear();
            }

            currentWave = nextWave;
        }

        //Console.WriteLine("Sequences length: {0}.", level);

        return(results);
    }