public override void Run()
        {
            ArrayService service = new ArrayService();

            decimal[] sequence = service.InsertNonEmptySequence();

            bool success = false;

            while (!success)
            {
                Console.WriteLine($"Please enter starting index in the range[0,{sequence.Length - 1}]:");
                int startIndex = int.Parse(Console.ReadLine());

                Console.WriteLine(
                    $"Please enter last index up to {sequence.Length - 1} and bigger or equal to than {startIndex}:");
                int endIndex = int.Parse(Console.ReadLine());

                int     indexOfMaxNumber = service.FindMaxInRangeIndex(sequence, startIndex, endIndex);
                decimal result           = sequence[indexOfMaxNumber];

                Console.WriteLine("Value of max member in selected subsequence is:");
                Console.WriteLine(result.ToString("f2"));

                success = true;
            }

            decimal[] sortedArray = service.SortDescending(sequence);

            Console.WriteLine("Descending sorted array:");
            Console.WriteLine(string.Join(", ", sortedArray));
        }
        public override void Run()
        {
            int choice = ChooseAnOption();

            switch (choice)
            {
            case 1:
            {
                ReverseNumService reverseManager = new ReverseNumService();
                reverseManager.Run();
                break;
            }

            case 2:
            {
                ArrayService averageManager = new ArrayService();
                averageManager.Run();
                break;
            }

            case 3:
            {
                LinealEquationService equationManager = new LinealEquationService();
                equationManager.Run();
                break;
            }
            }
        }
        public static void JumpGame()
        {
            var n    = 6;
            var jump = new int[] { 1, 2, 0, 3, 0, 0 };

            Console.WriteLine("jump: [{0}]", string.Join(", ", jump));
            Console.WriteLine("Can Jump: {0}", ArrayService.CanReach(jump, n));
        }
        public void SmallestNotExisting_NotInArray_WithOn_Returns1()
        {
            int[]        arr = new int[] { -1, -3 };
            ArrayService ars = new ArrayService();
            int          sm  = ars.SmallestNotIn(arr, true);

            Assert.Equal(1, sm);
        }
        public void SmallestNotExisting_NotInArray_WithOn_Returns5()
        {
            int[]        arr = new int[] { 1, 3, 6, 4, 1, 2 };
            ArrayService ars = new ArrayService();
            int          sm  = ars.SmallestNotIn(arr, true);

            Assert.Equal(5, sm);
        }
        public void SmallestNotExisting_NotInArray()
        {
            int[]        arr = new int[] { 1, 2, 3 };
            ArrayService ars = new ArrayService();
            int          sm  = ars.SmallestNotIn(arr);

            Assert.Equal(4, sm);
        }
Example #7
0
        static void Main()
        {
            var arrayService  = new ArrayService();
            var fileService   = new FileService();
            var cacheService  = new CacheService();
            var outputService = new OutputService(cacheService);

            try
            {
                var listOfArrays = fileService.ReadArray();

                var path = new List <int>();

                foreach (var array in listOfArrays)
                {
                    var pathFromCache = cacheService.GetCacheByArray(array);
                    if (pathFromCache == null)
                    {
                        path = arrayService.FindPath(array);

                        if (!arrayService.Failure) // don't save to cache if it's unreachable
                        {
                            cacheService.AddCacheToRepository(array, path);
                        }
                    }
                    else
                    {
                        path = pathFromCache;
                    }

                    Console.Write(outputService.PrepareOutput(array, path, arrayService.Failure));
                    arrayService.Failure = false;
                }

                Console.WriteLine("Would you like to see all of the cached arrays? Y/N");

                ConsoleKey response;
                do
                {
                    //Console.Write("Are you sure you want to choose this as your login key? [y/n] ");
                    response = Console.ReadKey(false).Key;   // true is intercept key (dont show), false is show
                    if (response != ConsoleKey.Enter)
                    {
                        Console.WriteLine(Environment.NewLine);
                    }
                } while (response != ConsoleKey.Y && response != ConsoleKey.N);

                if (response == ConsoleKey.Y)
                {
                    Console.WriteLine(outputService.PrepareCacheOutput());
                }
            }

            catch
            {
                Console.WriteLine("Error");
            }
        }
