wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> medium >> roman numerals flow diag
(Message started by: Noke Lieu on Jan 28th, 2009, 6:24pm)

Title: roman numerals flow diag
Post by Noke Lieu on Jan 28th, 2009, 6:24pm
(perhaps it should live over in CS- but I don't go there much...)

Using the standard accepted notation of Roman numerals (IV, not IIIII etc) and MMMCMXCIX (3999) being the biggest number that can be written.

Construct a flow diagram that allows transliteration from Roman numerals to Hindu-Arabic.

(I did to try and find out why people struggled with Roman Numerals. It blows out  and gets messy quite quickly)

Title: Re: roman numerals flow diag
Post by Grimbal on Jan 29th, 2009, 12:49am
If you don't need a validation, I'd propose the following:

static String[] rom[] = { "CM", "M", "CD", "D",  "XC", "C", "XL", "L", "IX", "X", "IV", "V", "I" };
static String[] val[] = { 900, 1000, 400, 500,  90, 100, 40, 50, 9, 10, 4, 5, 1 };
public String roman2int(String roman){
int value = 0;
for( int r=0 ; r<rom.length ; r++ ){
 while( roman.startsWith(rom[r]) ){
   value += val[r];
   roman = roman.substring(rom[r].length());
 }
}
return "" + value;
}

Never compiled, let alone tested!

Title: Re: roman numerals flow diag
Post by Noke Lieu on Jan 29th, 2009, 4:01pm
possessing perhaps 1/16 ounce of programming ability, that looks like it'd work. Nicely done.

However, I was after something a bit more... human. Boxes with one instruction and arrows for "yes" and "no" sort of stuff- if you wanted to slum it...

Title: Re: roman numerals flow diag
Post by towr on Jan 30th, 2009, 12:21am
Start at the back with a total of zero; take the value of the current numeral (I=1, V=5, X=10, L=50, C=100, D=500, M=1000); if it is smaller than the last numeral subtract it from the total, otherwise add it; move to the next numeral.

Title: Re: roman numerals flow diag
Post by Noke Lieu on Feb 1st, 2009, 6:16pm
um. Yes. :-X

The reason I had such a big and billowy set of rules was I was tackling it from the left hand side.

Why? I was trying to understand why people who don't understand roman numerals don't understand roman numerals. We're taught to read for left to right, the count left to right... so the jump to doing things right to left seemed to not help for what I was doing, it became an intersting challenge too.

But such elegance wins, make no mistake about it.

Title: Re: roman numerals flow diag
Post by Grimbal on Feb 2nd, 2009, 12:50am

on 02/01/09 at 18:16:56, Noke Lieu wrote:
I was trying to understand why people who don't understand roman numerals don't understand roman numerals.

That's easy.  It is by definition.

@towr: excellent insight.

Title: Re: roman numerals flow diag
Post by towr on Feb 2nd, 2009, 1:28am

on 02/01/09 at 18:16:56, Noke Lieu wrote:
We're taught to read for left to right, the count left to right...
Euhm. I'm not sure I quite agree. Try 17326493094; don't you start from the right to break it up into thousands? You have no idea of how significant the leftmost digit is, until you counted its position from the right. And for example addition also starts with the least significant digits.
Granted, you can easily write an algorithm to parse it from the left to right (multiply by ten and add the next), but the natural way to understand it, I'd have to say, is right to left; like reading arabic.

Title: Re: roman numerals flow diag
Post by Noke Lieu on Feb 2nd, 2009, 3:31pm

on 02/02/09 at 01:28:35, towr wrote:
Euhm. I'm not sure I quite agree. Try 17326493094; don't you start from the right to break it up into thousands?


Interesting.

I counted in groups for three left to right 173, 264, 930, 94.
Then reorganised the groups, again left to right but starting 17 326....

Then to convert it to words, I counted right to left thousands, milliions...
It reminds me strongly of dealing (in another life) with Okazaki fragments (http://en.wikipedia.org/wiki/Okazaki_fragment)
Never really made the connection then.

What about 0.17326493094  ::)

Title: Re: roman numerals flow diag
Post by rmsgrey on Feb 3rd, 2009, 10:32am
The lesson is to evaluate numbers starting from the decimal point and working outward :P

Title: Re: roman numerals flow diag
Post by Grimbal on Feb 3rd, 2009, 10:44am
Actually, it is the opposite: you have to start from both ends.
- Start with a=b=0,
- for each digit from the left end to the dot, do a=a*10+digit,
- for each digit from the right end to the dot do b = (b+digit)/10,
- the value is a+b.
:P



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board