Roman numeral challenge!
| Author |
Message |
Martin

|
Posted: Mon May 01, 2006 10:41 pm Post subject: (No subject) |
|
|
| Hey, sorry, I'll post results tonight - too busy right now. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
[Gandalf]

|
Posted: Mon May 01, 2006 11:18 pm Post subject: (No subject) |
|
|
I only had time for two pretty crappy solutions since I was doing multiple english essays throughout the week.  |
|
|
|
|
 |
MysticVegeta

|
Posted: Tue May 02, 2006 5:27 pm Post subject: (No subject) |
|
|
| I did all 3, part 3 was the easiest, all you had to do was convert the roman into arabic, then that arabic back to roman and cehck the original string argument with the roman you just got, if they match then it is valid else not. lol so basically a mixture of part 1 and 2 |
|
|
|
|
 |
GlobeTrotter
|
Posted: Tue May 02, 2006 5:32 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: I did all 3, part 3 was the easiest, all you had to do was convert the roman into arabic, then that arabic back to roman and cehck the original string argument with the roman you just got, if they match then it is valid else not. lol so basically a mixture of part 1 and 2
That won't necessarily work. If my memory serves me well, there are some cases where there are multiple valid ways to write a number as a roman numeral. There is probably a standard, but the other ways are still valid, I believe. |
|
|
|
|
 |
Cervantes

|
Posted: Tue May 02, 2006 5:35 pm Post subject: (No subject) |
|
|
GlobeTrotter wrote: That won't necessarily work. If my memory serves me well, there are some cases where there are multiple valid ways to write a number as a roman numeral. There is probably a standard, but the other ways are still valid, I believe.
No, I'm pretty sure there's only one way for each number. Unless you consider "VIIII" valid - it should be "IV" |
|
|
|
|
 |
MysticVegeta

|
Posted: Tue May 02, 2006 6:11 pm Post subject: (No subject) |
|
|
Cervantes wrote: GlobeTrotter wrote: That won't necessarily work. If my memory serves me well, there are some cases where there are multiple valid ways to write a number as a roman numeral. There is probably a standard, but the other ways are still valid, I believe.
No, I'm pretty sure there's only one way for each number. Unless you consider "VIIII" valid - it should be "IV"
oh noes, minsc needs more coffee!! "IX" |
|
|
|
|
 |
GlobeTrotter
|
Posted: Tue May 02, 2006 8:13 pm Post subject: (No subject) |
|
|
| Again, I'm not completely sure, but I was primarily referring to large numbers where C's, L's, and M's are involved. |
|
|
|
|
 |
MysticVegeta

|
Posted: Wed May 03, 2006 12:04 am Post subject: (No subject) |
|
|
| Nope still you are wrong, it could be written in multiple ways but only 1 is valid, go to the wiki link from Martin and scroll down to see what I and Cervantes are trying to tell you. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
MysticVegeta

|
Posted: Thu May 04, 2006 5:48 pm Post subject: (No subject) |
|
|
| May 4... whens the results coming up?? |
|
|
|
|
 |
Martin

|
Posted: Thu May 04, 2006 10:52 pm Post subject: (No subject) |
|
|
It's happening, it's happening, don't worry guys. This week has been hectic as hell, I'll do it as soon as I get a moment (I posted the solutions in the mod forum too so if you're really nice someone else might do it )
I'm really sorry for taking so long. |
|
|
|
|
 |
do_pete

|
Posted: Fri May 05, 2006 11:24 am Post subject: (No subject) |
|
|
| Could you post the solutions here as well so that we can see what others did? |
|
|
|
|
 |
zylum

|
Posted: Fri May 05, 2006 2:06 pm Post subject: (No subject) |
|
|
| edit: removed... |
|
|
|
|
 |
do_pete

|
Posted: Fri May 05, 2006 6:41 pm Post subject: (No subject) |
|
|
| What did you do for your solutions zylum? |
|
|
|
|
 |
zylum

|
Posted: Fri May 05, 2006 9:21 pm Post subject: (No subject) |
|
|
i really only submitted a solution for the first one... the rest are quite easy once you have that one solved.
| Turing: | var R : array 1 .. 13 of string := init ("I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M")
var D : array 1 .. 13 of int := init (1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000)
fcn integer_to_roman_numeral (n : int) : string
for decreasing i : 13 .. 1
if D (i ) <= n then
result R (i ) + integer_to_roman_numeral (n - D (i ))
end if
end for
result ""
end integer_to_roman_numeral
fcn roman_numeral_to_integer (s : string) : int
for i : 1 .. 3999
if integer_to_roman_numeral (i ) = Str.Upper (s ) then
result i
end if
end for
result 0
end roman_numeral_to_integer
fcn is_roman_numeral (s : string) : boolean
result Str.Upper (s ) = integer_to_roman_numeral (roman_numeral_to_integer (s ))
end is_roman_numeral
put roman_numeral_to_integer (integer_to_roman_numeral (3999))
put is_roman_numeral ("ABCDE")
put is_roman_numeral ("XXXIX")
put is_roman_numeral ("MMCCCXXIII") |
or in java
| Java: | import java.util.*;
class Integer_To_Roman_Numeral {
static String[] R = new String[] { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" };
static int[] D = new int[] { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
static String integer_to_roman_numeral (int n) {
for (int i = R.length-1; i >= 0; i--) if (D[i] <= n) return R[i] + integer_to_roman_numeral(n - D[i]);
return "";
}
static int roman_numeral_to_integer (String s) {
for (int i = 1; i < 4000; i++) if (s.toUpperCase().equals(integer_to_roman_numeral(i))) return i;
return 0;
}
static boolean is_roman_numeral (String s) {
return s.toUpperCase().equals(integer_to_roman_numeral(roman_numeral_to_integer(s)));
}
public static void main (String[] args) {
System.out.println(roman_numeral_to_integer(integer_to_roman_numeral(2424)));
System.out.println(is_roman_numeral("ABCE"));
System.out.println(is_roman_numeral("XXXIX"));
System.out.println(is_roman_numeral("MMCCCXXIII"));
}
} |
recursion for the first one
i dont think you were supposed to use those extra parameters in your functions. |
|
|
|
|
 |
Cervantes

|
Posted: Sat May 06, 2006 10:34 am Post subject: (No subject) |
|
|
zylum wrote: i really only submitted a solution for the first one... the rest are quite easy once you have that one solved.
| Turing: |
fcn roman_numeral_to_integer (s : string) : int
for i : 1 .. 3999
if integer_to_roman_numeral (i ) = Str.Upper (s ) then
result i
end if
end for
result 0
end roman_numeral_to_integer
|
| Java: |
static int roman_numeral_to_integer (String s) {
for (int i = 1; i < 4000; i++) if (s.toUpperCase().equals(integer_to_roman_numeral(i))) return i;
return 0;
} |
And zylum, don't you think you should have let Martin decide whether he wanted to post the solutions in this thread? |
|
|
|
|
 |
|
|