blob: c3aa068659d50dd9f3f782bf8508ecaba79993b9 (
plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/usr/bin/python3
'''
Week 138:
https://theweeklychallenge.org/blog/perl-weekly-challenge-138
Task #1: Workdays
You are given a year in 4-digits form. Write a script to calculate the total number of workdays in the given year.
'''
import unittest
from datetime import datetime
from datetime import timedelta
def workdays(year):
d = 1
m = 1
y = year
workdays = 0
while year == y:
date = datetime(y, m, d)
if date.weekday() < 5: workdays += 1
date = date + timedelta(days = 1)
y = date.year
m = date.month
d = date.day
return workdays
#
#
# Unit test class
class TestWorkdays(unittest.TestCase):
def test_example_1(self):
self.assertEqual(workdays(2021), 261, 'Example 1')
def test_example_2(self):
self.assertEqual(workdays(2020), 262, 'Example 2')
unittest.main()
|