Example #1
0
        public IBook CrackingTheCodingInterview(string filename)
        {
            // 1.1,3.2,14.1
            var biggestChapterNumberSoFar = 0;
            var solvedQuestions           = new HashSet <QuestionNumber>();

            using (TextReader reader = File.OpenText(filename))
            {
                var questions = reader.ReadLine().Split(',');

                foreach (var q in questions)
                {
                    string[] question = q.Split('.');

                    var qn = new QuestionNumber(int.Parse(question[0]), int.Parse(question[1]));
                    qn.Solved = true;

                    if (!solvedQuestions.Contains(qn))
                    {
                        solvedQuestions.Add(qn);
                        biggestChapterNumberSoFar = Math.Max(biggestChapterNumberSoFar, qn.Chapter);
                    }
                }
            }

            if (biggestChapterNumberSoFar < 1)
            {
                throw new InvalidOperationException("no questions were saved in file.");
            }

            IBook book = new BookFactory().CrackingTheCodingInterview();

            foreach (var solvedQuestion in solvedQuestions)
            {
                book.SolveQuestion(solvedQuestion);
            }

            return(book);
        }
        public static void Main(string[] args)
        {
            Console.WriteLine($"A sample question number is {new QuestionNumber(2,11)}");
            IBookFactory bookFactory = new BookFactory();
            var          book        = bookFactory.CrackingTheCodingInterview();

            Console.WriteLine($"created book {book.Name}");
            Console.WriteLine($"book {book.Name} has {book.NumChapters} chapters and {book.NumQuestions} questions in total.");

            var savedBook = bookFactory.CrackingTheCodingInterview("question-numbers.txt");

            Console.WriteLine($"saved book {savedBook.Name} has {savedBook.NumSolved} questions solved.");
            var questionsToChooseFrom = new Trainer(savedBook).MakeExamExcludingLastChapters(7);

            foreach (var question in questionsToChooseFrom)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();

                Console.WriteLine($"{DateTime.Now}: Now solve {question}.");
                var k = Console.ReadKey();

                stopWatch.Stop();
                var    ts          = stopWatch.Elapsed;
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds / 10);
                Console.WriteLine("Runtime: " + elapsedTime);

                Console.WriteLine($" Note: update the txt file if you finished {question}.");

                if (k.KeyChar.Equals('q'))
                {
                    break;
                }
            }
        }