Jack J Jack J - 6 months ago 7
Python Question

Project Euler 19. Too many Sundays

From Euler:
1 Jan 1900 was a Monday. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

My first function collects every day into a list starting from the known Monday and ending on Dec 31, 2000. The second function saves every seven days into a list of Sundays beginning with the first Sunday in 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())


Output: 200.
My answer is off by 29. The required answer is 171.
This is my list of Sundays:

[[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]]


I'm not sure what I have done wrong.

Answer

You're using modulus incorrectly. Try:

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