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

レビューお願い致します #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 45 additions & 2 deletions lib/sg_romanizer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
class SgRomanizer

BASE_SYMBOL={
1 => 'I',
5 => 'V',
10 => 'X',
50 => 'L',
100 => 'C',
500 => 'D',
1000 => 'M'
}

def romanize(arabic)
# write your code here
result=''
splitted_arabic_arr=split_by_digits(arabic)
splitted_arabic_arr.each do |arabic|
msd = arabic.digits[-1]
# 最上位桁の値で場合わけ
result += case msd
when 1,5
BASE_SYMBOL[arabic]
when 2,3
BASE_SYMBOL[arabic/msd]*msd
when 4,9
BASE_SYMBOL[arabic/msd] + BASE_SYMBOL[arabic+arabic/msd]
when 6,7,8
BASE_SYMBOL[arabic*5/msd] + BASE_SYMBOL[arabic/msd]*(msd-5)
end
end
result
end

def deromanize(roman)
# write your code here
result = []
arabic_arr = roman.chars.map {|r| BASE_SYMBOL.key(r)}
arabic_arr.each.with_index do |arabic,idx|
next_arabic = idx == arabic_arr.size - 1 ? 0 : arabic_arr[idx+1]
if arabic < next_arabic
result.push(-arabic)
else
result.push(arabic)
end
end
result.sum
end

# 数字を桁ごとに分割する.例1) 1030 -> [1000,30]. 例2) 691 -> [600,90,1]
def split_by_digits(arabic)
digits_arr = arabic.digits
digits_arr.map.with_index {|n,idx| n*(10**idx)}.reverse.reject{|n| n==0}
end
end