-5
编写接受包含从公历特定 日期的字符串的函数。你的功能应该返回当天的第一天 。以下是一些输入 字符串(月份日年)的示例。但是,您不应该将 '硬编码'您的程序仅用于这些输入!如何找出python中给定日期的一周中的哪一天?
"June 12 2012"
"September 3 1955"
"August 4 1843"
注意,每个项目(年月日)由一个空格隔开。对于 例如,如果输入字符串为:
"May 5 1992"
然后你的函数应该返回星期几(字符串),例如:
"Tuesday"
算法样本例如:
# Assume that input was "May 5 1992"
day (d) = 5 # It is the 5th day
month (m) = 3 # (*** Count starts at March i.e March = 1, April = 2, ... January = 11, February = 12)
century (c) = 19 # the first two characters of the century
year (y) = 92 # Year is 1992 (*** if month is January or february decrease one year)
# Formula and calculation
day of the week (w) = (d + floor(2.6m - 0.2) - 2c + y + floor(y/4) + floor(c/4)) modulo 7
after calculation we get, (w) = 2
Count for the day of the week starts at Sunday, i.e Sunday = 0, Monday = 1, Tuesday = 2, ... Saturday = 6
因为我们得到了2,1992年5月5日星期二
我的第一个问题是我怎么接受June 12 2012
从用户输入?有什么方法可以让我做到这一点?任何帮助,将不胜感激。
你真的要抛弃你在这里得到的每个家庭作业问题吗? – marcelm