-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroman_numerals_helper3.rb
61 lines (52 loc) · 1.59 KB
/
roman_numerals_helper3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
#require 'nokogiri'
require 'open-uri'
###
# For this solution, I decided to have fun and use Google in order to be hackier
# and also have the smallest code size lol.
#
# It is super slow, but passed.
#
# Originally, I used nokogiri because I expected to use its functionality, but
# then all I ended up doing was parsing the content as a String out of laziness.
#
# @author Jonathan Bradley Whited
# @see https://www.codewars.com/kata/roman-numerals-helper/ruby
# @rank 4 kyu
###
class RomanNumerals
def self.google(value,query)
#doc = Nokogiri::HTML(open("https://www.google.com/search?q=#{value}+#{query}"),nil,'utf-8').to_s
doc = open("https://www.google.com/search?q=#{value}+#{query}").read
doc.slice(doc.index("#{value} = "),100).gsub(/\A.*=[[:space:]]+/,'').gsub(/\<.*\z/,'')
end
def self.to_roman(num)
google(num,'to+roman+numerals')
end
def self.from_roman(roman)
google(roman,'to+arabic').to_i
end
end
def test_to(num,roman)
result = RomanNumerals.to_roman(num)
puts "#{result == roman ? '-' : 'X'} To: #{num} => #{roman}: #{result}"
end
def test_from(roman,num)
result = RomanNumerals.from_roman(roman)
puts "#{result == num ? '-' : 'X'} From: #{roman} => #{num}: #{result}"
end
test_to(1000,'M')
test_to(4,'IV')
test_to(1,'I')
test_to(1990,'MCMXC')
test_to(2008,'MMVIII')
test_from('XXI',21)
test_from('I',1)
test_from('IV',4)
test_from('MMVIII',2008)
test_from('MDCLXVI',1666)
puts if ARGV.length > 0
ARGV.each do |arg|
print "#{arg} => "
puts (arg =~ /\d/) ? RomanNumerals.to_roman(arg.to_i) : RomanNumerals.from_roman(arg)
end