Test Setup Failed
Push — master ( 65a459...91fe62 )
by Ken M.
47s
created

most_frequent_days()   D

Complexity

Conditions 8

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
dl 0
loc 34
rs 4
c 0
b 0
f 0
1
import datetime
2
from collections import Counter
3
4
5
def most_frequent_days(year):
6
    # since the week repeats in the whole year, we only need to find out
7
    # incompleted weekdays in the beginning and the end of the year.
8
9
    # incompleted weekdays in the beginning
10
    week_list = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
11
                 'Friday', 'Saturday', 'Sunday']
12
    weekdays = []
13
    for i in range(1, 8):
14
        weekday = datetime.datetime(year, 1, i).isoweekday()
15
        if weekday == 1:
16
            # no incompleted week in beginning
17
            break
18
        weekdays.append(weekday)
19
20
    # incompleted weekdays in the end
21
    for i in range(7):
22
        weekday = datetime.datetime(year, 12, 31 - i).isoweekday()
23
        if weekday == 7:
24
            # no incompleted week in beginning
25
            break
26
        weekdays.append(weekday)
27
28
    # the most common elements
29
    ret = []
30
    week_counter = Counter(weekdays).most_common()
31
    ret.append(week_counter[0][0])
32
    for i in week_counter[1:]:
33
        if i[1] == week_counter[0][1]:
34
            ret.append(i[0])
35
        else:
36
            break
37
    ret = sorted(ret)
38
    return [week_list[i] for i in ret]
39
40
41
if __name__ == '__main__':  # pragma: no cover
42
    # These "asserts" using only for self-checking and not necessary for
43
    # auto-testing
44
    assert most_frequent_days(2399) == ['Friday'], "1st example"
45
    assert most_frequent_days(1152) == ['Tuesday', 'Wednesday'], "2nd example"
46
    assert most_frequent_days(56) == ['Saturday', 'Sunday'], "3rd example"
47
    assert most_frequent_days(2909) == ['Tuesday'], "4th example"
48