Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Roman numeral challenge!
Index -> Contests
Goto page Previous  1, 2, 3, 4
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zylum




PostPosted: Sat May 06, 2006 1:53 pm   Post subject: (No subject)

yeah i guess.. but the contest is over so i didnt think any harm would be done.

oh and yeah, solutions 2 and 3 are just hacks. like i said i only solved the first one.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Sat May 06, 2006 7:07 pm   Post subject: (No subject)

I am still wondering who the winners are Confused
Cervantes




PostPosted: Sat May 06, 2006 7:28 pm   Post subject: (No subject)

You're not very patient, are you? The last time you raised this issue, Martin said he hasn't forgotten about it. Do you doubt Martin's ability to remember things? As far as I see it, this is spam.
Martin




PostPosted: Sun May 07, 2006 7:18 pm   Post subject: (No subject)

I live!

Anyway guys, I'm very very sorry for taking this long. Cervantes is right - I didn't forget. Last week was Golden Week here in the land of the rising sun, which means absolutely no free time (and crazy things like partying and spur of the moment trips to Tokyo). I was at home to sleep and that's about it.

So, better late than never.

First, here are the solutions, in the order received (or reverse order received, perhaps).

MysticVegeta wrote:
By: MysticVegeta
Part 3
Lang: Java

code:
class isRomanNumeral {
        public static boolean is_roman_numeral (String n) {
                  //Converting it to a number
                  n = n.toUpperCase();
                  String ro = n;
          char [] roChars = {'I','V','X','L','C','D','M'};
          char [] arabicChars = {1,5,10,50,100,500,1000};
         
          int k = 0;
          int arabic = 0; 
         
          while (k < ro.length()) {
             int number = 0;
             
             for (int j = 0; j < roChars.length; j++)
                   if (roChars[j]==ro.charAt(k)) number = arabicChars[j];
               
             k++;
             
             if (k != ro.length()) {
                int next = 0;
                for (int j = 0 ; j < roChars.length; j++)
                                if (roChars[j] == ro.charAt(k)) next = arabicChars[j];
                               
                if (next > number) {
                   number = next - number;
                   k++;
                }
             }
             arabic += number;
                }
          //Converting to roman again
          String [] ones = {"I","II","III", "IV", "V", "VI", "VII", "VIII", "IX", "X","XX","XXX", "XL", "L", "LX", "LXX", "LXXX", "XC","C","CC","CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "M","MM","MMM", "Mv", "v", "vM", "vMM", "vMMM", "xM"};
                  String finalAnsReversed = ""; //Reverse of answers from ans
                  for (int i = 0; i < 4; i++) {
                        int mod = arabic % 10;
                        arabic /= 10;
                        if (mod == 0) continue; //dont want array[-1] rofl.
                        else finalAnsReversed = ones[(i*9)+(mod-1)] + finalAnsReversed;
                }
                //Main Answer, check to see if they yield the same result, if they do then it is valid, else its not
                if (finalAnsReversed.equals(n)) return true;
                else return false;
        }
       
        public static void main (String [] Args) {
                String n = "IIII";
                System.out.println("WELCOME TO MYSTIC'S ROMAN NUMERAL CHECKER");
                System.out.println();
                System.out.println(n + " is a valid roman numeral? " + is_roman_numeral(n));
                System.out.println();
        }
}


do_pete wrote:
Here are the first two functions (I did them in Turing). I'll PM you the last one when I'm done.

code:
fcn integer_to_roman_numeral (integer : nat, output : string) : string
    var numerals : array 1 .. 2, 1 .. 9 of string := init ("M", "D", "C", "L", "X", "IX", "V", "IV", "I", "1000", "500", "100", "50", "10", "9", "5", "4", "1")
    for i : 1 .. 9
        if integer >= strint (numerals (2, i)) then
            result integer_to_roman_numeral (integer - strint (numerals (2, i)), output + numerals (1, i))
        end if
    end for
    result output
end integer_to_roman_numeral

fcn roman_numeral_to_integer (input : string, total : nat) : nat
    var numerals : array 1 .. 2, 1 .. 9 of string := init ("M", "D", "C", "L", "X", "IX", "V", "IV", "I", "1000", "500", "100", "50", "10", "9", "5", "4", "1")
    for i : 1 .. 9
        if index (Str.Upper (input), numerals (1, i)) = 1 then
            result roman_numeral_to_integer (input (length (numerals (1, i)) + 1 .. length (input)), total + strint (numerals (2, i)))
        end if
    end for
    result total
end roman_numeral_to_integer

put integer_to_roman_numeral (2006, "")
put roman_numeral_to_integer ("MmVi", 0)


[Gandalf] wrote:
Solution to part 2 in Java:
Java:
    private String[] roman_numeral = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    private int[] numeral_value = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
       
