protected override void ProcessRecord()
        {
            var      problems = ProblemService.GetProblems(typeof(Problems.DiscoveryProblem).Assembly);
            IProblem problem  = null;

            var pr = new ProblemResult
            {
                Number = Number,
            };

            if (problems.TryGetValue(Number, out problem))
            {
                try
                {
                    var sw     = Stopwatch.StartNew();
                    var answer = problem.Execute();
                    sw.Stop();
                    pr.Answer   = answer;
                    pr.Duration = sw.ElapsedMilliseconds;
                }
                catch (ProblemIncompleteException)
                {
                    pr.Answer = $"Problem {problem.Number} is incomplete";
                }
            }
            else
            {
                pr.Answer = $"Problem not found";
            }

            WriteObject(pr);
            //base.ProcessRecord();
        }
Exemplo n.º 2
0
        private static void TimedExecute(IProblem problem)
        {
            var timer = Stopwatch.StartNew();
            var result = problem.Execute();
            timer.Stop();

            Debug.WriteLine(string.Format("Executed in {0}ms", timer.ElapsedMilliseconds));
            Debug.WriteLine(string.Format("Result: {0}", result));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            ShowHeader();

            while (true)
            {
                LogInfoMessage("Type q/Q to quit the selection menu.");

                Console.Write("Welcome, please write a number between 1-4 to select a problem: ");
                string response = Console.ReadLine().Trim().ToLower();

                if (response.Equals("q"))
                {
                    break;
                }

                if (int.TryParse(response, out int numberSelected))
                {
                    IProblem problem = ProblemFactory.CreateFrom(numberSelected);
                    if (problem is null)
                    {
                        LogErrorMessage("No error defined for this number!");
                        continue;
                    }

                    bool continueExecution;
                    do
                    {
                        Console.Clear();
                        problem.Execute();
                        SubDivision();
                        continueExecution = AskToRepeatProblem();

                        if (!continueExecution)
                        {
                            ShowHeader();
                        }
                    } while (continueExecution);

                    continue;
                }

                LogErrorMessage("Please write a valid number!");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// UnitTest:
        ///
        /// Execute problem 'count' times.
        /// Compare problem result with answer from textfile.
        /// </summary>
        /// <returns>
        /// result, passed, ElapsedMilliseconds
        /// </returns>
        public static void Pass(IProblem problem, int count)
        {
            if (answers == null)
            {
                getAnswers("text/Answers.txt");
            }
            int       nr       = problem.ToString().nr();
            var       expected = answers[nr];
            Stopwatch s        = Stopwatch.StartNew();

            while (count-- > 0)
            {
                result = problem.Execute();
            }
            ElapsedMilliseconds = s.ElapsedMilliseconds;
            TotalTime          += ElapsedMilliseconds;
            passed = result == expected;
        }
Exemplo n.º 5
0
 private static void RunProblem(IProblem problem)
 {
     try
     {
         var    sw         = Stopwatch.StartNew();
         string answer     = string.Empty;
         var    executions = 1;
         for (var i = 1; i <= executions; i++)
         {
             answer = problem.Execute();
         }
         sw.Stop();
         Debug.Print($"Problem {problem.Number} Answer = {answer} took {sw.ElapsedMilliseconds}ms to run");
         Debug.Print($"Average {sw.ElapsedMilliseconds / (double)executions}");
     }
     catch (ProblemIncompleteException)
     {
         Debug.Print($"Problem {problem.Number} is incomplete");
     }
 }