the_most_frequent_weekdays   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 7

1 Function

Rating   Name   Duplication   Size   Complexity  
B most_frequent_days() 0 42 7
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 = [
11
        '',
12
        'Monday',
13
        'Tuesday',
14
        'Wednesday',
15
        'Thursday',
16
        'Friday',
17
        'Saturday',
18
        'Sunday',
19
    ]
20
    weekdays = []
21
    for i in range(1, 8):
22
        weekday = datetime.datetime(year, 1, i).isoweekday()
23
        if weekday == 1:
24
            # no incompleted week in beginning
25
            break
26
        weekdays.append(weekday)
27
28
    # incompleted weekdays in the end
29
    for i in range(7):
30
        weekday = datetime.datetime(year, 12, 31 - i).isoweekday()
31
        if weekday == 7:
32
            # no incompleted week in beginning
33
            break
34
        weekdays.append(weekday)
35
36
    # the most common elements
37
    ret = []
38
    week_counter = Counter(weekdays).most_common()
39
    ret.append(week_counter[0][0])
40
    for i in week_counter[1:]:
41
        if i[1] == week_counter[0][1]:
42
            ret.append(i[0])
43
        else:
44
            break
45
    ret = sorted(ret)
46
    return [week_list[i] for i in ret]
47
48
49
if __name__ == '__main__':  # pragma: no cover
50
    # These "asserts" using only for self-checking and not necessary for
51
    # auto-testing
52
    assert most_frequent_days(2399) == ['Friday'], "1st example"
53
    assert most_frequent_days(1152) == ['Tuesday', 'Wednesday'], "2nd example"
54
    assert most_frequent_days(56) == ['Saturday', 'Sunday'], "3rd example"
55
    assert most_frequent_days(2909) == ['Tuesday'], "4th example"
56