Results 1 to 10 of 20

Thread: Beginner's Programming Challenge 17

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Ubuntu Development Release

    Beginner's Programming Challenge 17

    Well, I won the last programming challenge, so here is the next one. This involves parsing input with regards to tasks.

    The challenge:
    Build a program that asks for input in the format:
    [Task Title] due [Date]
    Where [Task Title] is the title of the task, and [Date] is a string representing a date. This date string could be:
    1. An Absolute Date ("5 Nov 2010")
    2. A week day ("Tuesday"--meaning its due the next day that is a Tuesday)
    3. A Relative Date ("Tomorrow", "the day after tomorrow", "in 2 days")

    The program should return an array with the title of the task, and an absolute representation of the due date.

    At the very least, the program should understand absolute dates and "tomorrow."

    Anything more counts as bonus.

    Also, if you can get the program to actually keep track of your tasks, that would be great.

    Remember, normal rules apply - the code should be all your code - try not to copy/paste other code, but read it, understand it, and type it yourself.

    Good luck

  2. #2
    Join Date
    Nov 2010

    Re: Beginner's Programming Challenge 17

    Not familiar with these challenges, are we free to code in any language for this?

  3. #3
    Join Date
    Apr 2007
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner's Programming Challenge 17

    Quote Originally Posted by austinprete View Post
    Not familiar with these challenges, are we free to code in any language for this?
    Yup, although we usually ask that you use languages with a package available from the repos (for convenience). This includes most popular languages.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  4. #4
    Join Date
    Nov 2010

    Re: Beginner's Programming Challenge 17

    Alright, I wasn't going to do anything crazy with it. I'll probably either attempt it in C or Python. Just wanted to make sure it wasn't a specific language.

  5. #5
    Join Date
    Jun 2006

    Re: Beginner's Programming Challenge 17

    My entry in ruby:
    #!/usr/bin/env ruby
    require 'date'
    puts "Enter your task in the form:"
    puts "[Task Description] due [Date]"
    date_string,task = gets.chomp.reverse.split(" eud ",2).each{|s| s.reverse!}
        date = Date.parse(date_string)
    rescue ArgumentError
        date = case
               when date_string.downcase.match("day after tomorrow")
                   then + 2
               when date_string.downcase.match("tomorrow")
                   then + 1
               when days = date_string.downcase.match(/in (\d+) day/)
                   then + days[1].to_i
               else nil
    if !date
        $stderr.puts "ERROR: Unable to parse date."
        exit -1
    puts task,date
    • understands "tomorrow", "day after tomorrow" and "in xx day(s)", in addition to most permutations of YYYY-MM-DD.
    • correctly handles the string " due " appearing in a task description.

  6. #6

    Re: Beginner's Programming Challenge 17

    revised edition..
    '''convert received date from stdin to datetime object with the following format, DD-month_abbreviation-YYYY
    ex. 2000 12 november -> 12 Nov 2000'''
    import datetime
    import re
    local =
    def get_task():
    	task_date = input('input task title and date:')
    	return [a.rstrip().lstrip() for a in task_date.split('due')]
    def parse_date(today):
    	days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
    	if 'in' in today:
    		diff = int('(\d+)', today).group(0))
    		reply = lang_format(diff)
    	elif 'day after tomorrow' in today:
    		diff = 2
    		reply = lang_format(diff)
    	elif today == 'tomorrow':
    		diff = 1
    		reply = lang_format(diff)
    	elif today.lower() in days:
    		reply = days_format(today, days)
    		reply = iso_format(today)
    	if reply != None:
    		return reply
    def iso_format(today):
    	date_formats = ["%d %b %Y", "%d %b %y", "%Y %b %d", "%d %B %Y", "%d %B %y"]
    	for form in date_formats:
    			iso_date = datetime.datetime.strptime(today, form)
    			return iso_date
    		except ValueError:
    		print("entry not valid")
    		return None
    def lang_format(diff=1):	
    	'''lang_format (int -> datetime object)
    	add the integer given to today's date and return 
    	ex. today = 2000-12-11, int = 3 -> 2000-12-14'''
    	future = datetime.datetime.toordinal(local)
    	tomorrow = future + diff
    	tomorrow =
    	return tomorrow
    def days_format(today, days):
    	'''days_format (string, list -> datetime object)
    	take the literal day as input and calculate the date that it falls on.
    	ex. today is thursday, nov. 11
    	string = friday -> nov. 12'''
    	now ="%A").lower()
    	days = days * 2
    	diff = days[days.index(now):].index(today)
    	if diff == 0: #diff == 0 if the entered day is the same as today, add 7 to calculate the date that the day falls on next
    		diff = 7
    	return lang_format(diff)
    def main():
    	task_date = get_task()
    	future = parse_date(task_date[1])
    	print("{} due {}".format(task_date[0], future))
    if __name__ == "__main__":
    understands 'tomorrow', 'the day after tomorrow', 'in xx days', any day of the week

    *fixed bug: if 'tomorrow', 'the day after tomorrow', 'in xx days', any day of the week carries over into the next month, it will crash. haven't handled it yet.

    oh the code is commented, it might make it a bit harder to read here since there's no syntax highlighting.
    Last edited by mo.reina; December 7th, 2010 at 10:47 AM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts