【Python CheckiO 题解】Roman Numerals


CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。

CheckiO 官网:https://checkio.org/

我的 CheckiO 主页:https://py.checkio.org/user/TRHX/

CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise


题目描述

【Roman Numerals】:将阿拉伯数字转换成罗马数字

【链接】https://py.checkio.org/mission/roman-numerals/

【输入】:一个整数形式的阿拉伯数字

【输出】:一个字符串形式的罗马数字

【前提】:0 < number < 4000

【范例】

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

解题思路

先将一到十、十位整数和百位整数以列表形式列出来,分为四种情况:一位数、两位数、三位数和四位数,每种情况还要判断是否为整数,不是整数的,将其除以位数的余,再次传给 checkio() 函数,求剩下的值。

这种方法比较繁琐,语法简单,不适合大一点的数字,太菜了,看看大神的解答吧

代码实现

def checkio(data):
    roman_list1 = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
    roman_list2 = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
    roman_list3 = ['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
    if 0 < data < 10:
        roman = roman_list1[data - 1]
        return roman
    elif 10 <= data < 100:
        if data % 10 == 0:
            roman = roman_list2[data // 10 - 1]
        else:
            roman = roman_list2[data // 10 - 1] + checkio(data % 10)
        return roman
    elif 100 <= data < 1000:
        if data % 100 == 0:
            roman = roman_list3[data // 100 - 1]
        else:
            roman = roman_list3[data // 100 - 1] + checkio(data % 100)
        return roman
    elif 1000 <= data < 9999:
        if data % 1000 == 0:
            roman = 'M' * (data // 1000)
        else:
            roman = 'M' * (data // 1000) + checkio(data % 1000)
        return roman


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'
    print('Done! Go Check!')

大神解答

大神解答 NO.1

def checkio(n):
    result = ''
    for arabic, roman in zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
                             'M     CM   D    CD   C    XC  L   XL  X   IX V  IV I'.split()):
        result += n // arabic * roman
        n %= arabic
    return result

大神解答 NO.2

roman1 = ('', 'M', 'MM', 'MMM')
roman2 = ('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM')
roman3 = ('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC')
roman4 = ('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX')

def checkio(data):
    data1 = int(data / 1000)
    data2 = int(data % 1000 / 100)
    data3 = int(data % 100 / 10)
    data4 = int(data % 10 )
    return roman1[data1] + roman2[data2] + roman3[data3] + roman4[data4]

大神解答 NO.3

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

大神解答 NO.4

from enum import Enum

class Roman(Enum):
    M  = 1000
    CM = 900
    D  = 500
    CD = 400
    C  = 100
    XC = 90
    L  = 50
    XL = 40
    X  = 10
    IX = 9
    V  = 5
    IV = 4
    I  = 1

    @classmethod
    def encode(cls, n):
        for numeral in cls:
            rep, n = divmod(n, numeral.value)
            yield numeral.name * rep

checkio = lambda n: ''.join(Roman.encode(n))

大神解答 NO.5

def checkio(data):
    base = "I"*data
    
    base = base.replace("I"*5, "V")
    base = base.replace("V"*2, "X")
    base = base.replace("X"*5, "L")
    base = base.replace("L"*2, "C")
    base = base.replace("C"*5, "D")
    base = base.replace("D"*2, "M")
    
    base = base.replace("DCCCC", "CM")
    base = base.replace("CCCC", "CD")
    base = base.replace("LXXXX", "XC")
    base = base.replace("XXXX", "XL")
    base = base.replace("VIIII", "IX")
    base = base.replace("IIII", "IV")
    
    return base
发布了145 篇原创文章 · 获赞 511 · 访问量 45万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览