In this post I’ll write an implementation in Python of Zeller’s Congruence, a simple and elegant little formula to carry out the seemingly complex task of calculating the day of the week (Monday, Tuesday etc.) from a date.

在这篇文章中,我将用Zeller的Congruence用Python编写一个实现,这是一个简单优雅的小公式,可以执行看似复杂的任务,该任务从日期开始计算星期几(星期一,星期二等)。

克里斯蒂安·泽勒(Christian Zeller)和他的聪明小公式 (Christian Zeller and His Clever Little Formula)

The formula was developed by German mathematician Christian Zeller (24 June 1822, a Monday, to 31 May 1899, a Wednesday). I have always been impressed that a task which you might think highly complex and solvable only by some sort of brute-force iteration can actually be reduced to a short and elegant formula. Of course you wouldn’t use the code in this post in production applications as Python provides the functionality already, but I hope you agree it is a worthwhile programming exercise.

该公式由德国数学家克里斯蒂安·泽勒(Christian Zeller)(1822年6月24日,星期一,至1899年5月31日,星期三)开发。 我一直给我留下深刻的印象,您可能认为仅通过某种蛮力迭代就可以高度复杂且可解决的任务实际上可以简化为简短而优雅的公式。 当然,由于Python已经提供了该功能,因此您不会在生产应用程序中使用本文中的代码,但是我希望您同意这是值得进行的编程练习。

There are several versions of the formula, including two sub-versions of each, one for the Gregorian calendar and one for the Julian. This is the Gregorian version of the formula I will be implementing, in an image stolen from Wikipedia.

该公式有多个版本,每个版本包含两个子版本,一个用于格里高利历,一个用于朱利安。 这是我将要实现的公式的公历版本,该图片是从Wikipedia窃取的图像。

Note that the symbols like an elongated letter L and it’s mirror-image twin denote “floor”, ie the result of the term is rounded down to the nearest integer. The full Wikipedia article is here and is worth at least skimming.

请注意,像拉长的字母L这样的符号及其镜像双像表示“底”,即,该项的结果四舍五入到最接近的整数。 完整的Wikipedia文章在这里,至少值得一读。

Rewritten in a more computer-friendly way we get:

以更友好的计算机方式进行重写,我们得到:

开始编码 (Starting to Code)

The project consists of these files which you can clone/download from Github.

该项目包含这些文件,您可以从Github克隆/下载这些文件。

  • zeller.pyzeller.py
  • main.pymain.py

This is the first part of zeller.py.

这是zeller.py的第一部分。

import datetime
import random
import calendar
import mathdef showdatesanddays():"""Creates a selection of random dates and runs the Zeller Algorithm on them,printing out the date, and then the day according to Python and Zeller."""# Zeller gives a value 0 to 6 representing Saturday to Fridayzellerdays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]for i in range(0, 19):d = datetime.date.fromtimestamp(random.randint(1, 1000000000))print("Date:   " + str(d))print("Python: " + calendar.day_name[d.weekday()])print("Zeller: " + zellerdays[zellergregorian(d)])print("------------------")

The showdatesanddays function firstly creates a list of day names starting with Saturday. These correspond with the values returned by Zeller’s Algorithm. We need to do this as “Python Weeks” start on a Monday.

showdatesanddays函数首先创建一个以星期六开始的日期名称列表。 这些与Zeller算法返回的值相对应。 我们需要这样做,因为“ Python周”从星期一开始。

After that we enter a for loop which generates a number of random dates using the fromtimestamp and randint functions. The fromtimestamp function creates a date from the number of seconds from 1/1/1970, and using 1 billion as the upper limit of randint will create dates between 1/1/1970 and 9/9/2001, which is adequate to demonstrate the algorithm.

之后,我们进入一个for循环,该循环使用fromtimestamprandint函数生成许多随机日期。 fromtimestamp函数从fromtimestamp 1月1日开始的秒数创建日期,使用10亿作为randint的上限将创建1/1/1970到9/9/2001之间的日期,这足以证明算法。

After that we just need to print out the date and the corresponding day name using Python’s calendar.day_name list. Finally we print out the day name again from the zellerdays list, using a call to our zellergregorian function as the list index.

之后,我们只需要使用Python的calendar.day_name列表打印日期和相应的日期名称即可。 最后,我们通过调用zellergregorian函数作为列表索引,再次从zellerdays列表中打印出日期名称。

We can now implement that function:

我们现在可以实现该功能:

def zellergregorian(d):"""Runs the Zeller algorithm on the given dateand returns the day index 0 to 6 for Saturday to Friday."""q = d.daym = d.monthY = d.year# adjust month to run from 3 to 14 from March to Februaryif m <= 2:m+= 12# and also adjust year if January or Februaryif d.month <= 2:Y -= 1h = (q + math.floor((13 * (m + 1)) / 5) + Y + math.floor(Y / 4) - math.floor(Y / 100) + math.floor(Y / 400)) % 7return h

