数字转罗马数字_理解罗马数字

数字转罗马数字

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

数字转罗马数字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值