Skip to content

2024-07-01 v. 6.0.0: added "12. Integer to Roman" #657

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

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 7. Reverse Integer | [Link](https://leetcode.com/problems/reverse-integer/) | [Link](./lib/medium/7_reverse_integer.rb) |
| 8. String to Integer (atoi) | [Link](https://leetcode.com/problems/string-to-integer-atoi/) | [Link](./lib/medium/8_string_to_integer_atoi.rb) |
| 11. Container With Most Water | [Link](https://leetcode.com/problems/container-with-most-water/) | [Link](./lib/medium/11_container_with_most_water.rb) |
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/12_integer_to_roman.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '5.9.9'
s.version = '6.0.0'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
32 changes: 32 additions & 0 deletions lib/medium/12_integer_to_roman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: false

# https://leetcode.com/problems/integer-to-roman/
# @param {Integer} num
# @return {String}
def int_to_roman(num)
roman_numbers = {
1000 => 'M',
900 => 'CM',
500 => 'D',
400 => 'CD',
100 => 'C',
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
1 => 'I'
}

result = ''
roman_numbers.each do |value, symbol|
while num >= value
result << symbol
num -= value
end
end

result
end
13 changes: 13 additions & 0 deletions test/medium/test_12_integer_to_roman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/12_integer_to_roman'
require 'minitest/autorun'

class IntegerToRomanTest < ::Minitest::Test
def test_default
assert_equal('MMMDCCXLIX', int_to_roman(3749))
assert_equal('LVIII', int_to_roman(58))
assert_equal('MCMXCIV', int_to_roman(1994))
end
end
Loading