In zellergregorian we first declare three variables for day, month and year, setting them to the values from the date function argument. Of course we could just use the values in the date directly but I wanted to use variable names in the formula which matched those used by Herr Zeller. (I don’t know why day is called “q”!)

zellergregorian我们首先为日,月和年声明三个变量,并将它们设置为date函数参数中的值。 当然,我们可以直接使用日期中的值,但我想在公式中使用与Zeller先生使用的变量名称匹配的变量名称。 (我不知道为什么一天被称为“ q”!)

We then need to adjust month and year if the month of our date is January or February as the algorithm uses years running from March to February, indexed 3 to 14.

然后,如果我们的日期的月份是一月或二月,则需要调整月份和年份,因为该算法使用的是从三月到二月的年份,索引为3到14。

Finally we calculate the day index — if you examine this line carefully you will see it implements the algorithm given above exactly. Note the use of the math.floor function to round down various terms to the nearest integer.

最后,我们计算日指数-如果您仔细检查此行,您会看到它完全实现了上面给出的算法。 请注意使用math.floor函数将各种术语四舍五入为最接近的整数。

In main.py and after a line to print the heading we call showdatesanddays.

main.py中,在一行打印标题之后,我们将其称为showdatesanddays.

import zellerdef main():"""Zeller's Congruence calculates the day of the week from the given date."""print("-----------------------------------")print("| codedrome.com                   |")print("| Zeller's Congruence:            |")print("| Calculating the Day of the Week |")print("-----------------------------------")zeller.showdatesanddays()main()

Now we can run the program with this command…

现在我们可以使用此命令运行程序了……

python3.8 main.py

python3.8 main.py

…which will give us this output.

……这将为我们提供输出。

You will be pleased to see that Python and Zeller days are the same for all dates.

您会很高兴看到Python和Zeller的日期在所有日期都是相同的。

翻译自: https://medium.com/explorations-in-python/calculating-the-day-of-the-week-with-zellers-congruence-in-python-8009001dd84e


http://www.taodudu.cc/news/show-2466056.html

