{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# COhPy March 2016 Challenge\n", "This is the coding challenge for the Central Ohio Python Users Group for March 2016.\n", "\n", "Building on last month's challenge, this month's challenge will involve figuring out dates.\n", "\n", "This challenge has three parts. Each part builds on the previous part, and feel free to do\n", "all the parts or only some. This challenge is for you, so implement it any way you want. Find\n", "a new module, or way of implementing that scratches your itches.\n", "\n", "# Part One\n", "\n", "Given a month (say February), return a list of dates in that month that the Central Ohio Python\n", "Users Group met (in the case of February, it would return 2/22/2010, 2/28/2011, 2/27/2012, \n", "2/25/2013, 2/24/2014, and 2/29/2015.\n", "\n", "You can figure out those dates by implementing the following rules:\n", "\n", "1. The first COhPy meeting was September 28, 2009.\n", "\n", "2. COhPy meets the last Monday of the month, with the following exceptions:\n", "\n", "3. COhPy never meets in November.\n", "\n", "4. COhPy meets the first Monday of the month in December EXCEPT when that Monday is the 1st or 2nd of December, when it meets on the SECOND Monday.\n", "\n", "5. In May, to avoid Memorial Day, COhPy meets on the second-to-last Monday of the month.\n", "\n", "# Part Two\n", "\n", "Given a set of dates (particularly the dates returned in Part One), return the high temperature in Columbus, Ohio\n", "at the airport (weather station KCMH) for each of those dates.\n", "\n", "Here are a couple of resources to get historical weather observations:\n", "\n", "APIs:\n", " * https://www.wunderground.com/weather/api/d/docs?d=data/history\n", " * https://developer.forecast.io/docs/v2#time_call\n", " * https://developer.worldweatheronline.com/api/historical-weather-api.aspx\n", "\n", "CSV and other data:\n", " * http://www.ncdc.noaa.gov/cdo-web/search\n", "\n", "# Part Three\n", "\n", "Based on the data in Part Two (or whatever data you want) predict the high temperature for the next COhPy meetup. Your\n", "prediction algorithm will run on meetup day the month prior to the one you are predicting.\n", "\n", "At the April 2016 meeting we will run your prediction algorithms, and at the May meetup, the algorithm that most closely\n", "predicted the high reported by weather station KCMH for the May meetup will receive a Python ball cap! In the event of a tie,\n", "the tied algorithms will face off to predict June (or we'll randomly decide :-)!\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part One\n", "\n", "Given a month (say February), return a list of dates in that month that the Central Ohio Python\n", "Users Group met (in the case of February, it would return 2/22/2010, 2/28/2011, 2/27/2012, \n", "2/25/2013, 2/24/2014, and 2/29/2015.\n", "\n", "You can figure out those dates by implementing the following rules:\n", "\n", "1. The first COhPy meeting was September 28, 2009.\n", "\n", "2. COhPy meets the last Monday of the month, with the following exceptions:\n", "\n", "3. COhPy never meets in November.\n", "\n", "4. COhPy meets the first Monday of the month in December EXCEPT when that Monday is the 1st or 2nd of December, when it meets on the SECOND Monday.\n", "\n", "5. In May, to avoid Memorial Day, COhPy meets on the second-to-last Monday of the month.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "get_past_cohpy_meeting_dates_of_month() in the cells below is my solution for Part One. There is also some code to demonstrate it." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import datetime\n", "import calendar" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "datetime.date(2009, 9, 28)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FIRST_MEETING = datetime.date(2009, 9, 28)\n", "FIRST_MEETING" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2009" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FIRST_MEETING.year" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "MONTHS_PER_YEAR = 12" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_dates_for_weekday(year, month, weekday):\n", " '''Returns list of dates of specified month\n", " that are on specified weekday.\n", " Dates are in ascending order.\n", " '''\n", " c = calendar.Calendar()\n", " return [\n", " day_of_month\n", " for day_of_month, day_of_week in c.itermonthdays2(year, month)\n", " if day_of_month and day_of_week == weekday]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[4, 11, 18, 25]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "assert get_dates_for_weekday(2016, 4, calendar.MONDAY) == [4, 11, 18, 25]\n", "get_dates_for_weekday(2016, 4, calendar.MONDAY)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_cohpy_meeting_date(year, month):\n", " '''Returns date of specified cohpy meeting as datetime.date object\n", " or None if there is no COhPy meeting that month.'''\n", " month_name = calendar.month_name[month]\n", " if month_name == 'November':\n", " return None # COhPy does not meet in November.\n", " mondays = get_dates_for_weekday(year, month, calendar.MONDAY)\n", " if month_name == 'December':\n", " date = mondays[0]\n", " if date <= 2:\n", " date = mondays[1]\n", " return date\n", " elif month_name == 'May':\n", " return mondays[-2]\n", " else:\n", " return mondays[-1]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_cohpy_meeting_dates_of_month(month, min_date, max_date):\n", " '''Returns generator of datetime.date objects for\n", " dates of COhPy meetings in specified month\n", " between min_date and max_date inclusive.'''\n", " for year in range(min_date.year, max_date.year + 1):\n", " date_of_month = get_cohpy_meeting_date(year, month)\n", " if not date_of_month:\n", " continue\n", " date = datetime.date(year, month, date_of_month)\n", " if min_date <= date <= max_date:\n", " yield date" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(datetime.date(2016, 4, 25), datetime.date(2016, 4, 24))" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today = datetime.date.today()\n", "day_before_today = today - datetime.timedelta(days=1)\n", "today, day_before_today" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2010-02-22\n", "2011-02-28\n", "2012-02-27\n", "2013-02-25\n", "2014-02-24\n", "2015-02-23\n", "2016-02-29\n" ] } ], "source": [ "for d in get_cohpy_meeting_dates_of_month(2, FIRST_MEETING, day_before_today):\n", " print(d)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Meetings in January before 2016-04-25:\n", " 2010-01-25\n", " 2011-01-31\n", " 2012-01-30\n", " 2013-01-28\n", " 2014-01-27\n", " 2015-01-26\n", " 2016-01-25\n", "Meetings in February before 2016-04-25:\n", " 2010-02-22\n", " 2011-02-28\n", " 2012-02-27\n", " 2013-02-25\n", " 2014-02-24\n", " 2015-02-23\n", " 2016-02-29\n", "Meetings in March before 2016-04-25:\n", " 2010-03-29\n", " 2011-03-28\n", " 2012-03-26\n", " 2013-03-25\n", " 2014-03-31\n", " 2015-03-30\n", " 2016-03-28\n", "Meetings in April before 2016-04-25:\n", " 2010-04-26\n", " 2011-04-25\n", " 2012-04-30\n", " 2013-04-29\n", " 2014-04-28\n", " 2015-04-27\n", "Meetings in May before 2016-04-25:\n", " 2010-05-24\n", " 2011-05-23\n", " 2012-05-21\n", " 2013-05-20\n", " 2014-05-19\n", " 2015-05-18\n", "Meetings in June before 2016-04-25:\n", " 2010-06-28\n", " 2011-06-27\n", " 2012-06-25\n", " 2013-06-24\n", " 2014-06-30\n", " 2015-06-29\n", "Meetings in July before 2016-04-25:\n", " 2010-07-26\n", " 2011-07-25\n", " 2012-07-30\n", " 2013-07-29\n", " 2014-07-28\n", " 2015-07-27\n", "Meetings in August before 2016-04-25:\n", " 2010-08-30\n", " 2011-08-29\n", " 2012-08-27\n", " 2013-08-26\n", " 2014-08-25\n", " 2015-08-31\n", "Meetings in September before 2016-04-25:\n", " 2009-09-28\n", " 2010-09-27\n", " 2011-09-26\n", " 2012-09-24\n", " 2013-09-30\n", " 2014-09-29\n", " 2015-09-28\n", "Meetings in October before 2016-04-25:\n", " 2009-10-26\n", " 2010-10-25\n", " 2011-10-31\n", " 2012-10-29\n", " 2013-10-28\n", " 2014-10-27\n", " 2015-10-26\n", "No meetings in November before 2016-04-25.\n", "Meetings in December before 2016-04-25:\n", " 2009-12-07\n", " 2010-12-06\n", " 2011-12-05\n", " 2012-12-03\n", " 2013-12-09\n", " 2014-12-08\n", " 2015-12-07\n" ] } ], "source": [ "# Demonstrate that get_past_cohpy_meeting_dates_of_month() works.\n", "for month in range(1, MONTHS_PER_YEAR + 1):\n", " month_name = calendar.month_name[month]\n", " dates = list(get_cohpy_meeting_dates_of_month(month, FIRST_MEETING, day_before_today))\n", " if dates:\n", " print('Meetings in %s before %s:' % (month_name, today))\n", " for date in dates:\n", " print(' %s' % date)\n", " else:\n", " print('No meetings in %s before %s.' % (month_name, today))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }