small medium large xlarge

I wanted to cry when I compared my solution (44 lines) to Chris’ (13 lines). My solution was this:

``````def old_roman_numerals num
num = gets.chomp.to_i
original_num = num
roman_num = []

if num >= 1000
roman_num.push 'M' * (num/1000)
num = num%1000
end

if num >= 500
roman_num.push 'D' * (num/500)
num = num%500
end

if num >= 100
roman_num.push 'C' * (num/100)
num = num%100
end

if num >= 50
roman_num.push 'L' * (num/50)
num = num%50
end

if num >= 10
roman_num.push 'X' * (num/10)
num = num%10
end

if num >= 5
roman_num.push 'V' * (num/5)
num = num%5
end

if num >= 1
roman_num.push 'I' * (num/1)
end

puts "The number #{original_num} in old roman is #{roman_num.join}"
end

puts old_roman_numerals(puts 'Give me an integer:')
``````

So I obviously simplified my code but decided to keep the use of an array instead of a string:

``````def old_roman_numerals num
num = gets.chomp.to_i
original_num = num
roman_num = []

roman_num.push 'M' * (num			 / 1000)
roman_num.push 'D' * (num% 1000 /  500)
roman_num.push 'C' * (num%  500 /  100)
roman_num.push 'L' * (num%  100 /   50)
roman_num.push 'X' * (num%   50 /   10)
roman_num.push 'V' * (num%   10 /    5)
roman_num.push 'I' * (num%    5 / 	  1)

puts "The number #{original_num} in old roman is #{roman_num.join}"
end

puts old_roman_numerals(puts 'Give me an integer:')
``````

So I just wanted to know, why would using a string be better than using an array? Or is it just preference?

In this case, I don’t think a string is an better or worse than an array. Certainly if I were doing any processing on the result, I’d want to keep it an array. But since I knew I wanted it in string form anyway… [shrug]

You must be logged in to comment