    private int roman_numeral_to_integer(String numeral) {
        int result = 0, index = 0;
        for (int i = 0; i < roman_numeral.length; i++) {
            while (index + roman_numeral[i].length() <= numeral.length() && numeral.substring(index,index+roman_numeral[i].length()).equalsIgnoreCase(roman_numeral[i])) {
                result += numeral_value[i];
                index += roman_numeral[i].length();
            }
        }
        return result;
    }


MysticVegeta wrote:
Author: MysticVegeta
Lang: Java

Java:

// MADE BY MYSTIC VEGETA, CONVERTS ROMANS TO ARABIC
 // NOTE: I COULD PROGRAM THIS IS IN LESSER LINES, BUT IT WOULD BE IMPOSSIBLE TO READ, ROFL. <0_0>
 // LANG: JAVA
 
 
 class RomanToArabic {
 
       public static int roman_numeral_to_integer (String ro) {
          ro = ro.toUpperCase();
          char [] roChars = {'I','V','X','L','C','D','M'};
          char [] arabicChars = {1,5,10,50,100,500,1000};
         
          int k = 0;
          int arabic = 0; 
         
          while (k < ro.length()) {
             int number = 0;
             
             for (int j = 0; j < roChars.length; j++)
                   if (roChars[j]==ro.charAt(k)) number = arabicChars[j];
               
             k++;
             
             if (k != ro.length()) {
                int next = 0;
                for (int j = 0 ; j < roChars.length; j++)
                                if (roChars[j] == ro.charAt(k)) next = arabicChars[j];
                               
                if (next > number) {
                   number = next - number;
                   k++;
                }
             }
             arabic += number;
                }
                return arabic;
       }
         
       public static void main (String [] Args) {
                String n = "MMMCMXCIX";
                        System.out.println("WELCOME TO MYSTIC VEGETA'S CONVERTER");
                        System.out.println ("Edit the n so that its a roman numberal");
                        System.out.println ();
                        System.out.println (n + " = " + roman_numeral_to_integer (n));
                        System.out.println ();
       }
       
}


cool dude wrote:
hi, i tried the first problem and the only problem i have is if the number is 10^i -1. for example if the number is 99 then it should be XL but i get XXXX which is wrong! i'll keep trying. this is the code i have...

code:

var number : int
var remainder : int
var answer : array 1 .. 7 of int

put "enter a base 10 number"
get number


remainder := number mod 1000
answer (1) := number div 1000
for i : 1 .. answer (1)
    put "M" ..
end for

answer (2) := remainder div 500
remainder := number mod 500
for i : 1 .. answer (2)
    put "D" ..
end for

answer (3) := remainder div 100
remainder := number mod 100
for i : 1 .. answer (3)
    put "C" ..
end for

answer (4) := remainder div 50
remainder := number mod 50
for i : 1 .. answer (4)
    put "L" ..
end for

answer (5) := remainder div 10
remainder := number mod 10
for i : 1 .. answer (5)
    put "X" ..
end for

answer (6) := remainder div 5
remainder := number mod 5
for i : 1 .. answer (6)
    put "V" ..
end for

answer (7) := remainder div 1
for i : 1 .. answer (7)
    put "I" ..
end for


[Gandalf] wrote:
Here is my solution to part 1 in Turing:
code:
var roman_numeral : array 1 .. * of string := init ("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
var numeral_value : array 1 .. * of int := init (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)

%%% Solution #1 %%%
fcn integer_to_roman_numeral (num : int) : string
    var number : int := num
    var result_numeral : string := ""
    for i : 1 .. upper (roman_numeral)
        loop
            exit when number < numeral_value (i)
            result_numeral := result_numeral + roman_numeral (i)
            number -= numeral_value (i)
        end loop
    end for
    result result_numeral
end integer_to_roman_numeral

More to come, hopefully.


RESULTS! Best solution to each part is in red.

Results.

[Gandalf]
Part 1:(Turing)
Part 2:(Java)
Part 3:Didn't write.
600 bits!

cool_dude
Part 1:(Turing)
Part 2:Didn't write.
Part 3:Didn't write.
250 bits!

MysticVegeta
Part 1:(Java)
Part 2:(Java)
Part 3:(Java)
850 bits!

zylum
Part 1:(Java and Turing)
Part 2:Didn't write.
Part 3:Didn't write.
250 bits!

do_pete
Part 1:(Turing)
Part 2:(Turing)
Part 3:Didn't write.
600 bits!
MysticVegeta




PostPosted: Sun May 07, 2006 8:25 pm   Post subject: (No subject)

yay! I win! Smile I am now richer than admins rofl, just 7000 more to go for webhosting =\
Display posts from previous:   
   Index -> Contests
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 4 of 4  [ 50 Posts ]
Goto page Previous  1, 2, 3, 4
Jump to:   


Style:  
Search: