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 romanelif 10 <= data < 100:if data % 10 == 0:roman = roman_list2[data // 10 - 1]else:roman = roman_list2[data // 10 - 1] + checkio(data % 10)return romanelif 100 <= data < 1000:if data % 100 == 0:roman = roman_list3[data // 100 - 1]else:roman = roman_list3[data // 100 - 1] + checkio(data % 100)return romanelif 1000 <= data < 9999:if data % 1000 == 0:roman = 'M' * (data // 1000)else:roman = 'M' * (data // 1000) + checkio(data % 1000)return romanif __name__ == '__main__':# These "asserts" using only for self-checking and not necessary for auto-testingassert 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 * romann %= arabicreturn 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 % 1000if data / 100 != 0:s = s + mils[data/100]data = data % 100if data / 10 != 0:s = s + tens[data/10]data = data % 10if data / 1 != 0:s = s + ones[data/1]return s
大神解答 NO.4
from enum import Enumclass Roman(Enum):M = 1000CM = 900D = 500CD = 400C = 100XC = 90L = 50XL = 40X = 10IX = 9V = 5IV = 4I = 1@classmethoddef encode(cls, n):for numeral in cls:rep, n = divmod(n, numeral.value)yield numeral.name * repcheckio = lambda n: ''.join(Roman.encode(n))
大神解答 NO.5
def checkio(data):base = "I"*database = 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