Changes

Jump to: navigation, search

OPS435 Python3 Assignment 1

14 bytes removed, 02:15, 17 September 2019
Computation Requirements
= Computation Requirements =
== Algorithm Design ==
* Write a step-by-step instructions in English on how to compute a date in "YYYYMMDDYYYY/MM/DD" format which is n days before or after a given date also in "YYYYMMDDYYYY/MM/DD" format.
* While you are working on the step-by-step instructions, note that there are different number of days in each month and some years have 365 days and some years have 366 days.
* You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.)
Based on the algorithm you have designed for this assignment, you should at least have the following three functions defined in your python script (see later section on the purpose of each function) in order to get a passing grade for this assignment:
* dbda()
* tomorrowafter()* yesterdaybefore()
You should also create additional functions to improved the re-usability of your python code by adding the following functions to earn the maximum possible mark for this assignment:
* days_in_mon()
== Documentation ==
* Please use python's docstring to document your python script (script level documentation) and each of the functions (function level documentation) you created for this assignment. The docstring should describle describe 'what' the function does, not 'how' it does.* The following shows the docstring that was added to the tomorrowafter() function which provides the following information when called with help(tomorrowafter) in the python interactive shell:
<pre>
Help on function tomorrow after() in module rchan:
tomorrowafter(today) -> str tomorrowafter() takes a valid date string in 'YYYYMMDDYYYY/MM/DD' format and return a date string for the next day in 'YYYYMMDDYYYY/MM/DD' format. e.g. tomorrowafter('20171231') -> '20180101' tomorrowafter('20180131') -> '20180201' tomorrowafter('20180228') -> '20180301'
(END)
</pre>
== Authorship Declaration ==
All your Python code for this assignment must be placed in a <font color='red'><b><u>single source python file</u></b></font>. Please include the following declaration <b><u>as part of the docstring</u></b> in your Python source code file (replace "Student Name" with your own name):
<source>OPS435 Assignment 1 - Summer Fall 2019Program: a1_[student_id].py (replace student_id with your Seneca User name)
Author: "Student Name"
The python code in this file (a1_[Student_id].py) is original work written by
"Student Name". No code in this file is copied from any other source
except those provided by the course instructor, including any person,
== Tests and Test results ==
You must name your python 3 script as <code>a1_[Student_id].py</code>. The following examples assumes that the student_id is rchan.The script should accept two command line arguments, the first one is the date in "YYYYMMDDYYYY/MM/DD" format, and the second one is the number of day from the given date, a positive value indicates the number of days after the given date, and a negative value indicates the number of days before the given date. There is an optional flag called --step which can be provided at the command line that makes the program print out all dates until the target date. If the "YYYYMMDDYYYY/MM/DD" format is broken, your script should give an appropriate error message. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate error messages. For examples:* <b><code>python3 a1_rchan.py 20190101 2019/01/01 1</code></b>, and the output should be<br /> 201901022019/01/02* <b><code>python3 a1_rchan.py 20190101 2019/01/01 -1</code></b>, and the output should be<br /> 201812312018/12/31* <b><code>python3 a1_rchan.py 20190101 2019/01/01 2</code></b>, and the output should be<br /> 201901032019/01/03* <b><code>python3 a1_rchan.py --step 20190101 2019/01/01 3</code></b>, and the output should be<br /> 201901022019/01/02 201901032019/01/03 201901042019/01/04* <b><code>python3 a1_rchan.py 20180701 2018/07/01 500</code></b>, and the output should be<br /> 201911132019/11/13* <b><code>python3 a1_rchan.py 20189901 2018/99/01 2</code></b>, and the output should be<br />
Error: wrong month entered
* <b><code>python3 a1_rchan.py 20180199 2018/01/99 2</code></b>, and the output should be<br />
Error: wrong day entered
* <b><code>python3 a1_rchan.py 2018 2</code></b>, and the output should be<br />
If there is too few or too many command line arguments given, display the proper usage:
* <code>Usage: a1_rchan.py [--step] YYYYMMDD YYYY/MM/DD +/-n</code>
== Script structure and sample template ==
Your code should all be in a single python file with at least the functions mentioned above: dbda(), tomorrowafter(), and yesterdaybefore(). To earn the maximum mark, you should also create additional functions into your algorithm, e.g.: leap_year(), days_in_mon, valid_date(), usage(), etc
The following is a brief description of each function:
* The dbda() function should be the main function of your script. The dbda() function will take a date in "YYYYMMDDYYYY/MM/DD" format, a positive or negative integer, and return a date either before or after the given date according to the value of the given integer in the same format. Your dbda() function should delegate the actual calculation of the target date to either the tomorrowafter() function or the yesterdaybefore() function. * The yesterdaybefore() function will take a date in "YYYYMMDDYYYY/MM/DD" format and return the date of the previous day in the same format.* The tomorrowafter() function will take a date in "YYYYMMDDYYYY/MM/DD" format and return the date of the next day in the same format. Next paragraph is a sample python code for the tomorrowafter() function. To earn the maximum possible mark for the assignment, you should modify the sample tomorrowafter() function to make use of the days_in_mon() function.
* The leap_year() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
* The valid_date() function will take a date in "YYYYMMDDYYYY/MM/DD" format, and return True if the given date is a valid date, otherwise return Falseplus an appropriate status message. The valid_date() function should make use of the days_in_mon() function.* The days_in_mon() function will take a year in "YYYY" format, and return a dictionary object which contains the total number of days in each month of for the given year. The days_in_mon() function should make use of the leap_year() function.
* The usage() function will take no argument and return a string describing the usage of the script.
<pre>
import ...
def tomorrowafter(today):
....
return next_day
def yesterdaybefore(today):
....
return previous_day
....
def dbda(yyyymmdddate,days):
...
setup loop
call tomorrow after() or yesterday before() as appropriate
return target_day
.. call dbda()
...
.. output the expected date
</pre>
=== Sample code for the tomorrowafter() function ===
<pre>
# Return the date in YYYYMMDD YYYY/MM/DD after the given day
#
def tomorrow(today):
if len(today) != 810: return '000000000000/00/00'
else:
year = int(today[0:4]) , month , day = int(today[4:6]) day = int.split(today[6:]'/')
lyear = year % 4
feb_max = 29 # this is a leap year
tmp_day = day + 1 # tomorrow's next day
mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
to_month = tmp_month + 0
next_date = str(year)+"/"+str(to_month).zfill(2)+"/"+str(to_day).zfill(2)
return next_date
1,760
edits

Navigation menu