数字转罗马数字

In today’s world, we take for granted the numerical system that we use on a daily basis. The average human being is born with ten fingers, hence a counting system on base 10 is very natural to us.

在当今世界,我们认为每天使用的数字系统是理所当然的。 一般的人天生就有十根手指,因此以10为底的计数系统对我们来说很自然。

We have exactly 10 elementary numerical symbols, ranging from 0 to 9. Every other natural number can be expressed in a permutation of these 10 symbols. I was teaching my eldest daughter addition with change, and she was able to keep up with the lesson. This serves as proof that a five-year old would readily grasp numbers in base 10, and would be able to add any two natural numbers to form a third natural number using no more than the 10 elementary numerical symbols.

我们正好有10个基本数字符号,范围从0到9。每个其他自然数都可以用这10个符号的排列表示。 我当时正在教我的大女儿增添零钱,她也能跟上这一课。 这证明了一个五岁的孩子将很容易掌握以10为底的数字,并且能够使用不超过10个基本数字符号的方式将任意两个自然数相加以形成第三个自然数。

What if human beings were born with other than 10 fingers? What if an alien race were born with 8 digits in total on both hands instead? Would they be computing in base 8?

如果人类不是用十个手指出生的怎么办? 如果外星种族出生时双手双手总共有8位数字怎么办? 他们会以8为底进行计算吗?

Sometimes, even when we have 10 fingers on 2 hands, the fact that it is possible to consider just the 5 fingers on 1 hand, civilisation could reach a different conclusion and decide to count in base 5. The Mayans used both fingers and toes to develop a base 20 system.

有时,即使我们两只手有10个手指,也可能只考虑一只手的5个手指,但文明可能会得出不同的结论,并决定以5为基数。开发以20为基础的系统。

Today’s post is inspired by this challenge on Edabit. So if you want to attempt to solve the puzzle by yourself beforehand without any spoilers, do not read any further.

今天的帖子受到Edabit挑战的启发。 因此,如果您想尝试自己先解决难题而又不破坏任何事物,请不要再继续阅读。

The Romans had a very interesting counting system that is still in use today. The system combines elements of 5s and 10s, as well as 5 less 1s and 10 less 1s. This logic is extended by each power of 10. So, think of 50s and 100s, 50 less 10s and 100 less 10s. And so on and so forth.

罗马人有一个非常有趣的计数系统,至今仍在使用。 该系统结合了5s和10s的元素,以及少5个1和少10个1。 此逻辑通过10的幂进行扩展。因此,考虑50s和100s,50减去10s和100减去10s。 等等等等。

When we break the counting method down to individual elements, we can group them into two distinct sets of data. In this example we arrange them into two Python dictionaries.

当我们将计数方法分解为单个元素时,可以将它们分为两组不同的数据。 在此示例中,我们将它们分成两个Python字典。

In the first dictionary, we define what each latin numeral represents. Unlike Arabic numerals, which have 10 symbols, the Romans made do with 3 at each 10s. So at its simplest, you could constitute any number from 1 to 10 using just, I, V, and / or X. So II would represent 2, and VII would represent 7.

在第一个字典中,我们定义每个拉丁数字代表的含义。 与带有10个符号的阿拉伯数字不同,罗马人每10s用3个数字。 因此,最简单的说,您可以使用I,V和/或X构成从1到10的任何数字。因此II代表2,VII代表7。

The counting system also had a minus-one feature, where IV would represent 4, or V less I. This necessitates the second Python dictionary, which handles these minus-one examples, for both 5s, 10s, or its 10-multiples. So XL would be 50 less 10 to make 40, while CM would be 1,000 less 100 to make 900.

计数系统还具有减一功能,其中IV表示4,或减V减I。这需要第二个Python字典,该字典处理5、10或10的10个整数的这些减一示例。 因此XL将减去50减去10得到40,而CM将减去1000减去100得到900。

