티스토리 뷰
Friday the thirteenth is generally seen as an unlucky day in our culture. However, it is not immediately sure where this superstition comes from, and although there are numerous reports in various religions and customs, it seems that this superstition is at most 100 years old.
The combination of the weekday Friday and the number thirteen is also not as universal as you might think. In Belgium, the Netherlands and England, for example, Friday the thirteenth is indeed seen as an unlucky day. However, in Greece, Spain and Latin America, Tuesday the thirteenth is an unlucky day. In Italy everyone is extra careful on Friday the seventeenth.
Assignment
Write a function unluckyDays that takes a starting date as its argument. In addition, the function has three optional arguments that respectively indicate an end date, a day number and a weekday number. The function must return the number of days between the start date and the end date (including these boundaries) that fall on the specified weekday. If only the mandatory argument is given, the number of Fridays the thirteenth between the start date and today must be calculated. If the starting date comes later than the end date, there are no days between the starting date and the end date, and logically the function must return the value 0 in this case.
Preparation
In this assignment you will have to make use of the data types date and timedelta that are defined in the datetimemodule of the Python Standard Library. Before starting to work on the actual assignment, you should first have a look at how Python responds when you execute the following sequence of instructions in an interactive Python session:
>>> from datetime import date >>> birthday = date(1983, 1, 14) >>> d = date.today() - birthday >>> type(d) >>> d.days
>>> from datetime import timedelta >>> birthday + timedelta(1) >>> day1 = birthday + timedelta(1) >>> day1 >>> day2 = day1 + timedelta(1) >>> day2
>>> today = date.today() >>> today >>> today.weekday() >>> tomorrow = today + timedelta(1) >>> tomorrow.weekday() >>> tomorrow.day
Make sure that you understand why the different results are generated.
Example
>>> from datetime import date >>> unluckyDays(date(2012, 1, 1), date(2012, 12, 31)) 3 >>> unluckyDays(date(2012, 1, 1), date(2012, 12, 31), 14, 5) 3 >>> unluckyDays(date(2012, 1, 1), date(2012, 12, 31), 17, 4) 2 >>> unluckyDays(date(2012, 1, 1), date(2012, 12, 31), 13, 1) 2 >>> unluckyDays(startDate=date(2012, 1, 1), day=1, weekday=0, endDate=date(2012, 12, 31)) 1
from datetime import date
from datetime import timedelta
def unluckyDays(startDate, endDate=date.today(), day=13, weekday=4):
delta = 0
cur = startDate
cnt = 0
while cur <= endDate:
if cur.day == day and cur.weekday() == weekday:
cnt += 1
cur = startDate + timedelta(delta)
delta += 1
return cnt
print(unluckyDays(startDate=date(2012, 1, 1), day=1, weekday=0, endDate=date(2012, 12, 31)))
'python lecture > algorism' 카테고리의 다른 글
[edu] Isomers (0) | 2018.11.04 |
---|---|
[edu] Applied chemistry (0) | 2018.11.02 |
[edu] Colorful fruits (0) | 2018.10.24 |
[eud] bubble sorting (0) | 2018.10.15 |
[edu] Zigzag (0) | 2018.10.15 |
- Total
- Today
- Yesterday
- 장고
- pycrypto
- 플러스친구 자동응답
- 면접답변
- GIT
- 문서 비교
- PuTTYGen
- wsgi
- 파이썬 프로그래밍
- 면접정답
- 문과 코딩
- django chatbot
- 파이썬 독학
- chatbot
- 모바일 스킨 적용
- 이미지 비교
- 모바일 테마 적용
- 파이썬
- admin.py
- virtualenv
- gitlab
- Tistory
- 파이썬 강좌
- 장고 카톡 자동응답
- 장고 플러스친구 자동응답
- 파이썬 입문
- django
- gitignore
- 엑셀 비교
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |