CheckiO is for beginners and advanced programmers to code games, Python and JavaScript to address difficult challenges and interesting tasks, thereby improving your coding skills, this blog is mainly to record their ideas in Python do problems at the checkpoints and implementation code , but also learn from other great God wrote the code.
CheckiO official website: https://checkio.org/
My CheckiO Home: https://py.checkio.org/user/TRHX/
CheckiO problem solution series of columns: https://itrhx.blog.csdn.net/category_9536424.html
CheckiO all solution to a problem source: https://github.com/TRHX/Python-CheckiO-Exercise
Title Description
[Roman Numerals] : Converts Arabic numerals to Roman numerals
【链接】:https://py.checkio.org/mission/roman-numerals/
[Enter] : An integer form of Arabic numerals
[Output] : a string of Roman numerals
[Premise] : 0 <number <4000
[Example] :
checkio(6) == 'VI'
checkio(76) == 'LXXVI'
checkio(13) == 'XIII'
checkio(44) == 'XLIV'
checkio(3999) == 'MMMCMXCIX'
Problem-solving ideas
First one to ten, ten and one hundred integer integer be listed in tabular form, divided into four cases: a single digit, double-digit, three-digit and four-digit, but also to determine in each case whether or not an integer , it is not an integer, and dividing it by the number of bits of the remainder, again passed to checkio()
the function, find the rest of the value.
This method is cumbersome, simple syntax, not suitable for bigger numbers, too dishes, take a look at the god's answer it
Code
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!')
Okami answer
Okami answer 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
Okami answer 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]
Okami answer 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
Okami answer 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))
Okami answer 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