With this basic understanding, we set off to write a program that would be able to convert a modern-day Arabic numeral into its Roman equivalent, and vice versa.

基于这种基本理解,我们开始编写一个程序,该程序能够将现代阿拉伯数字转换为罗马数字,反之亦然。

So — I know we have not written these yet — let us imagine that we already have one program called convert_numeral_to_roman and another one called convert_roman_to_numeral. We can deduce the nature of the conversion by looking at the input’s data type. If a Roman numeral is fed into the program we want to write, it would be a string of Latin letters. If it were modern-day numerals, it would be an integer. So we could write the function-in-question like this:

所以-我知道我们还没有编写这些代码-让我们想象一下,我们已经有一个名为convert_numeral_to_roman的程序和另一个名为convert_roman_to_numeral的程序。 我们可以通过查看输入的数据类型来推断转换的性质。 如果将罗马数字输入到我们要编写的程序中,则它将是一串拉丁字母。 如果它是现代数字,则将是整数。 因此我们可以这样编写问题函数:

While we have not written both sub-programs, it is good to jot these two lines down first so we know there are two to dos in order to make the program work.

虽然我们都没有编写两个子程序,但是最好先将这两行记下来,以便我们知道有两个要做的事情才能使程序正常工作。

So for the first part, how do we parse a latin numeral into its modern-day equivalent? One way which I found works is to translate the numbers from left to right, adding on as we go along. It is important to start from the minus-one dictionary first. As two elements are analysed at the same time, this resolves the disambiguation problem of the machine misinterpreting the Roman letters for its individual parts. For instance, had we used the first dictionary, IV would be read as 1 plus 5 equals 6, instead of 4.

那么,对于第一部分,我们如何将拉丁数字解析为现代数字呢? 我发现有效的一种方法是将数字从左向右转换,并随着我们的前进而增加。 首先要从负一字典开始,这一点很重要。 由于同时分析了两个元素,因此解决了机器误解罗马字母各个部分的歧义问题。 例如,如果我们使用第一个字典,则IV将读为1加5等于6,而不是4。

It is only when the program is not able to find the pattern in the first two elements of the Roman numeral that we would resort to analysing just the first element. In Python, this could be done by purposely causing the program to throw an error by asking it to search for a key that does not exist in the minus-one dictionary, redirecting the code using the try-except arrangement. Once we are in the exception handing block, we change our analysis to just the first element of the latin string.

只有当程序无法在罗马数字的前两个元素中找到模式时,我们才求助于仅分析第一个元素。 在Python中,这可以通过有意使程序搜索负一号字典中不存在的键,并使用try-except方式重定向代码来故意引发程序抛出错误。 一旦进入异常处理区,就将分析更改为拉丁字符串的第一个元素。

As the elements get analysed, we add the sum of the parts and successively shorten the remainder of the Roman numeral. The answer is found when there are no more latin letters left to analyse.

在分析元素时,我们将各部分的总和相加并依次缩短罗马数字的其余部分。 当没有更多的拉丁字母要分析时,找到答案。

What if a number is fed to the program? This time, we would have to translate the number into its constituent Roman letters — starting from the largest possible value.

如果将数字输入程序,该怎么办? 这次,我们必须将数字转换为其组成的罗马字母-从可能的最大值开始。

The dictionary in use here would be a merger of the two original dictionaries since we only need one here.

这里使用的字典将是两个原始字典的合并,因为我们在这里只需要一个。

This time the access would go in reverse — the modern-day integers would be used to determine the Roman letters. So we use dictionary comprehension to swap the keys and values, utilising the items method native to the dictionary object in the process. Basically, we could think of dictionary.items() as a list of key-value tuples. The technique is a must-know in the art of dictionary manipulation.

这次访问将反向进行-现代整数将用于确定罗马字母。 因此,我们使用字典理解来交换键和值,在此过程中利用字典对象固有的方法。 基本上,我们可以将dictionary.items()视为键值元组的列表。 该技术是词典操作领域中必不可少的知识。

