LoginSignup
0
0

More than 1 year has passed since last update.

AOJトライに関する知識知見の記録共有 (Volume0-0039)

Posted at

概要

Roman Figure

コード

補助記号Zを使用


def roman_to_alabia(c):
    dict_r2a = {
        "Z": 0,
        "I": 1,
        "V": 5,
        "X": 10,
        "L": 50,
        "C": 100,
        "D": 500,
        "M": 1000,
    }
    return dict_r2a[c]

def calc_plus_minus(x, y):
    ret = 0
    xi, yi = [roman_to_alabia(r) for r in [x, y]]
    if xi < yi:
        ret = -1 * xi
    else:
        ret = xi
    return ret

def Roman_Figure(r_str):
    r_str = r_str + "Z"
    ret = 0
    for i in range(len(r_str)-1):
        ret = ret + calc_plus_minus(r_str[i], r_str[i+1])
    return ret

def hoge(*args):
    ret = []
    for arg in args:
        print(arg)
        ret.append(Roman_Figure(arg))
    return ret

print(hoge(
"IV",
"CCCCLXXXXVIIII",
"CDXCIX",
))

実行結果

デバッグログ出力部含む

IV
CCCCLXXXXVIIII
CDXCIX
[4, 499, 499]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0