相关文章:

  • POJ 3047 Bovine Birthday 日期定周求 泽勒公式
  • 《java语言程序设计》泽勒一致性问题
  • 问题:计算给定年月日的日期是星期几(泽勒一致性)
  • 【Java】运用泽勒一致性计算某天是星期几
  • 计算某天是星期几-泽勒算法
  • 算法:泽勒的一致性 给定一个日期,输出这个日期是该年一周中的星期几 【c++ java python版本】
  • 【算法】泽勒的一致性
  • mac 电脑 java wifi密码尝试器
  • WiFi密码破解亦或是WiFi热点软件?
  • python破解wifi密码软件下载-python暴力获取wifi密码
  • 关于wifi密码破解之路-1
  • Python实现穷举破解WiFi密码
  • Python破解WIFI密码详细介绍
  • Kali Linux破解WiFi密码完整步骤
  • MAC系统下破解WIFI密码
  • wifi密码破解案列
  • wifi密码破解软件,谨慎使用!
  • 无线攻击 --Fern WiFi Cracker(图形化无线密码破解工具 )
  • 小明左右手分别拿两张纸牌:黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果,输出结果如图所示。
  • 小明左右手分别拿两张纸牌:黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果
  • cmd看控制台输出红桃、方块、黑桃、梅花乱码解决
  • 小明左右手分别拿两张纸牌:黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果,输出结果如图所示
  • 写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组 例如:[(‘红心’,2),(‘草花’,2), …(‘黑桃,A
  • 小明左右手分别拿两张纸牌,黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果,输出结果如图。
  • 小明左手拿着纸牌黑桃10,右手拿着纸牌红桃8, 现在交换手中的牌, 用程序模拟实现的过程, 并输出交换前后手中的纸牌的结果
  • 案例三:小明左右手分别拿两张纸牌:黑桃10和红心8,现在交换手中的牌。编写一个程序模拟这一个过程:两个整数分别保存在两个变量中,将这两个变量的值互换,并输出互换后的结果。...
  • 现有16张牌:红桃A、Q、4;黑桃J、8、4、2、7、3;草花K、Q、5、4、6;方块A、5.抽出其中一张告诉甲点数,告诉乙花色。甲说,我不知道这张牌,乙说,我知道你不知道
  • 小明左右手分别拿了两张牌,黑桃十和红心八,现在交换手中的牌。编写一个程序模拟这一过程:两个整数分别保存两个变量,将这两个变量的值互换,并输出互换后的结果
  • 【面试概率】52张扑克牌,红桃A和黑桃A同时被一个人拿到的概率
  • “黑桃A” 11月19日团队实训总结

使用python中的zellers一致性计算星期几相关推荐

  1. Python中序列的累积计算

    [小白从小学Python.C.Java] [Python-计算机等级考试二级] [Python-数据分析] Python中序列的累积计算 cumsum()函数 选择题 以下python代码输出什么? ...

  2. 站长在线python精讲:在Python中使用len()函数计算字符串的长度详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中使用len()函数计算字符串的长度详解>.本知识点主要内容有:在Python中使用len()函数计算字符串在 ...

  3. python工程计算软件库_python中常用的科学计算工具包

    我们最了解的科学计算工具可能是Matlab,它能进行集数值计算,可视化工具及交互于一身,可惜的是它是一个商业产品.开源方面除了GNU Octave在尝试做一个类似Matlab的工具包外,Python的 ...

  4. Python中布尔类型 短路计算原理

    Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算: 与运算:只有两个布尔值都为 True 时,计算结果才为 True. True and True # ...

  5. python中backward_pytorch的梯度计算以及backward方法详解

    基础知识 tensors: tensor在pytorch里面是一个n维数组.我们可以通过指定参数reuqires_grad=True来建立一个反向传播图,从而能够计算梯度.在pytorch中一般叫做d ...

  6. python中心性评价_centrality 计算复杂网络中的节点或边 数中心性,基于python的 工具箱 matlab 238万源代码下载- www.pudn.com...

    文件名称: centrality下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Python 文件大小: 101 KB 上传时间: 2014-03-13 下载次数: 4 详细说明:计算 ...

  7. python 中自己写方法 计算向量长度 / 实现向量归一化

    # coding=utf-8 import math class Vector(object):"""docstring for Vector""&q ...

  8. Python 中的简单算术计算

    1. 除法 a, b, c, d, e =3, 2, 2.0, -3, 10 print(a / b) print(a / c) 输出都是: 1.5 print(d / b) print(b / a) ...

  9. python datetime计算时间差_Python中关于日期的计算总结

    1.获取当前时间的两种方法: 代码如下: 2.获取上个月最后一天的日期(本月的第一天减去1天) 代码如下: 3.获取时间差(时间差单位为秒,常用于计算程序运行的时间) 代码如下: 4.计算当前时间向后 ...

  10. python基本统计量_Python中简单统计量的计算

    本篇文章给大家带来的内容是关于Python中简单统计量的计算,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.这些操作都要确保已经在电脑中安装好了Anaconda集成库,如果安装好 ...

最新文章

  1. oracle 字符集 AL32UTF8、UTF8
  2. python使用正则表达式判别字符串是否以一个大写字符起始而跟随了一些小写字符
  3. 求解最大字段和的几种方法
  4. 【Android】3.12 兴趣点( POI)搜索功能
  5. Spring集成Shiro框架实战
  6. TCP服务器端和客户端建立连接 - 客户端的回调处理
  7. 【蓝桥杯官网试题 - 历届试题】小朋友排队(逆序数,树状数组)
  8. BugkuCTF-WEB题alert
  9. mariadb mysql表_mysql/mariadb学习记录——创建删除数据库、表的基本命令
  10. Java边缘填充_任意画一个多边形,用边缘填充算法填充
  11. 【转】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation 汉字编码
  12. Atitit  技术经理职责与流程表总结
  13. 跨计算机建立视图_解读 | 2019年10篇计算机视觉精选论文(上)
  14. 用Qt实现QQ好友列表界面伸缩功能(完全一模一样)(伸展和收缩、抽屉效果、类似树形控件)(鼠标划过QSS效果)
  15. linux输入密码后提示密码错误,用sudo命令没有提示输入密码,而是出现sudo:3次错误密码尝试的解决...
  16. 容器技术-Docker 网络03-用户自定义网络-网络命令的使用
  17. FlexRay通信机制
  18. N、Z、Q、R 分别代表什么
  19. stm32 ST-Link V2下载出现 No target connected问题
  20. 固态硬盘、机械硬盘工作原理和区别(内附接口知识)

热门文章

  1. 存储专栏:深度解读高端存储的快照技术
  2. 博客迁移说明 : )
  3. 微软“断臂求生”,能实现绝地反击吗?
  4. Python 情人节超强技能 导出微信聊天记录生成词云,深入讲解Python
  5. NFC bcm2079x驱动学习 .
  6. 如何在SQL SERVER的windows身份验证添加一个SQL Server身份验证方式
  7. tplink错误代码51215_TPLINK路由器设置后访问受限
  8. python白噪声检验结果查询_python白噪声
  9. 秀米html编辑器,ueditor集成秀米编辑器
  10. bh1750采集流程图_多路BH1750光强检测系统的设计