Once the combined dictionary with integer keys is in place, we can start encoding the Roman letters — exhausting the largest-value letters first before descending down the chain. This is why we loop by accessing the keys, sorted by largest to smallest. We keep encoding until the remainder is 0 and we subsequently return the answer.

一旦使用整数键组合字典,我们就可以开始对罗马字母进行编码-在沿链下降之前先用尽最大的字母。 这就是为什么我们通过访问按最大到最小排序的键来循环。 我们一直编码,直到余数为0,然后我们返回答案。

If you have followed this far, I hope that you would agree that while MMXX has not been easy — let us continue to do the best we can and emerge stronger once MMXXI comes along.

如果您已经走了这么远,我希望您同意,尽管MMXX并非易事-让我们继续尽力而为,一旦MMXXI出现,我们将变得更加强大。

翻译自: https://medium.com/swlh/making-sense-of-roman-numerals-c49d55e2b682

数字转罗马数字


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

相关文章:

  • 欧姆龙 PLC CP1E Modbus RTU通信程序
  • Labview Ethernetip TCP网口通讯欧姆龙PLC
  • 电脑用USB连接欧姆龙安全PLC失败解决方案
  • Linux制作系统U盘
  • 制作多系统启动优盘
  • 装ubuntu系统U盘制作启动盘
  • 服务器 u盘制作系统盘,制作U盘系统盘(WinToFlash)
  • Matlab常用清除命令:clc、clear、clear all、clf、close、close all
  • Matlab中的clc、clear、clear all、clf、close、close all解释
  • 【MATLAB】matlab中clc,close,close all,clear,clear all作用区别
  • MATLAB||清除指令clear,clear all,clc,clf,cla
  • matlab绘制抛物线,MATLAB中绘制抛物线的图像,请补充完成下面代码: clc,clear; x=linspace(...
  • matlab中clear的功能,matlab中clc,close,close all,clear,clear all作用区别
  • TI10假赛?
  • 南阳百里奚计算机学校,南阳之子 百里奚
  • %3c韩非子%3e语言研究pdf,韩非子王先慎.pdf
  • 安兰德《源泉》读后感:愿年轻人都能有志气
  • Flink入门教程(三)——窗口(一)
  • 孙子兵法与三十六计 复盘短记
  • 教育专家李彦良谈双减后的素质教育和智慧教育
  • 人工智能给未来教育带来深刻变革
  • 智能与人机融合智能的思考
  • 高等教育学:学生与教师
  • javaWeb中 servlet 、request 、response
  • JavaWeb_Servlet_Request_Response
  • 计算机英语brain,人类大脑纪录片《脑力大挑战 Test Your Brain》全3季 英语中字 标清/1080P高清纪录片...
  • 成人脑力训练 3.951
  • 天工开物 #5 我的 Linux 开发机
  • 03-lvs-persistence
  • 19年的桌面KDE的风雨和陪伴,没有什么能够割舍

数字转罗马数字_理解罗马数字相关推荐

  1. 对于数字特征的若干理解

    对于数字特征的若干理解 数字特征概述 随机变量 常见数字特征 数学期望均值 方差 标准差 协方差 相关系数 协方差矩阵 参考 数字特征概述 在我们学习概率论的时候,很多时候我们不能深刻理解概率论中的数 ...

  2. 【数字IC】深入浅出理解I2C协议

    深入浅出理解I2C协议 一.什么是I2C协议 二.I2C,SPI,UART协议的区别 三.I2C的信号线 四.I2C的连接方式 4.1 单主设备,单从设备 4.2 单主设备,多从设备 4.3 多主设备 ...

  3. java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字_灵思致远Leansmall的博客-CSDN博客_java判断字符串只包含数字字母

    import java.util.regex.Matcher; import java.util.regex.Pattern;public class StrValidate {// 纯数字priva ...

  4. 【vue 项目】表单正则表达式校验1-32位,中文、英文、数字及特殊字符_-,必须以中文或英文字符开头等

    验证内容 (1)1-32位,中文.英文.数字及特殊字符_-,必须以中文或英文字符开头 (2)1-32位,中文.英文.数字及特殊字符_-,必须以中文.英文.数字开头 data() {// 表单自定义校验 ...

  5. 罗马数字转换器(用罗马数字构造元素的值取模实现)

    我的CSDN主页 My Python 学习个人备忘录 我的HOT博 罗马数字转换器(用罗马数字构造单位的值取模实现)   刷CSDN无意间看到大佬用C实现的"罗马数字转换器(FreeCode ...

  6. 字符串型的数字相加减_【每日编程158期】罗马数字转整数

    每日编程中遇到任何疑问.意见.建议请公众号留言或直接撩Q474356284(备注每日编程) 今日问题: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符          数值 ...

  7. 1到10罗马数字_从罗马到现代

    1到10罗马数字 We're so used to our regular numbers - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 - that we forget we ha ...

  8. 对pca降维后的手写体数字图片数据分类_机器学习:数据的准备和探索——特征提取和降维...

    在数据的预处理阶段,特征提取和数据降维是提升模型表示能力的一种重要手段. 特征提取主要是从数据中找到有用的特征,用于提升模型的表示能力,而数据降维主要是在不减少模型准确率的情况下减少数据的特征数量. ...

  9. 下面哪个字段是http请求中必须具备的_理解HTTP协议-HTTP协议详解总结

    一.HTTP协议的演进 HTTP(HyperText Transfer Protocol)协议是基于TCP的应用层协议,它不关心数据传输的细节,主要是用来规定客户端和服务端的数据传输格式,最初是用来向 ...

最新文章

  1. Go游戏服务器开发的一些思考(九):Docker桥接网络及固定IP (二)
  2. 机器学习笔试题精选(七)
  3. Python学习笔记__5章 模块
  4. ssl提高组国庆模拟赛【2018.10.5】
  5. javaone_JavaOne正在重建动力
  6. DEVICE_ATTR设置0777引发血案
  7. Vue深入学习—虚拟DOM和Diff算法
  8. 数量查看_正则表达式学习之分割字符及数量词练习
  9. python选择排序_Python实现选择排序
  10. JQuery EasyUI之DataGrid列名和数据列分别设置不同对齐方式
  11. 炉石无限重新连接服务器,炉石传说:竟然还有这种无限资源的操作
  12. 浙江杭州一男子网恋1年求见面, 开门一看这美女长得像“李逵”
  13. 【数字图像处理】图像的几何变换之 图形平移与旋转
  14. php命令执行后门,phpstudy后门利用方法及getshell
  15. 02.1 知识图谱工具 Protege的下载安装与使用
  16. VUE 字符串拼接 各种拼接
  17. iOS 唤起微信小程序
  18. 50+PSD用户界面Web设计素材
  19. 批处理的简单小游戏 - 猜数字
  20. 画论19 李宗谔《黄筌竹赞序》

热门文章

  1. android盒子 小米遥控,小米盒子遥控器手机版下载-小米盒子遥控器appv6.0.0 安卓版 - 极光下载站...
  2. linux中隐藏得木马程序,Linux远控分析
  3. 微博5亿用户数据泄露:通讯录匹配机制是罪魁祸首!
  4. 转:绝对论坛元老,05年注册。告诉你一个真实的魅族
  5. Spatial Pyramid Pooling(SPP)原理简介
  6. Pyramid Mask Text Detector阅读笔记
  7. Request和Response的使用以及html连接java同步到数据的实例
  8. 【区块链与密码学】第9-8讲:群签名在区块链中的应用 II
  9. 【Basic Use Case】
  10. XXL-JOB适配人大金仓数据库kingbase