Example #8
0
        private void GetAverageColor()
        {
            ScreenService.GetScreenResolution(out var screenWidth, out var screenHeight);

            var size = new Size(screenWidth, screenHeight);

            var verticalDepth   = byte.Parse(ReadTextBox(VerticalDepthSampling));
            var horizontalDepth = byte.Parse(ReadTextBox(HorizontalDepthSampling));
            var screenBitmap    = ScreenService.CreateBitmap(screenWidth, screenHeight);
            var readings        = ArrayService.CreateTaskByteArray(4);
            var timer           = new Stopwatch();

            do
            {
                WaitMilliseconds(100);
                timer.Start();
                screenBitmap = ScreenService.CopyFromTheScreen(screenBitmap, size);
                var format      = screenBitmap.PixelFormat;
                var bppModifier =
                    format == PixelFormat.Format24bppRgb
                        ? 3
                        : 4; // cutting corners, will fail on anything else but 32 and 24 bit images
                var sourceData = screenBitmap.LockBits(new Rectangle(0, 0, screenWidth, screenHeight),
                                                       ImageLockMode.ReadOnly,
                                                       format);
                var stride        = sourceData.Stride;
                var scan          = sourceData.Scan0;
                var readingsCount = 0;

                #region VerticalRight
                readings[readingsCount++] = SendSideRequestAsync(screenHeight, screenWidth, verticalDepth,
                                                                 _screenLedCount.VerticalLedCountRight, 0, scan, bppModifier, stride, false, false, false);
                #endregion

                #region HorizonalTop
                readings[readingsCount++] = SendSideRequestAsync(screenHeight, screenWidth, horizontalDepth,
                                                                 _screenLedCount.HorizontalLedCountTop, 0, scan, bppModifier, stride, false, true, true);
                #endregion

                #region VerticalLeft
                readings[readingsCount++] = SendSideRequestAsync(screenHeight, screenWidth, verticalDepth,
                                                                 _screenLedCount.VerticalLedCountLeft, 0, scan, bppModifier, stride, true, true, false);
                #endregion

                #region HorizonalBottom
                readings[readingsCount] = SendSideRequestAsync(screenHeight, screenWidth, horizontalDepth,
                                                               _screenLedCount.HorizontalLedCountBottom, 0, scan, bppModifier, stride, true, false, true);
                #endregion
                var resultsArray = ArrayService.AwaitTaskByteArray(readings);
                screenBitmap.UnlockBits(sourceData);
                var endArray    = ArrayService.ConvertToSingleArray(resultsArray);
                var elapsedTime = timer.ElapsedMilliseconds.ToString();
                timer.Reset();
                UpdateTextBox(OutputBox1, elapsedTime);
                SendDataToSerialPort(endArray, endArray.Length);
            } while (_isLEDReadingRunning);
            screenBitmap.Dispose();
        }
        public void MaxSumShouldReturn_CorrectValue()
        {
            int[] arr    = new int[] { 1, 2, 3, 4, 5 };
            int   maxNum = 3;

            ArrayService ars    = new ArrayService();
            int          maxSum = ars.MaxSubarraySum(arr, maxNum);

            Assert.Equal(12, maxSum);
        }
        /// <summary>
        /// TODO
        /// </summary>
        public static void SmallestMissingInt()
        {
            int[] array1 = new int[] { 0, -1, 0, 2, 4, 5, 3, 5, 6, 7, 8, 8, 9, 1, 5, 6, -12, 4 };

            Console.WriteLine(@"
Given an unsorted integer array, find the smallest missing positive integer...
Your algorithm should run in O(n) time and uses constant extra space.");

            var solution = ArrayService.FindSmallestMissingNumber(array1);

            Console.WriteLine("array: [{0}]", string.Join(", ", array1));
            Console.WriteLine("answer: {0}", solution);
        }
        /// <summary>
        /// COMPLETE
        /// </summary>
        public static void TargetNum()
        {
            int target = 99;

            int[] array1 = new int[] { 0, 3, 6, 1, 5, 2, 7 };

            Console.WriteLine(@"
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.");

            var solution = ArrayService.FindTwoSum(array1, target);

            Console.WriteLine("target: {0}\narray: [{1}]", target, string.Join(", ", array1));
            Console.WriteLine("answer: [ {0}, {1} ]", solution?[0], solution?[1]);
        }
Example #12
0
        private Task <byte[]> SendSideRequestAsync(int screenHeight, int screenWidth, byte depth, byte sideLedCount, int currentLedCount, IntPtr screenPointer, int bppModifier, int stride, bool isIncremental, bool startFromZero, bool isHorizontal)
        {
            var requestModel = new SideLedReadingRequest
            {
                Y               = screenHeight,
                X               = screenWidth,
                Depth           = depth,
                SideLedCount    = sideLedCount,
                CurrentLedCount = currentLedCount,
                ScreenPointer   = screenPointer,
                BPPModifier     = bppModifier,
                Stride          = stride,
                ColourArray     = ArrayService.CreateByteArray(_screenLedCount),
                IsIncremental   = isIncremental,
                StartFromZero   = startFromZero,
                IsHorizontal    = isHorizontal
            };

            return(ScreenService.GetSideLEDAsync(requestModel));
        }
        /// <summary>
        /// TODO
        /// </summary>
        public static void FindWord()
        {
            var word = "CAEDC";

            char[][] board = new char[][]
            {
                new char[] { 'C', 'A', 'A', 'B', 'D' },
                new char[] { 'A', 'E', 'D', 'C', 'A' }
            };

            Console.WriteLine(@"
Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where 'adjacent' cells are horizontally or vertically neighboring.
The same letter cell may not be used more than once.
");

            Console.WriteLine("Find Word: {0}\n", word);
            Console.WriteLine("Board:\n\n[{0}]\n[{1}]\n", string.Join(", ", board[0]), string.Join(", ", board[1]));
            Console.WriteLine("It Exists: {0}", ArrayService.FindWordInBoard(board, word));
        }
 public MyDynamicArrayTests()
 {
     _arrayService = new ArrayService();
 }
Example #15
0
 public ArrayServiceTest()
 {
     _arrayService = new ArrayService();
 }
Example #16
0
 public void Setup()
 {
     _arrayService = new ArrayService();
 }