Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Sep 2012 04:01 PM |
Write a function that returns a roman numeral for a certain number....
Expected Output:
print(GetRomanNumeral(3)) --> III
print(GetRomanNumeral(3533)) --> MMMDXXXIII
print(GetRomanNumeral(35)) --> XXXV
print(GetRomanNumeral(105)) --> CV
And then, for bonux points (Yes, it uses the same function) (It should be easy to find out if the input is a string or number):
print(GetRomanNumeral("VI")) --> 6
print(GetRomanNumeral("MNI")) --> nil
print(GetRomanNumeral("MMMDCXXXIV"))
|
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:15 PM |
| Poor me, I forgot how the multipling part works. Like VX -> 5 * 10 -> 50, or so... |
|
|
| Report Abuse |
|
|
booing
|
  |
| Joined: 04 May 2009 |
| Total Posts: 6594 |
|
|
| 29 Sep 2012 04:18 PM |
I officially make this a do the topic of the thread in the most inefficient way possible. function GetRomanNumeral(n) if n == 3 then print"III" end
if n == 3533 then print("MMMDXXXIII") end
if n == 35 then print("XXXV") end
if n == 105 then print("CV") end
if n == "VI" then print("6") end
if n == "MNI" then print(nil) end
if n == "MMMDXCXXXIV" then print("ogm i dont no") end
end
|
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Sep 2012 04:28 PM |
| @Booing. The numeral to string is easy. Not so much the other direction. |
|
|
| Report Abuse |
|
|
TaslemGuy
|
  |
| Joined: 10 Jun 2009 |
| Total Posts: 12174 |
|
|
| 29 Sep 2012 05:32 PM |
Converting from roman numeral to number is pretty easy.
function toNum(rom) local vals = {} vals["I"] = 1 vals["V"] = 5 vals["X"] = 10 vals["L"] = 50 vals["C"] = 100 vals["D"] = 500 vals["M"] = 1000 local v = {} local t = #rom+1 while t > 1 do t = t - 1 v[#v+1] = vals[rom:sub(t,t)] end local max = 0 local i = 0 local sum = 0 while i < #v do i = i + 1 if v[i] < max then v[i] = -v[i] end sum = sum + v[i] max = math.max(max,math.abs(v[i])) end return sum end |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 29 Sep 2012 05:36 PM |
I did this in java to solve a project euler problem. It's difficult because the roman numeral system is so inconsistent and there are no formal guidelines.
IIIII, V, and IIIIIX are all correct representations of the number 5. V is considered the best solution because it expresses the value in the shortest amount of characters. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 29 Sep 2012 05:43 PM |
function GetRomanNumeral(n) numerals = { {["name"] = "M", ["value"] = 1000}, {["name"] = "D", ["value"] = 500}, {["name"] = "C", ["value"] = 100}, {["name"] = "L", ["value"] = 50}, {["name"] = "X", ["value"] = 10}, {["name"] = "V", ["value"] = 5}, {["name"] = "I", ["value"] = 1} } if type(n) == "number" then number = math.floor(n) string = "" while number ~= 0 do found = false for i, v in pairs(numerals) do if found == false then if number/v.value >= 1 then found = true number = number - v.value string = string..v.name elseif v.name ~= "M" and v.name ~= "C" and numerals[i+1] then if number/(v.value - numerals[i+1].value) >= 1 then string = string..numerals[i+1].name..v.name number = number - v.value + numerals[i+1].value end end end end end end return string end
print(GetRomanNumeral(4))
One sec... I have to work on the bonus. This is the main part I think.
|
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 05:43 PM |
@pwnedu46 I thought that it was a rule of Roman numerals to never use the same character more than 3 times in a row?
So for the number 4: IIII would be invalid IIIIIIX would be invalid IV would be valid |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 29 Sep 2012 05:45 PM |
@LPG: From the projecteuler site:
"You will read about many different rules concerning Roman numerals, but the truth is that the Romans only had one simple rule:
Numerals must be arranged in descending order of size." |
|
|
| Report Abuse |
|
|
TaslemGuy
|
  |
| Joined: 10 Jun 2009 |
| Total Posts: 12174 |
|
|
| 29 Sep 2012 05:47 PM |
@LPG
The rules for Roman numerals are only really informal. No one has ever standardized them.
I think the "3 in a row rule" is silly, since I don't think it actually changes the result if you just ask for the shortest possible name.
Also, many clocks use IIII for 4, which violates that rule.
|
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 29 Sep 2012 05:49 PM |
| Dangit... That's not quite right T_T 9 returns VXIV lol |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 05:50 PM |
@pwndu46 @TaslemGuy
My elementary school teachers all lied to me! Why did we learn Roman numerals anyway? |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 29 Sep 2012 05:51 PM |
@LPG
To learn from our past mistakes. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Sep 2012 05:53 PM |
If you guys want the solution:
http://www.roblox.com/Roman-Numeral-Converter-item?id=93893121
There it is.
If you're going to solve it, go ahead and solve it. xD
|
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 29 Sep 2012 05:59 PM |
"My elementary school teachers all lied to me! Why did we learn Roman numerals anyway?"
It's a good application of what you learned in math at the time. |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 06:04 PM |
@pwnedu46 That's like saying drilling holes in random peoples' walls is a good application of your new found talent of using an electric drill. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 29 Sep 2012 06:05 PM |
| It is though. Welcome to the US educational system. |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 29 Sep 2012 06:25 PM |
"That's like saying drilling holes in random peoples' walls is a good application of your new found talent of using an electric drill."
I agree, but that's my best explanation. |
|
|
| Report Abuse |
|
|