Пример #1
0
        /*
         * Problem 2 - http://www.pythonchallenge.com/pc/def/ocr.html
         * It contains a long long string of data in the sourcecode and it's asking us to find rare characters.
         * File is named "forP2.txt". So now we just need to extract the rare characters.
         *
         * Originally, what I used to do was step one char, read the letter, find all instances, output the total on screen and pop that character out.
         * Wonder if I can do the same here...
         */

        static void Problem2()
        {
            // Load file
            FileHandling file = new FileHandling();
            string       text = (file.readFile("forP2.txt"));

            // I was NOT aware that dictionaries were a thing in c#
            // https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0#definition
            Dictionary <char, int> openWith = new Dictionary <char, int>();

            // https://www.codeproject.com/Questions/177626/count-number-of-characters-in-a-string
            foreach (char letter in text)
            {
                if (!openWith.ContainsKey(letter))
                {
                    openWith.Add(letter, 1);
                }
                else
                {
                    openWith[letter]++;
                }
            }

            // https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary
            foreach (KeyValuePair <char, int> entry in openWith)
            {
                // do something with entry.Value or entry.Key
                Console.WriteLine(entry.Key + " - " + entry.Value);
            }
        }
Пример #2
0
        // http://www.pythonchallenge.com/pc/def/equality.html

        /*
         * Problem 3, here we go.
         * Long string of text, "One small letter, surrounded by EXACTLY three big bodyguards"
         * So basic regex.
         * File is named "forP3.txt".
         *
         */
        static void Problem3()
        {
            // Load file
            FileHandling file    = new FileHandling();
            string       text    = (file.readFile("forP3.txt"));
            string       search  = "[^A-Z][A-Z]{3}[a-z]{1}[A-Z]{3}[^A-Z]";
            RegexHandler handler = new RegexHandler();

            // https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.matches?redirectedfrom=MSDN&view=net-5.0#overloads
            foreach (Match match in handler.ReturnMatches(search, text))
            {
                Console.Write(match.Value.Substring(4, 1));
            }
        }