Created
October 5, 2012 19:59
-
-
Save idlehands/3842016 to your computer and use it in GitHub Desktop.
DBC Level 1 Roman numerals challenge PT 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def roman_numerals(num) | |
arabic_numbers = [1000, 500, 100, 50, 10, 5, 1] | |
raw_roman_numbers = [] | |
#POPULATE ARRAY WITH CORRECT NUMBERS IN ARABIC FORM | |
unless num == 0 | |
arabic_numbers.each do |i| | |
if i <= num | |
(num/i).times do | |
raw_roman_numbers << i | |
num -= i | |
end | |
end | |
end | |
end | |
def roman_numerals(num) | |
arabic_numbers = [1000, 900 , 500, 400 , 100, 90 , 50, 40 , 10, 9 , 5 , 4, 1] | |
raw_roman_numbers = [] | |
#POPULATE ARRAY WITH CORRECT NUMBERS IN ARABIC FORM | |
unless num == 0 | |
arabic_numbers.each do |i| | |
if i <= num | |
(num/i).times do | |
raw_roman_numbers << i | |
num -= i | |
end | |
end | |
end | |
end | |
#REPLACE ARABIC NUMBERS WITH ROMAN EQUIVALENT | |
romans = { 1000 => "M" , 900 => "CM" , 500 => "D" , 400 => "CD" , 100 => "C" , 90 => "XC", 40 => 'XL', 50 => "L" , 10 => "X" , 9 => "IX" , 5 => "V" , 4 => "IV" , 1 => "I" } | |
roman_array = raw_roman_numbers.map { |num| num = romans[num]}.join.to_s | |
end | |
puts "Enter a number:" | |
num = gets.chomp.to_i | |
puts roman_numerals(num) | |
#REPLACE ARABIC NUMBERS WITH ROMAN EQUIVALENT | |
romans = { 1000 => "M" , 500 => "D" , 100 => "C" , 50 => "L" , 10 => "X" , 5 => "V" , 1 => "I"} | |
raw_roman_numbers.map { |num| num = romans[num]}.join | |
end | |
puts roman_numerals(2346) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment