python - 欧拉计划 19. 周日太多

标签 python

来自欧拉: 1900 年 1 月 1 日是星期一。二十世纪(1901年1月1日至2000年12月31日)有多少个星期日是每月的第一天?

我的第一个函数将每天收集到一个列表中,从已知的星期一开始,到 2000 年 12 月 31 日结束。第二个函数将每 7 天保存到一个星期日列表中,从 1901 年的第一个星期日开始。

def dayListBuild():
    dayListBuild = [[0,0,0]]
    for year in range(1900, 2001):
        for month in range(1, 13):
            if month in [4, 6, 9, 11]:
                dayRange = 30
            elif not month - 2:
                if year % 4 and (not year % 100 or year % 400):
                        dayRange = 29
                else:
                    dayRange = 28
            else:
                dayRange = 31
            for day in range(1, dayRange + 1):
                dayListBuild.append([year, month, day])
    return dayListBuild

def sundayList():
    dayList = dayListBuild()
    sundayList = []
    for day in range(len(dayList)):
        if not (day - 6) % 7 and dayList[day][0] > 1900 and not dayList[day][2] - 1:
            sundayList.append(dayList[day])
    return len(sundayList)

print(sundayList())

输出:200。 我的答案差了 29。要求的答案是 171。 这是我的周日 list :

[[1901, 3, 1], [1901, 11, 1], [1902, 5, 1], [1903, 1, 1], [1903, 4, 1], [1903, 7, 1], [1904, 9, 1], [1904, 12, 1], [1905, 3, 1], [1905, 11, 1], [1906, 5, 1], [1907, 1, 1], [1907, 4, 1], [1907, 7, 1], [1908, 9, 1], [1908, 12, 1], [1909, 3, 1], [1909, 11, 1], [1910, 5, 1], [1911, 1, 1], [1911, 4, 1], [1911, 7, 1], [1912, 9, 1], [1912, 12, 1], [1913, 3, 1], [1913, 11, 1], [1914, 5, 1], [1915, 1, 1], [1915, 4, 1], [1915, 7, 1], [1916, 9, 1], [1916, 12, 1], [1917, 3, 1], [1917, 11, 1], [1918, 5, 1], [1919, 1, 1], [1919, 4, 1], [1919, 7, 1], [1920, 9, 1], [1920, 12, 1], [1921, 3, 1], [1921, 11, 1], [1922, 5, 1], [1923, 1, 1], [1923, 4, 1], [1923, 7, 1], [1924, 9, 1], [1924, 12, 1], [1925, 3, 1], [1925, 11, 1], [1926, 5, 1], [1927, 1, 1], [1927, 4, 1], [1927, 7, 1], [1928, 9, 1], [1928, 12, 1], [1929, 3, 1], [1929, 11, 1], [1930, 5, 1], [1931, 1, 1], [1931, 4, 1], [1931, 7, 1], [1932, 9, 1], [1932, 12, 1], [1933, 3, 1], [1933, 11, 1], [1934, 5, 1], [1935, 1, 1], [1935, 4, 1], [1935, 7, 1], [1936, 9, 1], [1936, 12, 1], [1937, 3, 1], [1937, 11, 1], [1938, 5, 1], [1939, 1, 1], [1939, 4, 1], [1939, 7, 1], [1940, 9, 1], [1940, 12, 1], [1941, 3, 1], [1941, 11, 1], [1942, 5, 1], [1943, 1, 1], [1943, 4, 1], [1943, 7, 1], [1944, 9, 1], [1944, 12, 1], [1945, 3, 1], [1945, 11, 1], [1946, 5, 1], [1947, 1, 1], [1947, 4, 1], [1947, 7, 1], [1948, 9, 1], [1948, 12, 1], [1949, 3, 1], [1949, 11, 1], [1950, 5, 1], [1951, 1, 1], [1951, 4, 1], [1951, 7, 1], [1952, 9, 1], [1952, 12, 1], [1953, 3, 1], [1953, 11, 1], [1954, 5, 1], [1955, 1, 1], [1955, 4, 1], [1955, 7, 1], [1956, 9, 1], [1956, 12, 1], [1957, 3, 1], [1957, 11, 1], [1958, 5, 1], [1959, 1, 1], [1959, 4, 1], [1959, 7, 1], [1960, 9, 1], [1960, 12, 1], [1961, 3, 1], [1961, 11, 1], [1962, 5, 1], [1963, 1, 1], [1963, 4, 1], [1963, 7, 1], [1964, 9, 1], [1964, 12, 1], [1965, 3, 1], [1965, 11, 1], [1966, 5, 1], [1967, 1, 1], [1967, 4, 1], [1967, 7, 1], [1968, 9, 1], [1968, 12, 1], [1969, 3, 1], [1969, 11, 1], [1970, 5, 1], [1971, 1, 1], [1971, 4, 1], [1971, 7, 1], [1972, 9, 1], [1972, 12, 1], [1973, 3, 1], [1973, 11, 1], [1974, 5, 1], [1975, 1, 1], [1975, 4, 1], [1975, 7, 1], [1976, 9, 1], [1976, 12, 1], [1977, 3, 1], [1977, 11, 1], [1978, 5, 1], [1979, 1, 1], [1979, 4, 1], [1979, 7, 1], [1980, 9, 1], [1980, 12, 1], [1981, 3, 1], [1981, 11, 1], [1982, 5, 1], [1983, 1, 1], [1983, 4, 1], [1983, 7, 1], [1984, 9, 1], [1984, 12, 1], [1985, 3, 1], [1985, 11, 1], [1986, 5, 1], [1987, 1, 1], [1987, 4, 1], [1987, 7, 1], [1988, 9, 1], [1988, 12, 1], [1989, 3, 1], [1989, 11, 1], [1990, 5, 1], [1991, 1, 1], [1991, 4, 1], [1991, 7, 1], [1992, 9, 1], [1992, 12, 1], [1993, 3, 1], [1993, 11, 1], [1994, 5, 1], [1995, 1, 1], [1995, 4, 1], [1995, 7, 1], [1996, 9, 1], [1996, 12, 1], [1997, 3, 1], [1997, 11, 1], [1998, 5, 1], [1999, 1, 1], [1999, 4, 1], [1999, 7, 1], [2000, 9, 1], [2000, 12, 1]]

我不确定我做错了什么。

最佳答案

您错误地使用了模数。尝试:

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):

关于python - 欧拉计划 19. 周日太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15017099/

相关文章:

使用for arange循环时的python数组索引

python - Geometry ('POINT' ) 列作为 str 对象返回

python - Pyramid 应用程序的版权符号

Python 没有名为 'yahoofinancials' 的模块

python - 如何在 python 包中包含 PyOpenSSL

python - 选择最右边的列,在 Pandas DataFrame 中没有 nan 值

python - 在窗口的顶部中心显示 QLabel

python - RequestsDependencyWarning : urllib3 (1. 25.2) 或 chardet (3.0.4) 与支持的版本不匹配!使固定

python - 如何在不使用循环的情况下为 3D numpy 数组中的每个值做出 N 个随机选择

python - len 的文档字符串中的 "module"是什么?