From 1f776fde12738be24e3f3dfe12108f136d5a6a7e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 4 May 2021 07:45:50 -0700 Subject: [PATCH 1/2] Roman to Integer --- maths/roman_to_integer.rb | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 maths/roman_to_integer.rb diff --git a/maths/roman_to_integer.rb b/maths/roman_to_integer.rb new file mode 100644 index 0000000..c2f2d3d --- /dev/null +++ b/maths/roman_to_integer.rb @@ -0,0 +1,96 @@ +# Challenge name: Roman to Integer +# +# Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. +# +# Symbol Value +# I 1 +# V 5 +# X 10 +# L 50 +# C 100 +# D 500 +# M 1000 +# +# For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. +# +# Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: +# +# I can be placed before V (5) and X (10) to make 4 and 9. +# X can be placed before L (50) and C (100) to make 40 and 90. +# C can be placed before D (500) and M (1000) to make 400 and 900. +# Given a roman numeral, convert it to an integer. +# +# + +# Approach 1: Left-to-Right Pass +# + +# Complexity Analysis +# +# Let n be the length of the input string (the total number of symbols in it). +# +# Time complexity: O(1). +# As there is a finite set of roman numerals. +# +# Space complexity: O(1). +# Because only a constant number of single-value variables are used, the space complexity is O(1). + +ROM_NUMS = { + "I" => 1, + "V" => 5, + "X" => 10, + "L" => 50, + "C" => 100, + "D" => 500, + "M" => 1000 +} + +# Now, recall that each symbol adds its own value, except for when a smaller +# valued symbol is before a larger valued symbol. In those cases, instead of +# adding both symbols to the total, we need to subtract the large from the +# small, adding that instead. + +# Therefore, the simplest algorithm is to use a pointer to scan through the +# string, at each step deciding whether to add the current symbol and +# go forward 1 place, or add the difference of the next 2 symbols and +# go forward 2 places. + +def roman_to_int(s) + res = 0 + temp = 0 + + s.chars.each_with_index do |el, i| + # subtractive case: if at least 2 symbols remaining AND value of s[i] < value of s[i + 1] + if ROM_NUMS[s[i + 1]] && ROM_NUMS[el] < ROM_NUMS[s[i+1]] + temp = ROM_NUMS[el] + else + # Else this is NOT the subtractive case. + res += (ROM_NUMS[el] - temp) + temp = 0 + end + end + + res +end + +s = "III" +puts roman_to_int(s) +# Output: 3 + +s = "IV" +puts roman_to_int(s) +# Output: 4 + +s = "IX" +puts roman_to_int(s) +# Output: 9 + +s = "LVIII" +puts roman_to_int(s) +# Output: 58 +# Explanation: L = 50, V= 5, III = 3. + +s = "MCMXCIV" +puts roman_to_int(s) +# Output: 1994 +# Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. From 9cc2e087c6801604cf303853efe3972521aa99a5 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 4 May 2021 14:46:10 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 15bcf9f..cd81998 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -87,6 +87,7 @@ * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) * [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb) * [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb) + * [Roman To Integer](https://github.com/TheAlgorithms/Ruby/blob/master/maths/roman_to_integer.rb) * [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb) * [Square Root Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root_test.rb) * [Sum Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/sum_of_digits.rb)