Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第4回 SG Tech スキルアップ勉強会「あなたもチャレンジ!コードレビューライブ 〜伊藤淳一からの挑戦状〜」の提出コード #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions lib/sg_romanizer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
class SgRomanizer
def romanize(arabic)
# write your code here
level4_num, level3_num, level2_num, level1_num = arabic.to_s.rjust(4).split('').map(&:to_i)
level1 =
case level1_num
when (1..3) then 'I' * level1_num
when 4 then 'IV'
when 5 then 'V'
when (6..8) then 'V' + 'I' * (level1_num - 5)
when 9 then 'IX'
else
''
end
level2 =
case level2_num
when (1..3) then 'X' * level2_num
when 4 then 'XL'
when 5 then 'L'
when (6..8) then 'L' + 'X' * (level2_num - 5)
when 9 then 'XC'
when 10 then 'C'
else
''
end
level3 =
case level3_num
when (1..3) then 'C' * level3_num
when 4 then 'CD'
when 5 then 'D'
when (6..8) then 'D' + 'C' * (level3_num - 5)
when 9 then 'CM'
when 10 then 'M'
else
''
end
level4 =
case level4_num
when (1..3) then 'M' * level4_num
else
''
end
"#{level4}#{level3}#{level2}#{level1}"
end

def deromanize(roman)
# write your code here
table = [
%w(I II III IV V VI VII VIII IX),
%w(X XX XXX XL L LX LXX LXXX XC),
%w(C CC CCC CD D DC DCC DCCC CM),
%w(M MM MMM),
]

remain_roman = roman
table.reverse.map do |row|
x = row.reverse.filter { remain_roman.match(/^#{_1}/) }.first
if x
remain_roman = remain_roman.sub(x, '')
row.find_index(x) + 1
else
'0'
end
end.join.to_i
end
end