Problem of the Day
A new programming or logic puzzle every Mon-Fri

New Problem

Happy Monday!

Today's problem is directly related to today's problem. What I mean by that is that your objective is to create a program that pings this website and alerts you when a new problem is posted. Alternatively if you subscribe to the daily/weekly e-mail you can can poll your e-mail account looking for new problems.

Really interested in seeing what everyone comes up with for this one.

Permalink: http://problemotd.com/problem/new-problem/

Comments:

  • Anonymous - 9 years, 11 months ago

    C# quick and dirty console app

    1 - http request to get the html for the Past Problems page

    2 - parse out the list items containing the problem names

    3 - check these names against a list of previous problems, if a new problem is found output it to the console and add it to the list.

    4 - Sleep for 5 min and repeat.

    Next step would be to store the previous problems in a file and also grab date added information.

    Code :

    public class Program
    {
        public const string PAST_PROBLEMS_URL = "http://www.problemotd.com/past/";
    
        static void Main(string[] args)
        {
            var pastProblems = new List<string>();
    
            while (true)
            {
                var problems = GetProblemList();
    
                foreach (var problem in problems)
                {
                    if (!pastProblems.Contains(problem))
                    {
                        Console.WriteLine("New Problem : {0}", problem);
                        pastProblems.Add(problem);
                    }
                }
    
                Thread.Sleep(300000);
            }
        }
    
        private static List<string> GetProblemList()
        {
            string pastProblemsHtml = "";
            var webRequest = HttpWebRequest.Create(PAST_PROBLEMS_URL);
            webRequest.Method = "GET";
    
            var webResponse = (HttpWebResponse)webRequest.GetResponse();
            using (var responseStream = new StreamReader(webResponse.GetResponseStream()))
            {
                pastProblemsHtml = responseStream.ReadToEnd();
                responseStream.Close();
            }
    
            return ParsePastProblemsHtml(pastProblemsHtml);
        }
    
        private static List<string> ParsePastProblemsHtml(string ppHtml)
        {
            var listItemList = new List<string>();
    
            for(int i = 0; i < ppHtml.Length - 4; i++)
            {
                if (ppHtml.Substring(i, 4).Equals("<li>"))
                {
                    bool isMatch = false;
                    for (int j = 1; j < ppHtml.Length - i && !isMatch; j++)
                    {
                        if (Regex.IsMatch(ppHtml.Substring(i, j), @"^<li>.*</li>$"))
                        {
                            isMatch = true;
                            listItemList.Add(ppHtml.Substring(i, j));
                            i = i + j;
                        }
                    }
                }
            }
    
            listItemList = listItemList.Where(s => !s.Contains("a href=\"/suggest")).ToList();
    
            var problemList = new List<string>();
            foreach (var problemListItem in listItemList)
            {
                for (int i = 0; i < problemListItem.Length - 2; i++)
                {
                    if (problemListItem.Substring(i, 2).Equals("\">"))
                    {
                        bool isMatch = false;
                        for (int j = 1; j < problemListItem.Length - i && !isMatch; j++)
                        {
                            if (Regex.IsMatch(problemListItem.Substring(i + 1, j), @"^>.*</a>$"))
                            {
                                isMatch = true;
                                problemList.Add(problemListItem.Substring(i + 2, j-5));
                                i = i + j;
                            }
                        }
                    }
                }
            }
    
            return problemList;
        }
    }
    

    reply permalink

Content curated by @MaxBurstein