Checkio: Roman numerals

题目:

Roman numerals come from the ancient Roman numbering system. They are based on specific letters of the alphabet which are combined to signify the sum (or, in some cases, the difference) of their values. The first
ten Roman numerals are:

I, II, III, IV, V, VI, VII, VIII, IX, and X.

The Roman numeral system is decimal based but not directly positional and does not include a zero. Roman numerals are based on combinations of these seven symbols:

  • Symbol Value
  • I 1 (unus)
  • V 5 (quinque)
  • X 10 (decem)
  • L 50 (quinquaginta)
  • C 100 (centum)
  • D 500 (quingenti)
  • M 1,000 (mille)

More additional information about roman numerals can be found on the
Wikipedia article.

For this task, you should return a roman numeral using the specified integer value ranging from 1 to 3999.

Input: A number as an integer.

Output: The Roman numeral as a string.

Example:

checkio(6)=='VI'
checkio(76)=='LXXVI'
checkio(13)=='XIII'
checkio(44)=='XLIV'
checkio(3999)=='MMMCMXCIX'

How it is used: This is an educational task that allows you to explore
different numbering systems. Since roman numerals are often used in the typography, it can alternatively be used for text generation. The year of construction on building faces and cornerstones is most often written by Roman numerals. These numerals have many
other uses in the modern world and you read about it here... Or maybe you will have a customer from
Ancient Rome ;-)

Precondition: 0 < number < 4000

我的代码如下,尽管比较简单,我觉得这个代码算是比较清晰的了。

def checkio(data):

    s = ''
    ones = ['X','I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
    tens = ['C', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
    mils = ['M', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']

    if data / 1000 != 0:
        s = s + 'M'*(data/1000)
        data = data % 1000
    if data / 100 != 0:
        s = s + mils[data/100]
        data = data % 100
    if data / 10 != 0:
        s = s + tens[data/10]
        data = data % 10
    if data / 1 != 0:
        s = s + ones[data/1]
    return s

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio(6) == 'VI', '6'
    assert checkio(76) == 'LXXVI', '76'
    assert checkio(499) == 'CDXCIX', '499'
    assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'

下面是checkio上面clear里面最火的答案:

elements = { 1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD',
             100 : 'C', 90 : 'XC', 50 : 'L', 40: 'XL',
             10 : 'X', 9 : 'IX', 5 : 'V', 4: 'IV', 1 : 'I' }

def checkio(data):
    roman = ''

    for n in sorted(elements.keys(), reverse=True):
        while data >= n:
            roman += elements[n]
            data -= n

    return roman

看了吧,总是有人思路更清晰些。Come and On!

Checkio: Roman numerals

时间: 2024-10-07 10:05:37

Checkio: Roman numerals的相关文章

Roman numerals

Roman numerals 罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, 90)->('X', 'XL', 'L', 'XC') 1 def checkio(data): 2 rel = '' 3 4 thonsand = data / 1000 5 rel += thonsand * 'M' 6 7 data %= 1000 8 9 table = [['C', 'CD'

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

CodeForcesGym 100641D Generalized Roman Numerals

Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100641D64-bit integer IO format: %I64d      Java class name: (Any) The ancient Romans developed a terrible numbering syste

uva :185 - Roman Numerals(dfs)

题目:uva :185 - Roman Numerals 题目大意:给出一个字符串的等式,问这个字符串是否是罗马等式吗?有符合的阿拉伯等式吗?前者是就输出correct or incorrect ,后者就得分情况: ambiguous 能组成阿拉伯等式的字母组合大于等于2, valid 能组成阿拉伯等式的字母组合只有1种 impossible 没有符合阿拉伯等式的字母组合. 解题思路: 1.能不能组成罗马等式的规则:每个当前的字母(i)的左边的字母((i-1)所对应的值如果比i所对应的值小的话,

CSU1622: Generalized Roman Numerals(区间DP)

Description Input Output Sample Input IVX XIXIX 0 Sample Output Case 1: 4 6 Case 2: 8 10 28 30 32 HINT Source 题意:给出一个罗马数字,要你输出这罗马数字所有可能组成的数 罗马数字组成规则: 1.左边的字母大于等于右边的字母,两者相加 2.左边的字母小于右边的字母,后者减去前者 3.一个罗马数字串,可以随意组合 思路:这题要用区间dp处理,dp[i][j],代表位置i之后的j个数所能组成的

Roman Numeral Converter

将给定的数字转换成罗马数字. 所有返回的 罗马数字 都应该是大写形式. 这是一些对你有帮助的资源: Roman Numerals Array.splice() Array.indexOf() Array.join() 这个我还没做出来..

LeetCode第[13]题(Java):Roman to Integer

题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is

Facebook interview problem:13. Roman to Integer

description: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as,

13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i