mà không cần sử dụng Lib chỉ là mã thuần túy:
#Calculate the Days between Two Date
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isLeapYear[year]:
# Pseudo code for this algorithm is found at
# //en.wikipedia.org/wiki/Leap_year#Algorithm
## if [year is not divisible by 4] then [it is a common Year]
#else if [year is not divisable by 100] then [ut us a leap year]
#else if [year is not disible by 400] then [it is a common year]
#else[it is aleap year]
return [year % 4 == 0 and year % 100 != 0] or year % 400 == 0
def Count_Days[year1, month2, day1]:
if month2 ==2:
if isLeapYear[year1]:
if day1 < daysOfMonths[month2-1]+1:
return year1, month2, day1+1
else:
if month2 ==12:
return year1+1,1,1
else:
return year1, month2 +1 , 1
else:
if day1 < daysOfMonths[month2-1]:
return year1, month2, day1+1
else:
if month2 ==12:
return year1+1,1,1
else:
return year1, month2 +1 , 1
else:
if day1 < daysOfMonths[month2-1]:
return year1, month2, day1+1
else:
if month2 ==12:
return year1+1,1,1
else:
return year1, month2 +1 , 1
def daysBetweenDates[y1, m1, d1, y2, m2, d2,end_day]:
if y1 > y2:
m1,m2 = m2,m1
y1,y2 = y2,y1
d1,d2 = d2,d1
days=0
while[not[m1==m2 and y1==y2 and d1==d2]]:
y1,m1,d1 = Count_Days[y1,m1,d1]
days+=1
if end_day:
days+=1
return days
# Test Case
def test[]:
test_cases = [[[2012,1,1,2012,2,28,False], 58],
[[2012,1,1,2012,3,1,False], 60],
[[2011,6,30,2012,6,30,False], 366],
[[2011,1,1,2012,8,8,False], 585 ],
[[1994,5,15,2019,8,31,False], 9239],
[[1999,3,24,2018,2,4,False], 6892],
[[1999,6,24,2018,8,4,False],6981],
[[1995,5,24,2018,12,15,False],8606],
[[1994,8,24,2019,12,15,True],9245],
[[2019,12,15,1994,8,24,True],9245],
[[2019,5,15,1994,10,24,True],8970],
[[1994,11,24,2019,8,15,True],9031]]
for [args, answer] in test_cases:
result = daysBetweenDates[*args]
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test[]
Python Basic: Bài tập-14 với giải pháp
Viết một chương trình Python để tính toán số ngày giữa hai ngày.
Python datetime.date [năm, tháng, ngày]:
Hàm trả về đối tượng ngày với cùng năm, tháng và ngày. Tất cả các lập luận được yêu cầu. Đối số có thể là số nguyên, trong các phạm vi sau:
- MINYEAR
- 1
- 1
Nếu một đối số bên ngoài các phạm vi đó được đưa ra, giá trịerror sẽ được nêu ra.
- Lưu ý: Số năm nhỏ nhất được phép trong một ngày hoặc đối tượng DateTime. Minyear là 1.
- Số năm lớn nhất được phép trong một ngày hoặc đối tượng DateTime. Maxyear là 9999.
Trình bày bằng hình ảnh:
Giải pháp mẫu:-:-
Mã Python:
from datetime import date
f_date = date[2014, 7, 2]
l_date = date[2014, 7, 11]
delta = l_date - f_date
print[delta.days]
Đầu ra mẫu:
9
Flowchart:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.
Trước đây: Viết một chương trình Python để in các 'tài liệu tại đây'.next: Viết chương trình Python để có được âm lượng của một quả cầu có RADIUS 6. Write a Python program to print the following 'here document'.
Next: Write a Python program to get the volume of a sphere with radius 6.
Python: Lời khuyên trong ngày
Ghi nhớ bằng cách sử dụng bộ đệm LRU:
import functools @functools.lru_cache[maxsize=128] def fibonacci[n]: if n == 0: return 0 elif n == 1: return 1 return fibonacci[n - 1] + fibonacci[n - 2]